失效链接处理 |
Servlet注解详解 PDF 下载
本站整理下载:
提取码:qhhz
相关截图:
主要内容:
Servlet 3.0 新特性概览
1.Servlet、Filter、Listener无需在web.xml中进行配置,可以通过Annotation进行配置;
2.模块化编程,即将各个Servlet模块化,将配置文件也分开配置。
3.Servlet异步处理,应对复杂业务处理;
4.异步Listener,对于异步处理的创建、完成等进行监听;
5. 文件上传API简化;
tomcat 7.0.X 支持Servlet 3.0
一、Annotation支持
1.Servlet
原本Servlet开发完后,必须在web.xml中配置如下代码:
[html] view plain copy
1.<servlet>
2. <servlet-name></servlet-name>
3. <servler-class></servlet-class>
4. <load-on-startup></load-on-startup>
5. <init-param>
6. <param-name></param-name>
7. <param-value></param-value>
8. </init-param>
9.</servlet>
10.<servlet-mapping>
11. <servlet-name></servlet-name>
12. <url-pattern></url-pattern>
13.</servlet-mapping>
[html] view plain copy
1.<servlet>
2. <servlet-name></servlet-name>
3. <servler-class></servlet-class>
4. <load-on-startup></load-on-startup>
5. <init-param>
6. <param-name></param-name>
7. <param-value></param-value>
8. </init-param>
9.</servlet>
10.<servlet-mapping>
11. <servlet-name></servlet-name>
12. <url-pattern></url-pattern>
13.</servlet-mapping>
现在只需要在Java源文件的Servlet类前面加上:
@WebServlet(name="",urlPatterns={""},initParams={@WebInitParam(name="",value=""),loadOnStartup=1})
public class FirstServlet extends HttpServlet{}
代码示例:
实现一个最简单的Servlet。
[java] view plain copy
1.package org.servlet;
2.import java.io.*;
3.import javax.servlet.*;
4.import javax.servlet.http.*;
5.import javax.servlet.annotation.*;
6./*
7. name == <servlet-name>
8. urlPatterns == <url-pattern>,
9. loadOnStartup == <load-on-startup>
10. initParam == <init-param>
11. name == <param-name>
12. value == <param-value>
13.*/
14.@WebServlet(name="HelloServlet" ,urlPatterns={"/HelloServlet"},loadOnStartup=1,
15. initParams={
16. @WebInitParam(name="name",value="xiazdong"),
17. @WebInitParam(name="age",value="20")
18. })
19.public class HelloServlet extends HttpServlet{
20. public void init(ServletConfig config)throws ServletException{
21. super.init(config);
22. }
23. public void service(HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException{
24. request.setCharacterEncoding("GBK");
25. ServletConfig config = getServletConfig();
26. PrintWriter out = response.getWriter();
27. out.println("<html>");
28. out.println("<body>");
29. out.println("Hello world"+"<br />");
30. out.println(config.getInitParameter("name"));
31. out.println("</body>");
32. out.println("</html>");
33. }
34.}
[java] view plain copy
1.package org.servlet;
2.import java.io.*;
3.import javax.servlet.*;
4.import javax.servlet.http.*;
5.import javax.servlet.annotation.*;
6./*
7. name == <servlet-name>
8. urlPatterns == <url-pattern>,
9. loadOnStartup == <load-on-startup>
10. initParam == <init-param>
11. name == <param-name>
12. value == <param-value>
13.*/
14.@WebServlet(name="HelloServlet" ,urlPatterns={"/HelloServlet"},loadOnStartup=1,
15. initParams={
16. @WebInitParam(name="name",value="xiazdong"),
17. @WebInitParam(name="age",value="20")
18. })
19.public class HelloServlet extends HttpServlet{
20. public void init(ServletConfig config)throws ServletException{
21. super.init(config);
22. }
23. public void service(HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException{
24. request.setCharacterEncoding("GBK");
25. ServletConfig config = getServletConfig();
26. PrintWriter out = response.getWriter();
27. out.println("<html>");
28. out.println("<body>");
29. out.println("Hello world"+"<br />");
30. out.println(config.getInitParameter("name"));
31. out.println("</body>");
32. out.println("</html>");
33. }
34.}
这样的话只需要将class文件放入WEB-INF\classes 中,不需要再web.xml中作任何改动就完成部署;
|