Order:
It is well known that the basis of JSP is servlet, if you simply use the servlet class to respond to the user's HTTP request can it? The answer is yes. The 9 built-in objects in the JSP are only automatically initialized for us, without them being able to implement the Web. It's just a matter of tedious workload.
4.1. The first servlet:
First: You need to create a new subclass that inherits from HttpServlet. and rewrite the public void service (ServletRequest request, Servletresponse Response) method. This method is used to receive all manner of HTTP requests, and Doget and Dopost are the GET requests and post requests of the receiving user respectively.
Second: Add annotations to this class, using the specified request to process that URL.
Finally: Response responds to the HTML script to the user.
1 PackageServlets;2 3 Importjava.io.IOException;4 ImportJava.io.PrintStream;5 6 Importjavax.servlet.ServletException;7 Importjavax.servlet.ServletRequest;8 ImportJavax.servlet.ServletResponse;9 ImportJavax.servlet.annotation.WebServlet;Ten ImportJavax.servlet.http.HttpServlet; One A //The urlpatterns in this note specifies the URL to process the request -@WebServlet (name = "Firstservlet", Urlpatterns = "/firstservlet") - Public classFirstservletextendsHttpServlet { the - @Override - Public voidService (ServletRequest request, servletresponse response)throwsservletexception, IOException { - //TODO auto-generated Method Stub +Request.setcharacterencoding ("Utf-8"); -Response.setcontenttype ("Text/html;charset=utf-8"); + APrintStream out =NewPrintStream (Response.getoutputstream ()); atOut.println ("); -Out.println ("<body>"); -Out.println ("); -Out.println ("</body>); - } -}
Note: There are no built-in objects in the servlet, and the built-in objects in the original JSP must be created by the program display. For static HTML tags, the servlet must use the page output stream to output line by row.
4.2, the configuration of the servlet:
In order for the servlet to respond to user requests, the servlet must also be configured in a Web App. When configuring a servlet, you need to modify the Web. xml file.
Starting with Servlet3.0, there are two ways to configure a servlet.
(1) Configure using @webservlet annotations in the Servlet class.
(2) by configuring in the Web. xml file.
When using @webservlet, you can pin the following common properties:
Property must indicate whether the
Asyncsupported whether the servlet supports one-step operation mode.
DisplayName whether the servlet's display name is pinned
InitParams No to configure parameters for the servlet
Loadonstartup no used to configure the servlet as a load-on-startup servlet
Name No to highlight the servlet
Urlpatterns/value the two properties work exactly the same. The URL that the servlet handles is developed
Note: If you plan to use annotations to configure a servlet, first do not metadata-complete= "true" in the root element (Web-app) of the Web. xml file. Next, do not configure the Servblet in the Web. xml file. If you plan to use the Web. xml file to configure the servlet, you need to configure the following two sections.
(1) Configure the servlet name: corresponds to the <servlet/> element in the Web. xml file
(2) Configuring the servlet configuration URL: Corresponds to the <servlet-mapping/> element in the Web. xml file. This step is optional. However, if the URL is not configured for the servlet, the servlet cannot respond to user requests.
4.3. Load-on-startup Servlet:
There are two opportunities for creating a servlet instance: when the user requests it or when the app starts. A servlet is created on the app launch, typically a servlet for some background service, or a servlet that needs to intercept many requests, which is often used as the underlying servlet for the application, providing back-office services in the background.
There are two ways to configure a servlet for Load-on-startup.
(1) configuration in the Web. xml file through the <servlet/> element's <load-on-startup/> child elements.
(2) specified by the Loadonstartup attribute of the @webservlet annotation.
Note the:<load-on-startup/> element or Loadonstartup property receives only one integer value, and the smaller the integer value, the more precedence the servlet instantiates.
1 PackageServlets;2 3 Importjava.awt.event.ActionEvent;4 ImportJava.awt.event.ActionListener;5 Importjava.util.Date;6 7 ImportJavax.servlet.ServletConfig;8 Importjavax.servlet.ServletException;9 ImportJavax.servlet.annotation.WebServlet;Ten ImportJavax.servlet.http.HttpServlet; One ImportJavax.swing.Timer; A -@WebServlet (Loadonstartup = 1, name = "Timerservlet") - Public classServiceservletextendsHttpServlet { the - @Override - Public voidInit (servletconfig config)throwsservletexception { - //TODO auto-generated Method Stub + Super. init (config); - /*Timer*/ +Timer T =NewTimer (1000,NewActionListener () { A at @Override - Public voidactionperformed (ActionEvent e) { - //TODO auto-generated Method Stub -System.out.println (NewDate ()); - } - in }); - T.start (); to } + -}
1 <servlet>2 <Servlet-name>Timerservlet</Servlet-name>3 <Servlet-class>Servlets.serviceservlet</Servlet-class>4 <Load-on-startup>1</Load-on-startup>5 </servlet>
Note: For unknown reasons the servlet does not start after adding the Loadonstartup attribute on the annotation, so the servlet configuration information on the Web. XML is added as above. You can see that when Tomcat boots the console will output the current time every 1s.
Tail:
The servlet can also access the configuration file, which is similar to the Config object and is no longer written. Come here today, next time you record the JSP custom label.
"JSP" JSP Basic Learning Record (iv)--servlet