Java Study Notes-Servlet technology (11), learning notes servlet
If you want to develop a dynamic website, you must learn a dynamic web development technology. The JavaEE provided by SUN mainly includes two technologies for developing dynamic web pages: Servlet and JSP.
Servlet technology Overview
Servlet technology is one of the core components provided by SUN to develop dynamic web pages. You can easily develop dynamic web pages. The main language used is java. Developers only need to implement the corresponding interfaces or inherit the corresponding classes, so your java file is a dynamic web page. Of course, some additional configurations are required.
A Servlet is actually a java program running on the web server.
Servlet Architecture
SUN provides a complete set of interfaces and classes to help developers develop basic dynamic web pages.
1 Servlet Interface
2 GenericServlet class
GenericServlet implements the abstract class of the Servlet interface.
3 HttpServlet class
HttpServlet inherits the abstract class of GenericServlet.
Servlet experience
1. create a dynamic website directory structure 2. compile a dynamic web page as follows: HelloServlet. javapackage cn. itcast. servlets; import javax. servlet. *; import java. io. *; public class HelloServlet extends GenericServlet {public void service (ServletRequest req, ServletResponse res) throws ServletException, IOException {// create a data String data = "hello servlet! "; // Send the above data to the browser to display res. getOutputStream (). write (data. getBytes ());}}
3. compile the above HelloServlet. java introduces the jar package required by JavaEE to the classpath environment variable set classpath = % atat_home % \ lib \ servlet-api.jar to compile D: \ test> javac-d. helloServlet. java
4. Cut the compiled package together with the class file to the WEB-INF \ classes directory of the website 5. Map the class file to the URL path required by the browser
Modify the web. xml file as follows <? Xml version = "1.0" encoding = "ISO-8859-1"?> <Web-app xmlns = "http://java.sun.com/xml/ns/javaee" xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance" xsi: schemaLocation = "http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version = "2.5"> <servlet-name> helloservlet </servlet-name> <servlet-class> cn. itcast. servlets. helloServlet </servlet-class> </servlet> <servlet-mapping> <servlet-name> helloservlet </servlet-name> <url-pattern>/helloservlet </url-pattern> </servlet-mapping> </web-app>
6. Deploy the compiled website to the webapps directory of tomcat http: // localhost: 8080/test/helloservlet
Running result: hello servlet
Summary:
2 IDE experience
Servlet, GenericServlet, and HttpServlet
1 Servlet Interface
Servlet interfaces are mainly used to define methods for initializing Servlets, processing user requests, and removing servlets from web servers. If developers need to implement servlet interfaces, we recommend that you inherit from GenericServlet or HttpServlet.
1. the servlet is constructed, then initialized with the init method. constructor, init () 2. any callfrom clients to the service method are handled. service () 3. the servlet is taken out of service, then destroyed with the destroy method, then garbage collected and finalized.
Example 1: experience the lifecycle method.
Public class Demo2 extends GenericServlet {// create object public Demo2 () {System. out. println ("constructor");} // initialize public void init (ServletConfig config) throws ServletException {super. init (config); System. out. println ("init method");} // process the public void service (ServletRequest arg0, ServletResponse arg1) throws ServletException, IOException {System. out. println ("service method");} // remove the public void destroy () {super. destroy (); System. out. println ("destroy method ");}}
Summary:
1. servlet executes the following process: Create an object, initialize the object, process the user request, and destroy the object. object initialization is only performed once. execute destroy when the server is shut down. 4. only when you access the servlet for the first time, if the web server does not have the servlet object. (Lazy loading) 5. the servlet-defined lifecycle methods are all called by the web server (callback). 6. servlet is all Singleton 7. the service method used in actual development.
2 GenericServlet abstract class
This class is a common servlet class that implements the Servlet and ServletConfig interfaces. To implement the HTTTP protocol, inherit the HttpServlet class.
This class makes servlet definition easy and provides some log, ServletConfig, and version methods. A ServletContext interface class is declared internally.
By default, this class implements empty Servlet interface methods. However, for the init method, it obtains the passed ServletConfig class and assigns the value to its own defined ServletConfig member variable. Then, the inti () method is called.
Defines a unique abstract method service ().
3 HttpServlet abstract class
If a website needs to implement HTTP Servlet, it must be a subclass of HttpServlet. That is, at least one of the following methods must be rewritten as a subclass of HttpServlet:
DoGet, if the servlet supports http get requests processes the user's GET request doPost, for http post requests processes the user's POST request doPut, for http put requests processes the user's PUT request doDelete, for http delete requests process the user's DELETE request init and destroy, to manage resources that are held for the life of the servlet lifecycle method getServletInfo, which the servlet uses to provide information about itself get servlet information
We should mainly rewrite the doGet and doPost methods.
For example, HttpServlet is used to process user requests.
Public class Demo3 extends HttpServlet {public Demo3 () {System. out. println ("create object");} public void init (ServletConfig config) throws ServletException {// TODO Auto-generated method stub super. init (config); System. out. println ("initialization");} // process the user's get request. In the address bar, press enter directly. a and form default public void doGet (HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {System. out. println ("processing users' get requests");} // modify the method in the form to post public void doPost (HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {System. out. println ("processing users' POST requests ");}}
Thinking: HttpServlet inherits from GenericServlet to implement the Servlet interface. But if you use the doGet and doPost methods to process user requests, do you still need the originally defined service ()?
The service () defined in the Servlet interface that Tomcat must execute when processing user requests ()
However, if the requested Servlet inherits HttpServlet directly, the service () method of the Servlet interface is executed.
By default, this method calls Http service () defined in HttpServlet. In this method, the request is forwarded to the corresponding doGet or doPost () the final method for processing user requests is doGet or doPost ().
However, if the developer manually overrides the service method of the Servlet interface, the forwarding is not allowed by default.
Example 1: Read the running results of the following programs.
Public class Demo3 extends HttpServlet {public Demo3 () {System. out. println ("create object");} public void init (ServletConfig config) throws ServletException {// TODO Auto-generated method stub super. init (config); System. out. println ("initialization");} // process the user's get request. In the address bar, press enter directly. a and form default public void doGet (HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {System. out. println ("processing users' get requests");} // modify the method in the form to post public void doPost (HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {System. out. println ("processing users' POST requests") ;}@ Override public void service (ServletRequest req, ServletResponse res) throws ServletException, IOException {super. service (req, res); System. out. println ("service method") ;}@ Override public void service (HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {super. service (req, resp); System. out. println ("httpservlet service ");}}
The running result is as follows:
Create object
Initialization
Process users' POST requests
Httpservlet service
Service Method