Tag: Val Server-side text requires INF real-byte program memory
Objective
I described earlier what a servlet is, its life cycle, the execution process, and its rationale. Here we do a simple review!
What is a servlet?
A servlet is a small Java program that runs on a WEB server (that is, a small application on the server side). Servlets typically receive and respond to requests from WEB clients over HTTP (Hypertext Transfer Protocol) .
Execution process:
Life cycle:
Instantiated---initialize--services--destroy
Born: (instantiated and initialized) first time access to Servlet is born (by default)
Live: (Service) application alive, the servlet is alive
Death: (Destroy) the application unloads the servlet and destroys it.
How to let the servlet be created when the server is started:
Configure a load-on-startup in the servlet tag in Web. XML to set
First, the servlet specification core class diagram
The next thing we'll talk about is the four classes of the servlet:
ServletConfig object, ServletContext object, Request object, Response object
We can know the connection between these several objects!
First, ServletConfig object 1.1, Get ServletConfig Object
1) Use initialization method to get a ServletConfig object
2) Get a ServletConfig object by inheriting the parent class (Genericservlet) method
this. Getservletconfig ();
1.2. ServletConfig Object function
1) getservletname (); Gets the name of the servlet, which is the servlet-name that we configured in Web. xml
2) Getservletcontext (); Get the ServletContext object, the role of the object see below to explain
3) Getinitparameter (String); Gets the value of the initialization parameter in the servlet. Here , note the distinction with global initialization parameters. This gets only the initialization parameters under the servlet
4) getinitparameternames (); Gets the name of all initialization parameters in the servlet, which is the key value, which can be used to find the value of each initialization parameter by using the key value . Note that the enumeration type is returned
In the Servlet class
Results
Note: In the process of the source code we analyzed above, we know that we can not get servletconfig First, then we can use its methods directly , such as the ServletConfig () we used above. Getservletname ();
Can be written directly to Getservletname (), rather than get ServletConfig (), the reason is that in the Genericservlet, has helped us to obtain the data, we only need to directly take the line .
this. Getinitparameter ("Wolf");
Second, ServletContext2.1, get ServletContext object
1) getservletcontext ();
2) Getservletconfig (). Getservletcontext ();
The difference between the two methods of acquisition is the same as the above explanation, the first is to take directly, in Genericservlet has helped us to use Getservletconfig (). Getservletcontext (); Got the ServletContext. .
We just have to get it straight, and the second is the same as we're getting it again, and the two readings are the same.
2.2. ServletContext Object function
Tomcat creates a ServletContext instance for each Web project that Tomcat creates at startup, destroys when the server shuts down, shares data in a Web project , manages Web project resources,
Configure public information for the entire web , and so on, popular point, is a Web project, there is a ServletContext instance, each servlet read can access it .
1) Sharing data in Web projects
Allow multiple servlets to share data within a certain range (currently applied)
GetAttribute (string name), SetAttribute (string name, Object obj), removeattribute (string name)
1.1) SetAttribute (String name, Object obj) stores content within the scope of a Web project so that all servlets in the Web project can read access to the
1.2) getattribute (String name) to get the content by specifying a name
1.3) RemoveAttribute (String name) removes content by specifying a name
Instance:
We write in the servlet_demo_0010:
this. Getservletcontext (). SetAttribute ("bang""ADC" );
And then we'll see in servlet_demo_0020 if we can get:
Results:
Get the ADC
2) Get global configuration information
The entire Web project initialization parameter (this is the global initialization parameter, which can be obtained in each servlet)
2.1) Getinitpatameter (String name)//Gets the initialization value by specifying a name
2.2) Getinitparameternames ()//Get enum type
Web. XML configures the initialization of the entire website project
Results:
3) Get Web project resources
3.1) Gets the path to the specified resource under the WEB Project: Getservletcontext (). Getrealpath ("/web-inf/web.xml")
String Getrealpath (string path);// The absolute path to the resource based on the resource name
Results:
Application:
3.2) Gets the contents of the specified resource under the Web project, and returns the byte input stream. InputStream getResourceAsStream (java.lang.String path)
Part of the result:
3.3) Getresourcepaths (java.lang.String path) specifies all content under the path.
Results:
3.4) Implement the servlet forwarding
RequestDispatcher getrequestdispatcher (String path);//parameter indicates where to jump to
Javaweb (i) ServletConfig and ServletContext in the servlet