Basic knowledge Review Chapter 1 servlet learning and understanding, Chapter 1 servlet
As for servlet, I believe that I have learned java, and I have recently sorted out the basic knowledge so that I can better understand ssm or ssh. Next I will start from
1: Servlet Interface
Servlet has five methods. The functions of each method are briefly described below.
1.1: public void init (ServletConfig config)
After the servlet is instantiated, the servlet container will call the init method to initialize the object. This method is mainly used to initialize the object before the request, such as calling the database connection and instantiating the object.
1.2: public void service (ServletRequest req, ServletResponse res)
This method is a core method. Our main operations are carried out in this method. After receiving a request from the client, we process the service and send the processed information to the client.
1.3: public void destroy ()
This method is called when the servlet instance is removed and resources in the servlet are released. It can be understood as a destructor to close the database connection.
1.4: public String getServletInfo ()
This method is mainly used to return the Servlet description information, which can be the Servlet author, version, copyright information, etc.
1.5: public ServletConfig getServletConfig ()
It mainly returns the ServletConfig object that is passed to the servlet object after the container calls init.
The functions of the five methods are finished. Now let's look at the code.
1 public class HelloWorldServlet implements Servlet {2 3 private ServletConfig servletConfig; 4 // private ServletInfo 5 public void init (ServletConfig config) throws ServletException {6 this. servletConfig = config; 7} 8 9 public ServletConfig getServletConfig () {10 return servletConfig; 11} 12 13 public void service (ServletRequest req, ServletResponse res) throws ServletException, IOException {14 String initReleName = this. servletConfig. getInitParameter ("realName"); 15 String servletName = this. servletConfig. getServletName (); 16 String username = req. getParameter ("username"); 17 18 res. setContentType ("text/html; charset = gb2312"); // avoid Chinese garbled characters 19 // get the PrintWriter object, 20 PrintWriter out = res. getWriter (); 21 // send data 22 out to the client. println ("Hello World" + username + "<br/>"); 23 out. println ("servlet name:" + servletName + "<br/>"); 24 out. println ("inline real name:" + initReleName + "<br/>"); 25 // close the stream 26 out. close (); 27} 28 29/** 30 * is used to return the Servlet description information, which can be the Servlet author, version, copyright information, etc. 31 */32 public String getServletInfo () {33 String str = "zhangsan"; 34 return str; 35} 36 37 public void destroy () {38 // TODO Auto-generated method stub39} 40}View Code
We are configuring a web. xml
1 <! DOCTYPE web-app PUBLIC 2 "-// Sun Microsystems, Inc. // DTD Web Application 2.3 // EN "3" http://java.sun.com/dtd/web-app_2_3.dtd "> 4 <web-app> 5 <display-name> myservlet </display-name> 6 <description> This Is My the first servlet </description> 7 <servlet> 8 <servlet-name> HelloWorldServlet </servlet-name> 9 <servlet-class> com. lp. servlet. helloWorldServlet </servlet-class> 10 <init-param> 11 <param-name> realName </param-name> 12 <param-value> Zhang San </param-value> 13 </init-param> 14 </servlet> 15 <servlet-mapping> 16 <servlet-name> HelloWorldServlet </servlet-name> 17 <url-pattern>/helloworld </ url-pattern> 18 </servlet-mapping> 19 </web-app>Web. xml
NOTE 1: The first Servlet in xml indicates the Servlet-name. to associate with the servlet-name in Servlet-mapping, then, you can find the required Servlet class during the shooting.
In the browser, enter http: // localhost: 8080/servlet-test/helloworld? Username = lp and then we can see the effect
2: Servlet Lifecycle
I use a picture to represent