在Java Web的世界里,Tomcat 等Web Server被称作servlet容器。这一称谓的由来就是因为,Tomcat 实际上运行JSP 页面和Servlet。servlet实际上包含了诸多的业务逻辑,后来经过分层思想的演化,业务逻辑会被独立成dao,service等层,但在servlet中还是会调用service层的逻辑。这时候servlet实际上是页面和逻辑层的桥梁,差不多是MVC中的控制器(C)的意思。
正是因为这种原因,structs2这个很好的MVC框架才诞生出来,这时候通过Action,ActionSupport,等接口屏蔽了servlet的doGet,doPost等方法。为什么需要屏蔽这些方法呢?我们分别看看doGet和doPost里面做了些什么。
public void doGet(ServletRequest rep, ServletResponse req)
throws ServletException, IOException {
System.out.println("doGet");
req.setContentType("text/html");
PrintWriter out = req.getWriter();
out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
out.println("<HTML>");
out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");
out.println(" <BODY>");
out.print(" This is ");
out.print(this.getClass());
out.println(", using the GET method");
out.println(" </BODY>");
out.println("</HTML>");
out.flush();
out.close();
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
System.out.println("doPost");
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
out.println("<HTML>");
out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");
out.println(" <BODY>");
out.print(" This is ");
out.print(this.getClass());
out.println(", using the POST method");
out.println(" </BODY>");
out.println("</HTML>");
out.flush();
out.close();
}
是的,您没有看错,确实在拼html标签。动态的部分来自业务逻辑代码返回的,而静态部分需要在这里拼接字符串,效率很低,而且很容易出错。
下面我们来看看实现servlet的两种方式:实现Servlet接口和继承httpServlet
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.Servlet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class MyServlet2 implements Servlet{
public MyServlet2(){
System.out.println("MyServlet2");
}
public void destroy() {
// TODO Auto-generated method stub
System.out.println("destroy");
}
public ServletConfig getServletConfig() {
// TODO Auto-generated method stub
System.out.println("ServletConfig");
return null;
}
public String getServletInfo() {
System.out.println("getServletInfo");
// TODO Auto-generated method stub
return null;
}
public void init(ServletConfig arg0) throws ServletException {
// TODO Auto-generated method stub
System.out.println("init");
}
public void service(ServletRequest rep, ServletResponse req)
throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(rep,req);
System.out.println("service");
}
public void doGet(ServletRequest rep, ServletResponse req)
throws ServletException, IOException {
System.out.println("doGet");
req.setContentType("text/html");
PrintWriter out = req.getWriter();
out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
out.println("<HTML>");
out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");
out.println(" <BODY>");
out.print(" This is ");
out.print(this.getClass());
out.println(", using the GET method");
out.println(" </BODY>");
out.println("</HTML>");
out.flush();
out.close();
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
System.out.println("doPost");
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
out.println("<HTML>");
out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");
out.println(" <BODY>");
out.print(" This is ");
out.print(this.getClass());
out.println(", using the POST method");
out.println(" </BODY>");
out.println("</HTML>");
out.flush();
out.close();
}
}
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 MyServlet extends HttpServlet {
/**
* Constructor of the object.
*/
public MyServlet() {
super();
System.out.println("constructor.");
}
/**
* Destruction of the servlet. <br>
*/
public void destroy() {
System.out.println("destroy");
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}
/**
* 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 {
System.out.println("doGet");
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
out.println("<HTML>");
out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");
out.println(" <BODY>");
out.print(" This is ");
out.print(this.getClass());
out.println(", using the GET method");
out.println(" </BODY>");
out.println("</HTML>");
out.flush();
out.close();
}
/**
* 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 {
System.out.println("doPost");
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
out.println("<HTML>");
out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");
out.println(" <BODY>");
out.print(" This is ");
out.print(this.getClass());
out.println(", using the POST method");
out.println(" </BODY>");
out.println("</HTML>");
out.flush();
out.close();
}
/**
* Initialization of the servlet. <br>
*
* @throws ServletException if an error occurs
*/
public void init() throws ServletException {
// Put your code here
System.out.println("init");
}
}
这两个servlet算是最简单的两个servlet了,因为只包含doGet和doPost方法,实际开发过程中,肯定比这个复杂,但是大量地拼接字符串不是好的做法。
我们实际使用structs的Action,实现Action接口或者继承ActionSupport
package com.wicresoft.action;
import com.opensymphony.xwork2.Action;
public class IndexAction2 implements Action {
public String execute() throws Exception {
System.out.println("IndexAction2 executed.");
// TODO Auto-generated method stub
return SUCCESS;
}
}
package com.wicresoft.action;
import com.opensymphony.xwork2.ActionSupport;
public class IndexAction3 extends ActionSupport{
@Override
public String execute() throws Exception {
System.out.println("IndexAction3 executed.");
// TODO Auto-generated method stub
//return super.execute();
return "success";
}
public String UserAdd(){
return SUCCESS;
}
public String Add(){
return SUCCESS;
}
}
值得注意的是,这两种方式和实现servlet的两种方式遥相呼应。
Servlet生命周期分为三个阶段:
1,初始化阶段 调用init()方法
2,响应客户请求阶段 调用service()方法
3,终止阶段 调用destroy()方法
Servlet被装载后,Servlet容器创建一个Servlet实例并且调用Servlet的init()方法进行初始化。在Servlet的整个生命周期内,init()方法只被调用一次。
在servlet的实例被创建后,如果有请求过来,servlet容器会开新的线程来处理请求,而不再创建新的servlet的实例,也是因此,init()方法只会被调用一次!