Get the App
SLTechnology News&Howtos  ›  Development  › 

How to understand the life cycle of J2EE and Servlet

Shulou Source: shulou.com Published: 2022-06-02 12:07:37 09月17日 Update

This article introduces you how to understand J2EE and the life cycle of Servlet, the content is very detailed, interested friends can refer to, hope to be helpful to you.

J2EE is a standard middleware architecture, which aims to simplify and standardize the development and deployment of distributed multi-tier enterprise application systems.

Typical applications of J2EE structure are: client layer, presentation logic layer, business logic layer, enterprise information system layer.

Servlet is a server-side Java application that is independent of the operating system platform and network transport protocol. It is used to expand the function of the server and can generate dynamic web pages.

Life cycle of Servlet

The life cycle of a Servlet begins when it is loaded into the memory of the Web server and ends when the Servlet is terminated or reloaded.

(1) initialize

Load Servlet at the following times:

If the auto-mount option is configured, it is automatically loaded when the server is started

When the client sends a request to Servlet after the server starts

After reloading the Servlet when reloading Servlet, the server creates an instance of Servlet and calls the init () method of Servlet. During the initialization phase, the Servlet initialization parameters are passed to the Servlet configuration object.

(2) request processing

For client requests that arrive at the server, the server creates a request-specific "request" object and a "response" object. The server calls the service () method of Servlet, which is used to pass the request and response objects. The service () method takes the request information from the request object, processes the request, and sends the response back to the client using the method of the response object. The service () method can call other methods to handle the request, such as doGet (), doPost (), or other methods.

(3) termination

When the server no longer needs Servlet or remounts a new instance of Servlet, the server calls the destroy () method of Servlet.

A sample servlet (ServletSample.java) is as follows:

Import java.io.*; import java.util.*; import javax.servlet.*; import javax.servlet.http.*; public class ServletSample extends HttpServlet {/ / * * step: extend the HttpServlet abstract class. Public void doGet (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {/ / second step: rewrite the doGet () method String myName = ""; / / step 3: get HTTP request information java.util.Enumeration keys = request.getParameterNames (); while (keys.hasMoreElements ()); {key = (String) keys.nextElement (); if (key.equalsIgnoreCase ("myName")) myName = request.getParameter (key);} if (myName = ") myName =" Hello " / / step 4: generate HTTP response. Response.setContentType ("text/html"); response.setHeader ("Pragma", "No-cache"); response.setDateHeader ("Expires", 0); response.setHeader ("Cache-Control", "no-cache"); out.println (""); out.println (""); out.println ("Just a basic servlet"); out.println ("+ myName +", this is a very basic servlet that writes an HTML page. ") Out.println ("For instructions on running those samples on your WebSphere Application Server," + "open the page:"); out.println ("http://your.server.name/IBMWebAs/samples/index.aspl"); out.println ("where your.server.name is the hostname of your WebSphere Application Server."); out.println (""); out.flush ();}}

The format of Cookie is actually a piece of plain text message, which is sent by the server to the client along with the web page and saved in the directory specified on the client's hard disk.

A simple example.

1. Write Cookie-writecookie.jsp

two。 Read Cookie.jsp-readcookie.jsp

The lifecycle of Servlet is described above.

On how to understand J2EE and the life cycle of Servlet to share here, I hope that the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.

Tags: Server service method client object cycle life application information client system processing enterprise content instance client application more program structure Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Linux Shulou Technology Microsoft Huawei MySQL