servlet的三種建立
阿新 • • 發佈:2018-12-22
servlet有三種手動建立的方式:
第一種:實現Servlet介面
瞭解一下Servlet生命週期
A、在容器中建立一個Servlet物件 (放到容器中,如果容器中存在,不建立)
B、初使化Servlet(呼叫init方法)
C、呼叫service方法,服務使用者的請求;
D、呼叫destroyed方法,銷燬物件;
package cn.lanqiao.servlet; import java.io.IOException; import java.util.Enumeration; import javax.servlet.Servlet; import javax.servlet.ServletConfig; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; public class Servlet6 implements Servlet { private ServletConfig config; // 1.建立servlet物件 public Servlet6() { System.out.println("1 servlet物件建立。。。"); } // 2.初始化servlet @Override public void init(ServletConfig conf) throws ServletException { // TODO Auto-generated method stub System.out.println("2,servlet初始化 了。。。。。。"); this.config = conf; } @Override public ServletConfig getServletConfig() { // TODO Auto-generated method stub return null; } @Override public String getServletInfo() { // TODO Auto-generated method stub return null; } @Override public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException { // System.out.println(res); // res.getWriter().write("呼叫service方法。。。"); res.getOutputStream().write("hello".getBytes()); System.out.println("3 service方法被呼叫,響應使用者請求"); // 通過servletconfig拿初始化引數 // String name = config.getInitParameter("uname"); // String upassword = config.getInitParameter("upassword"); // System.out.println(name); // System.out.println(upassword); Enumeration<String> names = config.getInitParameterNames(); while (names.hasMoreElements()) { String str = (String) names.nextElement(); System.out.println(str + "---" + config.getInitParameter(str)); } } @Override public void destroy() { // TODO Auto-generated method stub System.out.println("4 銷燬物件。。。。。"); } }
第二種:繼承GenericServlet,實現裡面的Service方法
package cn.lanqiao.servlet; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.GenericServlet; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; public class Servlet2 extends GenericServlet { @Override public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException { // TODO Auto-generated method stub PrintWriter pw = res.getWriter(); pw.write("hello,servlet2,,,,,,,"); } }
第三種:繼承HttpServlet,實現裡面的doGet方法和doPost方法,一般我們都是用這個建立Servlet物件
package cn.lanqiao.servlet; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class Servlet5 extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doPost(req, resp); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { resp.getWriter().write("hello,servlet5..."); } }
以上三種手動建立Servlet物件的方法在釋出的時候需要去配置web.xml檔案
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>servletproject</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>Servlet1</servlet-name>
<servlet-class>cn.lanqiao.servlet.Servlet1</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Servlet1</servlet-name>
<url-pattern>/Servlet1</url-pattern>
</servlet-mapping>
</web-app>
但是我們使用直接建立一個Servlet的時候,會自動幫我們在web.xml中配置,對於新手還是使用第三種手動建立物件的方法好,這種直接建立的方法在以後的工作中使用可以提高效率。