J2EE 13 specification (3)-Java Servlet

Source: Internet
Author: User

J2EE 13 specification (3)-Java Servlet

Servlet Introduction:

A servlet is a class in the Java programming language. It is used to expand the server's performance. The server resides in an application that can be accessed through the "request-response" programming model. Although the servlet can respond to any type of requests, it is generally only used to expand the Web server applications. 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 compiling servlets. All servlets must implement the servlet interface, which defines the lifecycle method.
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 process HTTP-specific services.

Servlet Workflow

After the Web Container (the container here uses the TomCat server) loads and instantiates the servlet class, the TomCat container initializes the servlet before the servlet instance passes the request from the client. You 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 execute any other one-time activities. Servlet must use UnavailableException to complete the initialization process. See the Java code: <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHA + PC9wPgo8cHJlIGNsYXNzPQ = "brush: java;"> import java. text. *; import java. util. *; import java. io. *; import javax. servlet. http. *; import javax. servlet. *; public 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 resolution:

HTML code:

Student ManagementQuery by birthdate segment
Web. xml

 
     
  
   
    StudentMgrServlet
   
   
    StudentServlet
   
  
  
   
    StudentMgrServlet
   
   
    /queryStudentServlet
   
  
 
Servlet Lifecycle

The entire process of life, load the Servlet and instantiate it, initialize init, process the request, and exit the service.

Note: The Servlet is instantiated only once, and the init method is executed only once. Servlet is NOT thread-safe.

Finally, let's talk about Cookies and sessions: Cookies are stored on the client, but the Session is stored on the server.

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.