Servlet3.0的注解支持[转]

注解支持

    开发者可以用注解标记Servlet,Filter等,而不用在部署描述符web.xml文件中配置。随着注解的引入,部署描述符web.xml文件成为可选的。

 

 现在创建Servlet相当简单:

UserServlet.java:

 

 

Java代码 复制代码
  1. package com.tanlan.servlet3;   
  2.   
  3. import java.io.IOException;   
  4. import javax.servlet.ServletException;   
  5. import javax.servlet.annotation.WebServlet;   
  6. import javax.servlet.http.HttpServlet;   
  7. import javax.servlet.http.HttpServletRequest;   
  8. import javax.servlet.http.HttpServletResponse;   
  9. /**  
  10.  * 处理用户信息的Servlet  
  11.  * @author tanlan [email protected]  
  12.  * @date 2009-08-09  
  13.  */  
  14. @WebServlet(urlPatterns={"/UserServlet"})   
  15. public class UserServlet extends HttpServlet{   
  16.     protected void doGet(HttpServletRequest request, HttpServletResponse response)   
  17.     throws ServletException, IOException {   
  18.       String userName=request.getParameter("userName");   
  19.       request.setAttribute("userName", userName);   
  20.       //处理逻辑...   
  21.       request.getRequestDispatcher("result.jsp").forward(request, response);   
  22.     }    
  23.     protected void doPost(HttpServletRequest request, HttpServletResponse response)   
  24.     throws ServletException, IOException {   
  25.        doGet(request,response);   
  26.     }   
  27. }  
package com.tanlan.servlet3;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
 * 处理用户信息的Servlet
 * @author tanlan [email protected]
 * @date 2009-08-09
 */
@WebServlet(urlPatterns={"/UserServlet"})
public class UserServlet extends HttpServlet{
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
      String userName=request.getParameter("userName");
      request.setAttribute("userName", userName);
      //处理逻辑...
      request.getRequestDispatcher("result.jsp").forward(request, response);
    } 
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
       doGet(request,response);
    }
}

 

  注解@WebServlet用于在Web应用程序中标记一个继承了HttpServlet的类为Servlet。其属性urlPatterns的值即是web.xml中的url-pattern。在地址栏输入如下的路径即可访问改Servlet:

  http://localhost:8080/Javaee6web/UserServlet?userName=tanlan

 

创建过滤器如下

CharSetFilter.java:

Java代码 复制代码
  1. package com.tanlan.servlet3;   
  2.   
  3. import java.io.IOException;   
  4. import javax.servlet.Filter;   
  5. import javax.servlet.FilterChain;   
  6. import javax.servlet.FilterConfig;   
  7. import javax.servlet.ServletException;   
  8. import javax.servlet.ServletRequest;   
  9. import javax.servlet.ServletResponse;   
  10. import javax.servlet.annotation.WebFilter;   
  11. /**  
  12.  * 处理正文问题的Filter  
  13.  * @author tanlan [email protected]  
  14.  * @date 2009-08-09  
  15.  */  
  16.   
  17. @WebFilter(urlPatterns={"/*"})   
  18. public class CharSetFilter implements Filter {   
  19.     public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)   
  20.     throws IOException, ServletException {   
  21.     request.setCharacterEncoding("UTF-8");   
  22.         chain.doFilter(request, response);   
  23.     }   
  24.     public void init(FilterConfig filterConfig) throws ServletException {   
  25.     }   
  26.     public void destroy() {   
  27.     }   
  28. }  
package com.tanlan.servlet3;

import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
/**
 * 处理正文问题的Filter
 * @author tanlan [email protected]
 * @date 2009-08-09
 */

@WebFilter(urlPatterns={"/*"})
public class CharSetFilter implements Filter {
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
	throws IOException, ServletException {
	request.setCharacterEncoding("UTF-8");
        chain.doFilter(request, response);
    }
    public void init(FilterConfig filterConfig) throws ServletException {
    }
    public void destroy() {
    }
}

 注解@WebFilter用于在Web应用程序中标记一个实现了Filter接口的类为过滤器。

 

为Servlet或Filter配置初始化参数分别如下:

Java代码 复制代码
  1. @WebServlet(urlPatterns={"/UserServlet"},initParams={@WebInitParam(name="name",value="Servlet's tanlan")})   
  2.   
  3. @WebFilter(urlPatterns={"/*"},initParams={@WebInitParam(name="name",value="Filter's tanlan")})   
@WebServlet(urlPatterns={"/UserServlet"},initParams={@WebInitParam(name="name",value="Servlet's tanlan")})

@WebFilter(urlPatterns={"/*"},initParams={@WebInitParam(name="name",value="Filter's tanlan")}) 

 

调用的代码分别为:

UserServlet.java:

Java代码 复制代码
  1. public void init(ServletConfig config) throws ServletException {   
  2.    System.out.print(config.getInitParameter("name"));   
  3. }  
    public void init(ServletConfig config) throws ServletException {
       System.out.print(config.getInitParameter("name"));
    }

 CharSetFilter.java:

Java代码 复制代码
  1. public void init(FilterConfig filterConfig) throws ServletException {   
  2.     System.out.println(filterConfig.getInitParameter("name"));   
  3. }  
    public void init(FilterConfig filterConfig) throws ServletException {
        System.out.println(filterConfig.getInitParameter("name"));
    }

 

 

你可能感兴趣的:(java,Web,jsp,xml,servlet)