In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article is to share with you about the basic methods of Java Servlet. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
The Java Servlet development tool (JSDK) provides several software packages that are needed when writing Servlet. This includes two basic packages for all Servlet: javax.servlet and javax.servlet.http.
HTTP Servlet uses an HTML table to send and receive data. To create a HTTP Servlet, extend the HttpServlet class, which is a subclass of GenericServlet that uses specialized methods to handle HTML tables. The HTML form is made up of
And
Defined by the tag. The form typically contains input fields, such as text input fields, check boxes, radio buttons, and select lists, and buttons for submitting data. When submitting information, they also specify which Servlet (or other program) the server should execute. The HttpServlet class contains methods like init (), destroy (), service (), and so on. Where the init () and destroy () methods are inherited.
(1) init () method
The init () method is executed only once during the lifetime of the Servlet. It is executed when the server loads the Servlet. You can configure the server to load the Servlet when you start the server or client to access Servlet for the first time. No matter how many clients access Servlet, init () is not repeated.
The default init () method usually meets the requirements, but it can also be overridden with a custom init () method, typically managing server-side resources. For example, you might write a custom init () to load GIF images only once, improving the performance of Servlet returning GIF images and containing multiple client requests. Another example is initializing a database connection. The default init () method sets the initialization parameters of Servlet and starts the configuration with its ServletConfig object parameters, so all Servlet that override the init () method should call super.init () to ensure that these tasks are still performed. Before calling the service () method, you should make sure that the init () method is completed.
(2) service () method
The service () method is the core of Servlet. Whenever a customer requests a HttpServlet object, the object's service () method is called and passed to the method a ServletRequest object and a ServletResponse object as parameters. The service () method already exists in HttpServlet. The default service function is to invoke the do function corresponding to the method requested by HTTP. For example, if the HTTP request method is GET, doGet () is called by default. Servlet should override the do functionality for the HTTP methods supported by Servlet. Because the HttpServlet.service () method checks to see if the request method calls the appropriate handling method, it is not necessary to override the service () method. Just override the corresponding do method.
The doPost () method is called when a customer makes a HTTP POST request through the HTML form. The parameters related to the POST request are sent from the browser to the server as a separate HTTP request. When you need to modify server-side data, you should use the doPost () method.
The doGet () method is called when a customer makes an HTTP GET request through the HTML form or requests a URL directly. Parameters related to the GET request are added to the end of the URL and sent with this request. The doGet () method should be used when the server-side data will not be modified.
Servlet responses can be of the following types:
An output stream that the browser interprets according to its content type, such as text/HTML.
A HTTP error response, redirected to another URL, servlet, JSP.
(3) destroy () method
The destroy () method is executed only once, that is, when the server stops and unmounts the Servlet. Typically, Servlet is shut down as part of the server process. The default destroy () method usually meets the requirements, but it can also be overridden, typically managing server-side resources. For example, if Servlet accumulates statistics at run time, you can write a destroy () method that saves statistics in a file when Servlet is not loaded. Another example is to close a database connection.
When the server unmounts the Servlet, the destroy () method is called after all service () method calls are completed, or after the specified time interval. A Servlet may generate other threads when running the service () method, so make sure that these threads are terminated or completed when the destroy () method is called.
(4) GetServletConfig () method
The GetServletConfig () method returns a ServletConfig object, which is used to return initialization parameters and ServletContext. The ServletContext interface provides environmental information about servlet.
(5) GetServletInfo () method
The GetServletInfo () method is an optional method that provides information about servlet, such as author, version, and copyright.
When the server calls the Service (), doGet (), and doPost () methods of sevlet, the request and response objects are required as parameters. The request object provides information about the request, while the response object provides a communication way to return the response information to the browser. The related classes in the javax.servlet package are ServletResponse and ServletRequest, while those in the javax.servlet.http package are HttpServletRequest and HttpServletResponse.
Servlet communicates with the server through these objects and eventually with the client. Servlet can learn about the client environment, the server environment, and all the information provided by the client by calling the method of the request object. Servlet can call the method of the response object to send a response that is ready to be sent back to the client.
Thank you for reading! This is the end of this article on "what are the basic methods of Java Servlet?". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.