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 mainly explains "what are the ways of Servlet comments", the content of the explanation is simple and clear, easy to learn and understand, now please follow the editor's ideas slowly in depth, together to study and learn "what are the ways of Servlet comments"!
It is worth noting that deployment descriptors take precedence over comments. In other words, the deployment descriptor overrides the configuration information specified by the annotation mechanism.
Version 3.0 of the Web deployment descriptor includes a new attribute called metadata-complete on the web-app element. This property defines whether the web descriptor is complete or whether the class file of the web application is checked against comments for the specified deployment information. If this property is set to true, the deployment tool must ignore any servlet comments that exist in the class file and use only the configuration details mentioned in the descriptor. Otherwise, if the value is not specified or is set to false, the container must scan all class files of the application for comments. This property provides the ability to enable or disable annotation scanning and the processing of comments during the startup phase of the application.
All comments introduced in Servlet 3.0 can be found in the javax.servlet.http.annotation and javax.servlet.http.annotation.jaxrs packages.
Servlet:javax.servlet.http.annotation.Servlet is a class-level comment that confirms that the annotated class is a servlet and holds metadata about the declared servlet. The urlMappings attribute is a mandatory attribute of @ Servlet that specifies the URL schema (which calls the servlet). When a request is received, the container matches the URL in the request with the urlMappings of the servlet, and if the URL pattern matches, the corresponding servlet is called in response to the request. All other properties of the comment are optional and have reasonable default values. There must be a way in the Servlet class to annotate with HttpMethod annotations such as GET, PUT, POST, HEAD, or DELETE. These methods should take HttpServletRequest and HttpServletResponse as method parameters. In contrast to previous versions, the version of servlets 3.0 can be implemented as a simple traditional Java object (Plain Old Java Objects,POJOs); that is, servlets no longer has to extend basic servlet implementation classes like HTTPServlet or GenericServlet.
For comparison purposes, a snippet of Java servlet code written in traditional Servlet 2.5 API is given here, as shown below. In Servlet 2.5, the web container initializes servlet as long as the details of servlet are configured in the deployment descriptor.
Public class MyServlet extends HttpServlet {public void doGet (HttpServletRequest req, HttpServletResponse res) {.... }}
Deployment descriptor (web.xml)
MyServlet samples.MyServlet MyServlet / MyApp...
Here is a simpler version written using Servlet 3.0 API. When MyServlet is annotated as a servlet using the @ Servlet annotation, it is initialized during startup of the web container. Note that the deployment descriptor is optional in this case.
@ Servlet (urlMappings= {"/ MyApp"}) public class MyServlet {@ GET public void handleGet (HttpServletRequest req, HttpServletResponse res) {.... }} Deployment descriptor (web.xml) optional
@ ServletFilter and @ FilterMapping: you can easily create a servlet filter by annotating the filter class using the javax.servlet.http.annotation.ServletFilter annotation. This comment encapsulates metadata about the filter being declared. It is also mandatory to have @ FilterMapping annotations on filter classes. The @ FilterMapping annotation defines the URL schema used for the filter. All other properties of @ ServletFilter are optional and have reasonable default values. The V3.0 filter class is now similar to the POJO class and does not have the Filter interface or nonparametric common constructor required for these classes. The following is a code snippet of the filter class that uses Servlet v2.5 API:
Public class MyFilter implements Filter {public void doFilter (ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {. } Deployment descriptor (web.xml) MyFilter samples.MyFilter MyFilter / foo...
A sample filter class written using Servlet 3.0 is shown below. Because the class uses ServletFilter annotations, the container marks MyFilter as a filter class. MyFilter intercepts all received requests, where the URL of the request matches the pattern / foo. Servlet 3.0 provides optional deployment descriptors for filter configuration.
ServletFilter @ FilterMapping ("/ foo") public class MyFilter {public void doFilter (HttpServletRequest req, HttpServletResponse res) {. }} Deployment descriptor (web.xml) optional
InitParam: this annotation can be used to define any initialization parameters that must be passed to the servlet or filter class. It is an attribute of the @ Servlet and @ ServletFilter annotations. The following code example illustrates how to pass an initialization parameter with an english value called lang to a servlet class.
@ Servlet (urlMappings= {"/ MyApp"}, initParams = {@ InitParam
(name= "lang", value= "english")})
Public class MyServlet {
@ GET
Public void handleGet (HttpServletRequest req
HttpServletResponse res) {
....
}
}
The @ ServletContextListener:javax.servlet.http.annotation.ServletContextListener annotation declares the class as a servlet context listener. The context listener receives comments when the web container creates or destroys the ServletContext. The context listener is a POJO class and does not have to implement the ServletContextListener interface. The listener classes written using Servlet 2.5 API are shown below. The container recognizes the listener class if and only if you have configured it in the deployment descriptor.
Public class MyListener implements ServletContextListener {public void contextInitialized (ServletContextEvent sce) {}. } Deployment Descriptor (web.xml) samples.MyListener....
A greatly simplified listener class written using Servlet 3.0 API is shown below.
@ ServletContextListener public class MyListener {public void contextInitialized (ServletContextEvent sce) {}. } Deployment Descriptor (web.xml) optional thank you for reading, the above is the content of "what are the ways of Servlet comments?" after the study of this article, I believe you have a deeper understanding of the way of Servlet comments, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
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.