In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
What is the basis of servlet, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain for you in detail, people with this need can come to learn, I hope you can gain something.
What is servlet?
Servlet is a Mini Program running on the server, a servlet is a java class, and the servlet program that resides in the server memory can be accessed through the request-response programming model. As shown in the following figure:
For example, when a browser visits a project deployed in tomcat, there must be an access address since it is a visit, and when the things to be handled after the visit are finished, what should be returned to the browser should be given to the browser, so what is this address like? Or who handled the request? Here you need to use servlet, which is the handler that servlet is used to receive the browser's request and finally return the result.
Servlet is one of the three major components of javaWeb. The other two are Filter (interceptor) and Listener (listener).
Tomcat Container level
Tomcat containers are divided into four levels: container container-> engine container-> host (that is, host container)-> servlet container. The level is from large to small, that is, the front container contains the latter container, the servlet container manages the context container, and a context (context) corresponds to a web project.
The steps to write a simple servlet
Custom class, which can be implemented in three ways
One way is to write a class and then implement the Servlet interface (all abstract methods in that interface must be overridden)
The second way is to write a class that inherits the GenericServlet abstract class (the service method that overrides the life cycle). The GenericServlet abstract class implements the Servlet interface and the GenericConfig interface. GenericServlet is actually a general protocol-independent servlet defined.
The third way is to write a class to inherit the HttpServlet abstract class (there are no abstract methods, rewrite doGet or doPost methods according to the way the page is submitted), HttpServlet is based on the http protocol and is the most frequently used.
If you inherit the HttpServlet abstract class, you need to override the doGet () or doPost () method. Depending on which method is used to submit, the get request rewrites the doGet,post request and the doPost is rewritten (but it is important to note that these two submission methods are not the only ones, but there are also put,delele requests).
This is just to override the doGet or doPost method because even if the service method is overridden, there are called doGet or doPost methods in the service method, so there is no need to override the service method
As for why methods such as doPut or doDelete are not overridden, it is because doPost and doGet are the most commonly used, and using the doPost method can also implement the functions of doDelet and doPut requests, so it is not necessary.
Register servlet in web.xml, mainly configure servlet and servlet-mapping tags
ServletDemo servletPackage.ServletDemo ServletDemo / hello
The following url-pattern is actually the access path we configured. For example, if / hello is configured here, you need to access the http://ip: port / project name / hello, so that you can correspond to the url-pattern here, and then find the servlet-name in the matching servlet tag through the servlet-name in the servlet-mapping, so that you can find the servlet-class in the servlet tag (that is, the request processing class).
Create a servlet in eclipse
The first way is to directly new a servlet (the way established here is actually to implement the HttpServlet abstract class)
The second is to implement the Servlet interface when creating the class.
Add servl-api.jar package
If the servlet created using the first method shows that there is no guide package, or if there are no results in the search servlet in the second way, then no Servlet-api.jar package has been added. There are two steps to add:
Configure tomcat in eclipse and add it in window- > perference
The second step is to add tomcat to the runtime environment by right-clicking on the selected project, open Properties- > java Bulid path- > Libraries- > Add Libraries- > Server Runtime, and select the tomcat server.
Create a servlet in idea
Reference article
The life cycle of servlet from a methodological perspective
There are mainly three methods: init- > service- > destory
Init is called when servlet is accessed for the first time, indicating that an instance of servlt is created by default on first access. (but you can modify it yourself), the instance will only be created once. It shows that the example of servlet is a single case.
The service method is called whenever there is a request.
The destory method is called when the server is shut down
Create a servlet instance when tomcat starts
As mentioned above, by default, servlet instances are created on the first visit, that is, even if the tomcat server has been started, servlet instances will not be created as long as there is no request. However, this can be modified by yourself, and can be modified in web.xml:
ServletDemo servletPackage.ServletDemo 1
It should be noted that the value of the load-on-startup tag must be an integer greater than or equal to 0. All servlet with this tag will be created when tomcat starts. The value of this element is a sequence number, and tomcat will use this number to sort multiple servlet, and then create servlet instance objects in this order when tomcat starts.
The load-on-startup element marks whether the container loads the servlet at startup (instantiation and calls its init () method).
Its value must be an integer indicating the order in which servlet should be loaded
When the value is 0 or greater than 0, the container loads and initializes the servlet when the application starts
When the value is less than 0 or is not specified, the container will not be loaded until the servlet is selected.
The lower the value of a positive number, the higher the priority of the servlet, and the more the application is loaded when it starts.
When the values are the same, the container chooses its own order to load.
ServletConfig
When using HttpServlet, there are two init methods, one of which contains ServletConfig parameters. ServletConfig has four main methods, two of which are to obtain initialization parameters: getInitParameter and getInitParameterNames, respectively.
String getServletName (); ServletContext getServletContext (); String getInitParameter (String var1); Enumeration getInitParameterNames (); initialization parameters
As mentioned above, there are two methods in ServletConfig to get initialization parameters, so what are initialization parameters? In fact, it is the value configured in the init-param tag in the servlet tag:
ServletDemo servletPackage.ServletDemo username 1341234 password helloworldnas 1
In the above configuration file, there are two init-param configured in the servlet tag, which are the initialization parameters. (this is just an example. Initialization parameters are often used in the framework.)
Get initialization parameters @ Overridepublic void init (ServletConfig config) throws ServletException {String username = config.getInitParameter ("username"); String password = config.getInitParameter ("password"); System.out.println (username + "," + password); super.init (config);}
The above code is to get the configuration parameters through the servletConfig object in the init method.
Servlet path matching
There are three ways to match url-pattern paths:
Full path matching starts with / such as / aaa/ aaa/bbb
Directory matching begins with / such as / aaa/* / *
Extension matching cannot start with /, such as * .do, * .action
Priority: full path matching > directory matching > extension matching
ServletContext
When the web container starts, it creates a corresponding servletContext object for each web container application, which represents the current web application
Because all servlet in a web application share a servletContext object, servlet objects can communicate with each other through servletContext objects, which are usually called context domain objects.
Since servletContext is a global object, you can configure global parameters. You have previously configured init-param in the servlet tag, but the parameters are only useful for that servlet. If you want all servlet to be available, you need to configure them in servletContext:
Username 13412432 ServletDemo servletPackage.ServletDemo username 1341234 password helloworldnas 1
From the configuration file above, you can see that what is configured in context-param is the value in the global servletContext, and then obtained in the ServletContext object, which can be obtained from ServletConfig or directly from the getServletContext method of the parent class:
Public void init (ServletConfig config) throws ServletException {ServletContext servletContext = config.getServletContext (); String username = servletContext.getInitParameter ("username"); System.out.println (username); super.init (config);} is it helpful for you to read the above? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.
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.