In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
Editor to share with you the life cycle of the Servlet method and the implementation of the principle of example analysis, I believe that most people do not know much about it, so share this article for your reference, I hope you will learn a lot after reading this article, let's go to understand it!
Create a JavaEE project
Define a class that implements the Servlet interface
The public class ServletDemo1 implements Servlet {/ * initialization method * is executed when the Servlet is created. * @ param servletConfig * @ throws ServletException * * / @ Override public void init (ServletConfig servletConfig) throws ServletException {System.out.println ("init.");} / * * get the ServletConfig object * ServletCongig:Servlet configuration object * @ return * * / @ Override public ServletConfig getServletConfig () {return null } / * * provide service method * each time the Servlet is accessed, it is executed. You can execute * @ param servletRequest * @ param servletResponse * @ throws ServletException * @ throws IOException * * / @ Override public void service (ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException {System.out.println ("serve Pei Meizi!") multiple times. ;} / * get some details of Servlet, version, author, etc. * @ return * * / @ Override public String getServletInfo () {return null;} / * * destroy method * execute when the server shuts down normally. Execute * * / @ Override public void destroy () {System.out.println ("the service is down...");}} to implement the abstract method in the interface
Configure Servlet
Configure in web.xml
Demo1 xppmzz.ServletDemo1 demo1 / demo1 execution
Click the launch button of Tomcat in IDEA and type http://localhost:8080/demo1 in the browser.
Observe the output of the IDEA output window.
Executive principle
When the server receives the request from the client browser, it parses the request URL path to get the resource path of the accessed Servlet.
Find the web.xml file to see if there is a corresponding tag body content.
If so, find the corresponding full class name.
Tomcat loads the bytecode file into memory and creates its objects.
Call its method.
Life cycle method in Servlet 1. Created: executes the init method, only once
When will Servlet be created?
By default, the Servlet is created the first time it is accessed.
You can configure the creation time to execute the Servlet.
Configure under the label:
The first time it is accessed, the value created is negative.
When the server starts, the value created is 0 or a positive integer.
Demo1 xppmzz.ServletDemo1 5
If it is negative, the init method will not be executed until you enter http://localhost:8080/demo1 for the first time in the browser. 0 or a positive integer executes the init method the first time Tomcat starts.
The init method of Servlet, executed only once, indicates that a Servlet has only one object in memory, and that Servlet is a singleton.
Therefore, there may be thread safety problems when multiple users access at the same time.
Solution: try not to define member variables in Servlet. Even if a member variable is defined, do not modify the value
two。 Provide services: execute the service method, multiple times
The Service method is called every time the Servlet is accessed.
3. Destroyed: execute the destroy method, only once
Executes when the Servlet is destroyed. When the server shuts down, the Servlet is destroyed.
The destroy method is executed only if the server shuts down normally.
The destroy method is executed before the Servlet is destroyed and is typically used to release resources.
Servlet3.0
Benefits: support for annotated configuration, without the need for web.xml.
Steps:
To create a JavaEE project, choose a version of Servlet above 3.0. you can avoid creating a web.xml.
Define a class that implements the Servlet interface.
The method of copying.
Use the @ WebServlet annotation on the class to configure. @ WebServlet (Resource path)
Code example:
@ Target ({ElementType.TYPE}) @ Retention (RetentionPolicy.RUNTIME) @ Documented public @ interface WebServlet {String name () default "; / / equivalent to String [] value () default {} / configure String [] urlPatterns () default {} on behalf of the urlPatterns () attribute; / / equivalent to int loadOnStartup () default-1 / equivalent to WebInitParam [] initParams () default {} Boolean asyncSupported () default false; String smallIcon () default ""; String largeIcon () default "" String description () default ""; String displayName () default "";} Servlet architecture
Servlet-Interfac
GenericServlet-abstract class
HttpServlet-abstract class
GenericServlet
The other methods in the Servlet interface are implemented by default null, and only the service () method is abstracted.
When you define a Servlet class in the future, you can inherit GenericServlet and implement the service () method
@ WebServlet ("/ demo2") public class ServletDemo2 extends GenericServlet {@ Override public void service (ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException {System.out.println ("GenericServlet....");}} HttpServlet
An encapsulation of the http protocol that simplifies operation.
Defines that the class inherits from HttpServlet.
Override the doGet/doPost method.
@ WebServlet ("/ demo3") public class ServletDemo3 extends HttpServlet {@ Override protected void doGet (HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {System.out.println ("doGet.");} @ Override protected void doPost (HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {System.out.println ("doPost.");}} Servlet related configuration
Urlpartten:Servlet access path.
A Servlet can define multiple access paths: @ WebServlet ({"/ d4", "/ dd4", "/ ddd4"}) path definition rule: / xxx: path matching. / xxx/xxx: multi-tier path, directory structure. * .do: extension match.
These are all the contents of the article "sample Analysis of the Life cycle and execution principles of Servlet methods". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
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.