1. How can I obtain files under the current web application in the code under the src object?
A: You can define a servlet. this servlet does not need to be mapped. You can configure it to start when the server is started. In this servlet, you can obtain the servletcontext pair
This object can be used to obtain the getrealpath (absolute path) of the file we are interested in, and save this absolute path to the static variable of a custom class.
Yes.
2. If you want to filter client input information?
A: The filter is used, but the request can only obtain page parameters, but cannot modify the value of the page parameters and return it to the page (that is, only the getparameter side
Method, but there is no setparameter method). At this time, we can only rewrite the getparameter method of the request object by inheritance. In this case
Two common methods are provided for implementation.
Solution 1. inherit the javax. servlet. http. httpservletrequestwrapper class
This class is actually a decoration of the request object. A request object is obtained through the constructor. All the methods in the internal httpservletrequest interface are actually called.
This request object is used for implementation.
When this class is used to pass in the request object, we only need to rewrite the methods we are interested in, while other methods use the default Implementation of the request. For example, here we will rewrite the getparameter method.
@ Override <br/> Public String getparameter (string name) <br/>{< br/> string value = super. getparameter (name); <br/> If (value! = NULL & value. contains ("bad") <br/>{< br/> value = value. replaceall ("bad", "======"); <br/>}< br/> return value; <br/>}< br/>
Solution 2:
Use Dynamic proxy to pass in a request object as the object to be proxy, and use the dynamic proxy invoke method to traverse each of its internal methods.
"Getparameter" is overwritten, and other methods call its initial implementation directly.
3. Differences between several terms
Assume that the project is a filter
Root directory (PATH) of the Web site-root directory of the Web site: http: // localhost: 8080/
The root directory of the Web application-http: // localhost: 8080/filter/
4. Meaning of "/": (the project is filter)
1) represents the root directory of the web site in the Action attribute of-form-Form
Action = "/loginservlet" indicates that the http: // localhost: 8080/loginservlet server will go outside the root directory of the current web application. This will definitely cause an error!
Action = "loginservlet" indicates http: // localhost: 8080/filter/loginservlet. This is the path that we usually map to servlet.
Under the same level directory)
2) represent the root directory of the web site in -- redirect -- operation
In loginservlet. Java:
Response. sendredirect ("/Hello. jsp"); indicates that http: // localhost: 8080/Hello. jsp does not exist and an error is returned.
Response. sendredirect ("Hello. jsp"); indicates that hello. jsp and loginservlet are in the same directory; otherwise, an error is reported.
3) represent the root directory of the current web application in -- forward -- operation
In loginservlet. Java:
Request. getrequestdispatcher ("/Hello. jsp") indicates http: // localhost: 8080/filter/Hello. jsp
Request. getrequestdispatcher ("Hello. jsp") indicates that hello. jsp and loginservlet are in the same directory.
4) represent the root directory of the current web application in the -- ing path -- operation.
In the Web. xml file:
<URL-pattern>/loginservlet </url-pattern> maps loginservlet to http: // localhost: 8080/filter/loginservlet.
5) "/" indicates the current directory.
The request object must be in the same directory as the object that receives the request. Otherwise, an error will be reported.
Current Directory -- Current Directory (different files under the same directory): http: // localhost: 8080/filter/mydir/
Filter supplement:
Values that can be set by the <dispatcher> sub-element mapped to the filter and their meanings
Request: When you directly access the page, the Web Container will call the filter. If the target resource is using the include () or forward () method of requestdispatcher
The filter will not be called.
Include: if the target resource is accessed through the include () method of requestdispatcher, the filter will be called. In addition, the filter will not be tuned
.
Forward: if the target resource is accessed through the forward () method of requestdispatcher, the filter will be called. In addition, the filter will not be called.
.
Error: if the target resource is called through the declarative Exception Handling Mechanism, the filter will be called. In addition, the filter will not be called.
If this parameter is not configured, requests are filtered by default.
When several filters with the same target are configured at the same time, pay attention to their execution order and reflect this order.