Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Summary of knowledge points about Servlet

2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article introduces the relevant knowledge of "summary of knowledge points about Servlet". In the operation of actual cases, many people will encounter such a dilemma. Next, let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

1. Overview

In this short article, we will conceptually understand what servlet and servlet containers are and how they work. At the same time, they can be seen in the context of requests, responses, session objects, shared variables, and multithreading.

2. Servlets and its containers

Servlet is a common component of JEE for web development. They are basically Java programs that run within the boundaries of the container. In general, they are responsible for accepting requests, processing requests, and returning responses.

To use them, you first need the container to register servlet, and both JEE-based and Spring-based containers can receive them at startup. At the beginning, the container instantiates servlet by calling the init () method. After initialization, servlet can accept incoming requests. The container then directs these requests to the service method of the servlet for processing. It then further delegates the request to the appropriate method, such as doGet () or doPost (), depending on the HTTP request type.

With destroy (), the container destroys the servlet and no longer accepts incoming requests. We call this cycle of init-service-destroy the life cycle of servlet.

Now let's look at it from a container point of view, such as when Apache Tomcat or Jetty starts up, creating a ServletContext object, ServletContext's task is to act as the memory of the server or container, and remember all the servlet, filters, and listeners associated with the web application, such as their web.xml files or equivalent comments. ServletContext retains the container until it stops.

In any case, the load-on-startup parameter of servlet plays an important role. If the value of this parameter is greater than zero, the server initializes it only at startup. If this parameter is not specified, its init () is called when the request hits servlet for the first time.

3. Request, Response and Session

In the previous section, we discussed sending requests and receiving responses, which are basically the basis of any CS application. Now, let's take a closer look at them from the perspective of servlet.

In this case, the request will be represented by HttpServletRequest and the response will be represented by HttpServletResponse.

Whenever a request is sent by a browser or curl command, the container creates a new HttpServletRequest and HttpServletResponse object. These new objects are then passed to the service method of servlet. Based on the method property of HttpServletRequest, this method determines which doXXX method should be called.

In addition to information about methods, request objects carry other information, such as headers, parameters, and bodies. Similarly, HttpServletResponse objects also carry headers, parameters, and bodies-- we can set them in the doXXX method of servlet.

The lives of these objects are fleeting. When the client gets a response, the server marks the request and response objects for garbage collection. So how do we maintain a state between subsequent client requests or connections? The answer is HttpSession.

The principle is to bind these objects to a user session so that information related to a particular user can be persisted across multiple requests. This is usually done by using the concept of cookies, using [JSESSIONID] as the unique identifier for a given session. We can specify the timeout of the session in web.xml.

ten

The above configuration indicates that if the session is idle for 10 minutes, the server will discard it. Any subsequent requests will create a new session.

4. Servlets share data

Depending on the scope of your needs, servlet can share data in a variety of ways.

As mentioned in the previous section, different objects have different life cycles. The HttpServletRequest and HttpServletResponse objects exist only between one servlet call. HttpSession persists as long as it is active and does not time out.

ServletContext has the longest life cycle. It is born with Web applications and is destroyed only when the application itself is closed. Because servlet, filter, and listener instances are context-bound, they will always exist as long as the web application is up and running.

Therefore, if our requirement is to share data among all servlet, and suppose we want to calculate the number of visitors to the site, then we should put the variables in the ServletContext. If we need to share data in a session, we keep it within the scope of the session. In this case, the user name is an example.

Finally, there is a request scope related to the data of a single request, such as the request payload.

5. Dealing with multithreading

Multiple HttpServletRequest objects share servlet with each other so that each request operates using its own servlet instance thread.

In terms of thread safety, this actually means that we should not specify data within the scope of the request or session as an instance variable of servlet.

For example, the following code snippet:

Public class ExampleThree extends HttpServlet {private String instanceMessage; @ Override protected void doGet (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {String message = request.getParameter ("message"); instanceMessage = request.getParameter ("message"); request.setAttribute ("text", message); request.setAttribute ("unsafeText", instanceMessage); request.getRequestDispatcher ("/ jsp/ExampleThree.jsp") .forward (request, response) }}

In this case, all requests in the session share instanceMessage, and message is unique to a given request object. Therefore, in the case of concurrent requests, the data in instanceMessage may be inconsistent.

This is the end of the summary of knowledge points about Servlet. Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report