Reference book: "Java EE open source programming Essentials 15"
A servlet is a Java class that can handle an HTTP request from a client and return a response, executed by a server-side call, with some specification written.
For example:
1 Packagetest;2 3 Importjava.io.IOException;4 ImportJava.io.PrintWriter;5 6 Importjavax.servlet.ServletException;7 ImportJavax.servlet.http.HttpServlet;8 Importjavax.servlet.http.HttpServletRequest;9 ImportJavax.servlet.http.HttpServletResponse;Ten One Public classTestextendsHttpServlet { A - PublicTest () { - Super(); the } - - Public voiddestroy () { - Super. Destroy ();//Just puts "destroy" string in log + //Put Your code here - } + A //handling HTTP GET requests at Public voiddoget (httpservletrequest request, httpservletresponse response) - throwsservletexception, IOException { - //The Web server is configured to respond in the form of HTML -Response.setcontenttype ("text/html"); - //get the PrintWriter object to print out the response -PrintWriter out =Response.getwriter (); in //The HTML response is displayed on the browser. -Out.println ("<! DOCTYPE HTML public \ "-//W3C//DTD HTML 4.01 transitional//en\" > "); toOut.println ("<HTML>"); +Out.println ("); -Out.println ("<BODY>"); theOut.print ("This is"); *Out.print ( This. GetClass ()); $Out.println (", using the GET method");Panax NotoginsengOut.println ("</BODY>"); -Out.println ("</HTML>"); the Out.flush (); + out.close (); A } the //handling HTTP POST requests + Public voidDoPost (httpservletrequest request, httpservletresponse response) - throwsservletexception, IOException { $ $Response.setcontenttype ("text/html"); -PrintWriter out =Response.getwriter (); -Out.println ("<! DOCTYPE HTML public \ "-//W3C//DTD HTML 4.01 transitional//en\" > "); theOut.println ("<HTML>"); -Out.println (");WuyiOut.println ("<BODY>"); theOut.print ("This is"); -Out.print ( This. GetClass ()); WuOut.println (", using the POST method"); -Out.println ("</BODY>"); AboutOut.println ("</HTML>"); $ Out.flush (); - out.close (); - } - A Public voidInit ()throwsservletexception { + //Put Your code here the } -}
View Code
Servlets need to be deployed in a web container, and the lifecycle is managed by the Web container and can be divided into the following phases:
- Load servlet: General dynamic execution
- Create a Servlet instance
- Calling the servlet's init () method
- Service: If the Web container accepts a request to this servlet, call the appropriate response method (configure the servlet via Web. xml)
- Destroy: Instance is destroyed, call Destroy ()
Main interface classes:
The HttpServletRequest interface provides a way to handle client requests, such as String getparameter (Note: name is the name of the form label), and data is obtained from the Client page window, such as obtaining a user name
1 httpservletrequest request; 2 String name=request.getparameter ("username");
View Code
The HttpServletResponse interface provides a way to send a request to a client as an HTML page, mainly setContentType (), etc., such as a request forwarded to the next page
1 httpservletresponse response; 2 response.sendredirect ("next.jsp");
View Code
The HttpSession interface records user conversations in the current servlet and can be used to store information about user conversations, access data using methods such as Putvalue () and GetValue () of the HttpSession interface. A HttpSession object can be generated by the GetSession () method of the Httpserveltrequest object, such as
1 httpservletrequest request; 2 HttpSession session=request.getsession (true);
View Code
The RequestDispatcher interface can submit or delegate a request for a servlet to another resource through the forward (HttpServletRequest request,httpservletresponse response) method. such as servlet, HTML, or JSP. The RequestDispatcher object can be generated by the Getrequestdispatcher () method of the HttpServletRequest object. Such as
1 requestdispatcher rd=request.getrequestdispathcer ("reply.jsp"); 2 Rd.forward (request,response);
View Code
Examples of integrated use:
1 Packagetest;2 3 Importjava.io.IOException;4 ImportJava.io.PrintWriter;5 6 ImportJavax.servlet.RequestDispatcher;7 Importjavax.servlet.ServletException;8 ImportJavax.servlet.http.HttpServlet;9 Importjavax.servlet.http.HttpServletRequest;Ten ImportJavax.servlet.http.HttpServletResponse; One Importjavax.servlet.http.HttpSession; A - Public classTestextendsHttpServlet { - the PublicTest () { - Super(); - } - + Public voiddestroy () { - Super. Destroy ();//Just puts "destroy" string in log + //Put Your code here A } at - //handling HTTP GET requests - Public voiddoget (httpservletrequest request, httpservletresponse response) - throwsservletexception, IOException { - DoPost (request,response); - } in //handling HTTP POST requests - Public voidDoPost (httpservletrequest request, httpservletresponse response) to throwsservletexception, IOException { +String Name=request.getparameter ("username"); - RequestDispatcher Rd; the * Try{ $ Try{Panax Notoginseng if(Name.equals ("Tom")){ -HttpSession session=request.getsession (); theSession.setattribute ("NM", name); +Response.sendredirect ("reply.jsp"); A } the Else{ +Rd=request.getrequestdispatcher ("error.jsp"); - Rd.forward (request, response); $ } $}Catch(servletexception e) { - e.printstacktrace (); - } the}Catch(IOException e) { - e.printstacktrace ();Wuyi } the } - Wu Public voidInit ()throwsservletexception { - //Put Your code here About } $}
View Code
When a servlet is deployed to a Web server, it needs to be declared in the Web configuration file XML, such as:
1<?xml version= "1.0" encoding= "UTF-8"?>2<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_3_0.xsd" id= "Webapp_ ID "version=" 3.0 ">3<display-name>Login_Proj</display-name>4<servlet>5<description>this is the description of my EE component</description>6<display-name>this is the display name of my EE component</display-name>7<servlet-name>LoginServlet</servlet-name>8<servlet-class>login. loginservlet</servlet-class>9</servlet>Ten One<servlet-mapping> A<servlet-name>LoginServlet</servlet-name> -<url-pattern>/LoginServlet</url-pattern> -</servlet-mapping> the<welcome-file-list> -<welcome-file>index.jsp</welcome-file> -</welcome-file-list> -</web-app>
View Code
Assuming the user form action= "Loginservlet", the form submission jumps to the/loginservlet,web.xml in the <servlet-mapping> tag <url-pattern>/ loginservlet</url-pattern> corresponding <servlet-name> for Loginservlet, so under the <servlet> tag to find <servlet-name > For the Loginservlet servlet, find the corresponding class login. Loginservlet.
Main components of Java Web programming technology--servlet