Talking about Servlet
I. Comparison between the traditional servlet configuration and the Java EE 7 Servlet
As we all know, the traditional creation of a servlet requires a related configuration in the Web. xml file. That needs to be in Web. xml
File, add the following code:
<servlet> <servlet-name>First_Servlet</servlet-name> <servlet-class>servlet. first_servlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>first_servlet </servlet-name> <url-pattern>/hello</url-pattern> </servlet-mapping>
In Java EE 7, whenever we create a new servlet, we do not need to configure it in the E.xml file, only
You need to inherit the servlet class HttpServlet and declare @WebServlet ("/hello") annotations before that class to start the Tomcat server
, the servlet can be accessed by Http://localhost:8080/Java_Web/hello on this computer.
Note the point:
1. @WebServlet annotations are used to identify a class that is a servlet class.
2, when the annotation is used, generally contains the Urlpatterns, the Value property, but when the note is only used to declare the servlet's
When you access a path, that is, when you declare urlpatterns, you only need to specify the value. Otherwise, when more than two properties are required
, you need to display the specified Urlpatterns property instead of specifying the value only.
Second, comparison between the traditional Servlet initialization parameter configuration and the Java EE 7 Servlet
In the Java Web, we can configure at least two types of parameters.
The first is the Web global initialization parameter, which takes effect for the entire Web application. The second type is the servlet initialization parameters. But in the past
Are configured in the Web. xml file, the detailed configuration is as follows:
<!-- Global Parameters --> <context-param> < param-name>user</param-name> <param-value>root</ param-value> </context-param> <!-- servlet Parameters --> <servlet> <servlet-name>first_servlet</ Servlet-name> <servlet-class>servlet. first_servlet</servlet-class> <init-param> <param-name>user</param-name> <param-value>root< /param-value> </init-param> </servlet> < servlet-mapping> <servlet-name>first_servlet</servlet-name> <url-pattern>/hello</url-pattern> </servlet-mapPing>
The above two parameters can be obtained in a servlet in one of the following ways:
String userName = Request.getservletcontext (). Getinitparameter ("user"); System.out.println ("= =" +username);p ublic void init (servletconfig config) throws servletexception {//TODO Auto-generated method Stubsuper.init (config); System.out.println (Config.getinitparameter ("user")); Or: ServletConfig p = this.getservletconfig (); String value = P.getinitparameter ("name"); System.out.println (value);///Note: The value of the initialization parameter cannot be obtained directly through the request object, but can be obtained by request of ServletContext to//like and then get the value of the global initialization variable, as Request.getservletcontext (). Getinitparameter ("user")
Of course, in addition to the above two parameters, we can also configure other parameters, such as filter filter parameters.
In Java EE 7, the use of Java annotations to define servlet initialization parameters (non-global parameters) is allowed
The servlet path is declared with an initialization parameter of @webservlet (urlpatterns= "/a", [email protected] (name= "name", value= "a")
We might ask, what if I want to declare multiple servlet initialization parameters?
The servlet path is declared along with the 2 initialization parameters of the servlet @webservlet (urlpatterns= "/A", initparams={@WebInitParam (name= "name", value= "A"), @WebInitParam (name= "age", value= "13")})
In @webservlet annotations, the InitParams property can pass in an array of @webinitparam annotations, each @webinitparam
Annotations are equivalent to defining an initialization parameter, but note that multiple parameters are wrapped in brackets {}.
This article is from the "@coder" blog, be sure to keep this source http://smallcoder.blog.51cto.com/11941149/1870185
Talking about Servlets