Java servlet Learning Notes (i)

Source: Internet
Author: User

About Servlet

A servlet is a small program that runs on a web container. This program is implemented using the Java programming language. On servers with large traffic, the advantage of Servlets is that they execute faster than CGI programs, and individual user requests are activated as one thread in a single program without creating a separate process, which means that the overhead of server-side processing requests is significantly reduced.


Servlet life cycle

After the server receives the client's request:

The 1.web container determines whether the corresponding servlet class is loaded into memory and creates an instance of the servlet. If yes, then go to step 4th, or 2nd step;

2. Load and create an instance object of the Servlet class;

3. Invoke the Init () method of the Servlet instance object;

4. Create a HttpServletRequest object that encapsulates the HTTP request message and a HttpServletResponse object that represents the HTTP response, and then call the servlet's service () method and pass the two objects as parameters;

The Destroy () method of the servlet is called to destroy the instance object before the 5.web container is stopped or restarted.


All servlets need to inherit from the HttpServlet class and need to rewrite their doget and Dopost methods.

Package Servlet;import Java.io.ioexception;import Javax.servlet.servletexception;import Javax.servlet.annotation.webservlet;import Javax.servlet.http.httpservlet;import Javax.servlet.http.httpservletrequest;import javax.servlet.http.httpservletresponse;/** * servlet implementation Class cart */@WebServlet ("/cart") public class Cart extends HttpServlet {private static final long Serialversionuid = 1L;PR Ivate String action;/** * @see httpservlet#httpservlet () */public Cart () {super ();//TODO auto-generated constructor stub} /** * @see Httpservlet#doget (httpservletrequest request, HttpServletResponse * response) */protected void Doget (HttpS Ervletrequest request,httpservletresponse response) throws Servletexception, IOException {this.dopost (Request, Response);} /** * @see Httpservlet#dopost (httpservletrequest request, HttpServletResponse * response) */protected void DoPost (HTT Pservletrequest request,httpservletresponse response) throws Servletexception, IOException {if (request.getpArameter ("action") = null) {this.action = Request.getparameter ("action"), if (This.action.equals ("add")) { This.addtocart (request, response);} else if (this.action.equals ("Remove")) {This.removefromcart (request, response);} else if (this.action.equals ("list")) {This.showcart (request, Response);}}} protected void AddToCart (HttpServletRequest request,httpservletresponse response) {Integer itemId = Integer.parseint ( Request.getparameter ("ItemId"));} protected void Showcart (HttpServletRequest request,httpservletresponse response) {}protected void Removefromcart ( HttpServletRequest request,httpservletresponse response) {}}

Once you have written a servlet class, you will need to register it in the Web. xml file located in the Web-inf directory.

To register a servlet requires the following two code, the first is to register the Servlet class to the system, the second is to register the servlet's party path to the system, it is important to note that in the Url-pattern must start with "/", otherwise you will be prompted 404 error.

<servlet>    <servlet-name>servlet name</servlet-name>    <servlet-class>servlet class NAME INCLUDED Package name</servlet-class><servlet><servlet-mapping>    <servlet-name> SERVLET name</servlet-name>    <url-pattern>/access path</url-pattern></servlet-mapping >

The Ps:web-inf directory stores access to restricted files, which may cause the browser to be unable to read the contents of the file if the default index.jsp file or any other JSP file without access control is placed in the directory.



Java servlet Learning Notes (i)

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.