J2EE技术规范(三)——Servlet的生命周期与接口
分类: java
一、Servlet概览
什么是Servlet
(1) Servlet是提供基于协议的请求/响应服务的java类
(2) HTTP servlets是能被HTTP命令调用的servlet
二、理解servlet生命周期
Servlet生命周期通过Web容器控制
简单的概括这就分为四步:servlet类加载--->实例化--->服务--->销毁。
三、HTTP Servlet示例
(1)HTTP servlet继承自Httpservlet
(2)每一次请求都通过servlet容器调用服务方法
servlet示例:
[java]view plaincopyprint?
1. import javax.servlet.*;
2. import javax.servlet.http.*;
3. import java.io.*;
4.
5. public class MyServlet extends HttpServlet{
6. public void service(HttpServletRequest req,HttpServletResponse res)throws
7. ServletException,IOException{
8. //process request,generate response
9. }
10. }
import javax.servlet.*;import javax.servlet.http.*;
import java.io.*;
public class MyServlet extends HttpServlet{
public void service(HttpServletRequest req,HttpServletResponse res)throws
ServletException,IOException{
//process request,generate response
}
}
四、HttpServletRequest接口
HttpServletRequest接口代表了从客户来的请求处理HTTP请求的servlet示例:
[java]view plaincopyprint?
1. import javax.servlet.*;import javax.servlet.http.*;
2. import java.io.*;
3.
4. public class MyServlet extends HttpServlet{
5. public void service(HttpServletRequest req,HttpServletResponse res)throws
6. ServletException,IOException{
7. String server=req.getServerName();
8. int port=req.getServerPort();
9. Enumeration headers=req.getHeaders();
10. String method=req.getMethod(0);
11. String query=req.getQueryString();
12. String param1=req.getParameter("p1");
13. }
14. }
import javax.servlet.*;import javax.servlet.http.*;import java.io.*;
public class MyServlet extends HttpServlet{
public void service(HttpServletRequest req,HttpServletResponse res)throws
ServletException,IOException{
String server=req.getServerName();
int port=req.getServerPort();
Enumeration headers=req.getHeaders();
String method=req.getMethod(0);
String query=req.getQueryString();
String param1=req.getParameter("p1");
}
}
五、HttpServletResponse接口
HttpServletResponse接口代表Servlet的响应
处理HTTP请求的servlet示例:
[java]view plaincopyprint?
1. import javax.servlet.*;import javax.servlet.http.*;
2. import java.io.*;
3.
4. public class MyServlet extends HttpServlet{
5. public void service(HttpServletRequest req,HttpServletResponse res)throws
6. ServletException,IOException{
7. res.setContentType("text/html");
8. printWriter out=res.getWriter();
9. res.setBufferSize(1024);
10. res.addHeader("headerName","value");
11. res.sendError(404,"error message");
12. }
import javax.servlet.*;import javax.servlet.http.*;import java.io.*;
public class MyServlet extends HttpServlet{
public void service(HttpServletRequest req,HttpServletResponse res)throws
ServletException,IOException{
res.setContentType("text/html");
printWriter out=res.getWriter();
res.setBufferSize(1024);
res.addHeader("headerName","value");
res.sendError(404,"error message");
}
我们接下来看一个简单的servlet示例:
[java]view plaincopyprint?
1. import javax.servlet.*;
2. import javax.servlet.http.*;
3. import java.io.*;
4.
5. public class MyServlet extends HttpServlet{
6. public void service(HttpServletRequest req,HttpServletResponse res)throws
7. ServletException,IOException{
8. res.setContentType("text/html");
9. printWriter out=res.getWriter();
10. out.println("<html>");
11. out.println("<head><title>simgle page</head></title>");
12. out.println("<body>");
13. out.println("<h1><center>Hello World</center></h1>");
14. out.println("</body>");
15. out.println("</html>");
16. }
import javax.servlet.*;import javax.servlet.http.*;
import java.io.*;
public class MyServlet extends HttpServlet{
public void service(HttpServletRequest req,HttpServletResponse res)throws
ServletException,IOException{
res.setContentType("text/html");
printWriter out=res.getWriter();
out.println("<html>");
out.println("<head><title>simgle page</head></title>");
out.println("<body>");
out.println("<h1><center>Hello World</center></h1>");
out.println("</body>");
out.println("</html>");
}
那么如何声明和配置Servlets
(1) Servlet 是在web.xml部署描述文件中注册和配置的声明和映射一个servlet的示例:
[java]view plaincopyprint?
1. <web-app>
2. <servlet>
3. <servlet-name>myservlet</servlet_name>
4. <servlet-class>myPackage.MyServlet</servlet-class>
5. </servlet>
6. <servlet-mapping>
7. <servlet-name>myservlet</servlet_name>
8. <url-pattern>servlet</url-pattern>
9. </servlet-mapping>
10. </web-app>
<web-app> <servlet>
<servlet-name>myservlet</servlet_name>
<servlet-class>myPackage.MyServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>myservlet</servlet_name>
<url-pattern>servlet</url-pattern>
</servlet-mapping>
</web-app>
这个servlet能通过使用下面的URL调用他
http://www.bea.com:7001/servlet1
如果MyWebApplication不是默认的web应用:
http://www.bea.com:7001/MyWebApplication/servlet1
那么servlet的生命周期与接口我们就介绍到这.