码迷,mamicode.com
首页 > Web开发 > 详细

servlet url通配符

时间:2015-03-31 23:48:34      阅读:189      评论:0      收藏:0      [点我收藏+]

标签:

在配置servlet映射时,<servlet-mapping>节点的子节点<url-pattern>可以用通配符来配置,通配符格式有以下2种:

  1. 以/开头/*结尾,如/*、/news/*
  2. *.后缀名,如*.do

通配符匹配原则:

  • 相似度越高,匹配度越高
  • *.后缀名的优先级最低

举例:

如果一个网站要暂时关闭整改,不管用户访问什么资源,页面都跳出“网站在整改中,暂时关闭”,现在只需要写一个这样的servlet

package com.zjb.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class HelloServlet extends HttpServlet {

    /**
     * The doGet method of the servlet. <br>
     * 
     * This method is called when a form has its tag value method equals to get.
     * 
     * @param request
     *            the request send by the client to the server
     * @param response
     *            the response send by the server to the client
     * @throws ServletException
     *             if an error occurred
     * @throws IOException
     *             if an error occurred
     */
    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        response.setContentType("text/html");
        response.setCharacterEncoding("utf-8");
        PrintWriter out = response.getWriter();
        out.println("网站在整改中,暂时关闭");
    }

    /**
     * The doPost method of the servlet. <br>
     * 
     * This method is called when a form has its tag value method equals to
     * post.
     * 
     * @param request
     *            the request send by the client to the server
     * @param response
     *            the response send by the server to the client
     * @throws ServletException
     *             if an error occurred
     * @throws IOException
     *             if an error occurred
     */
    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        doGet(request, response);
    }

}

然后在web.xml中做如下配置即可:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" 
    xmlns="http://java.sun.com/xml/ns/javaee" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
    http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
  <display-name></display-name>
  <servlet>
    <description>This is the description of my J2EE component</description>
    <display-name>This is the display name of my J2EE component</display-name>
    <servlet-name>HelloServlet</servlet-name>
    <servlet-class>com.zjb.servlet.HelloServlet</servlet-class>
  </servlet>


  <servlet-mapping>
    <servlet-name>HelloServlet</servlet-name>
    <url-pattern>/*</url-pattern>
  </servlet-mapping>    
</web-app>

 

servlet url通配符

标签:

原文地址:http://www.cnblogs.com/zhang-tech/p/4382393.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!