Java EE 13 specification (3)-java Servlet

Source: Internet
Author: User

Introduction to Servlets :

A servlet is a class in the Java programming language that is used to extend the performance of the server, which resides in applications that can be accessed through the request-response programming model. Although the servlet can respond to any type of request, it is usually used only to extend the application of the Web server. Java servlet Technology defines an HTTP-specific servlet class for these applications.
The Javax.servlet and javax.servlet.http packages provide interfaces and classes for writing Servlets. All servlets must implement the Servlet interface, which defines the life cycle approach.
When implementing a common service, you can use or extend the Genericservlet class provided by the Java Servlet API. The HttpServlet class provides methods, such as Doget and dopost, to handle HTTP-specific services.

servlet Work Flow

theTomcat container initializes the servlet after the servlet class is loaded and instantiated in the Web container (where the container we are using is a Tomcat server), and the Servlet instance passes the request from the client. Users can customize this initialization process to allow the servlet to read persistent configuration data, initialize resources, and ignore the Init method of the Servlet interface to perform any other one-time activities. The servlet must use Unavailableexception to complete the initialization process. Take a look at the Java code:

Import java.text.*;import java.util.*;import java.io.*;import javax.servlet.http.*;import javax.servlet.*;p ublic Class Studentservlet extends Httpservlet{public void doget (httpservletrequest request,httpservletresponse response) Throws Servletexception,ioexception{dopost (Request,response);} public void DoPost (HttpServletRequest request,httpservletresponse response) throws servletexception,ioexception{    Doget (Request,response);      String sbegindate = Request.getparameter ("Begindate");                  String senddate = Request.getparameter ("EndDate");      Date begindate = new Date ();      Date endDate = new Date ();        try{begindate = new SimpleDateFormat ("Yyyy-mm-dd"). Parse (sbegindate);      EndDate = new SimpleDateFormat ("Yyyy-mm-dd"). Parse (senddate);      }catch (Exception e) {e.printstacktrace ();        } System.out.println ("sbegindate=" + sbegindate); System.out.println ("senddate=" + Senddate);}}
Instance parsing:

HTML code:

Xml

<?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_3_0.xsd "  version=" 3.0 ">    <servlet><servlet-name >studentmgrservlet</servlet-name><servlet-class>studentservlet</servlet-class></ Servlet><servlet-mapping><servlet-name>studentmgrservlet</servlet-name><url-pattern >/queryStudentServlet</url-pattern></servlet-mapping></web-app>
servlet life cycle

The whole process of life, loading servlets and instantiating, initializing INIT, processing requests, exiting the service.

Note: The servlet is instantiated only once, and the Init method is only executed once, and the servlet is not thread-safe.

Finally, there is a note about cookies and session: The cookie is stored in its client, but the session is stored in the server.

Java EE 13 specification (3)-java Servlet

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.