Servlet
Filters can add additional actions before the request arrives in the JSP or servlet or before the response returns to the client. If you have multiple JSP pages and a servlet that require an identical or similar operation, we can extract the operation into a servlet filter and intercept the interest JSP or servlet via a matching path.
We cite several examples of typical filter applications that readers can generalize by:
• Code Conversion
Garbled problems have plagued many Web application developers because java-based Web applications may run on different Web applications, operating system platforms, or hardware servers, and different environments have their own default encoding types. In the process of data conversion, different default encoding is the originator of garbled problem, so it is often necessary to encode and convert the data when developing JSP. We can create a servlet filter to encode conversions before requesting access to the service program.
• Add a uniform title or footnote
Some pages need to add a uniform header or footnote, and you can add a uniform title and footnote to a Web page by using the servlet filter before the response is returned to the client.
• Safe control
Log on to the system, will generally put the user information object into the session, you can through the servlet filter in the request to enter the JSP or servlet before the user information object, if there is, then the user has logged in, if not, then the user has not logged in, Redirects the request to the login page.
The servlet filters provided in this section are designed to implement security controls, and we will develop this servlet below.
1. Open the wizard that creates the servlet filter.
Start the wizard that creates the servlet filter by double-clicking the filter servlet icon by file->new...->web->, as shown in the following illustration:
Figure 7 Specifying the name of the servlet filter |
Similar to creating a standard servlet, specify the filter's class name and package name in this step. Enter Logincheckfilter in class name, and the package name is Bookstore.servlet. Press next to the next step.
2. Specify a path-matching pattern for the filter
Figure 8 Specifying a matching path pattern for the filter |
· Name:logincheckfilter, specify the name of the filter
· URL pattern:/*, which specifies the filter path matching pattern, where we filter all requests by the filter.
Press Finish to create this filter.
Open the Logincheckfilter.java file created by the wizard and enter the code shown in bold, as follows:
Code Listing 4 Logincheckfilter.java code
1. Package bookstore.servlet; 2... 3. PublicClass Logincheckfilter 4. Extends HttpServlet implements Filter 5. { 6... 7. PublicvoidDofilter (ServletRequestRequest, ServletresponseResponse 8., Filterchain Filterchain) 9. { Try 11. { 12.//Type conversions for requests and responses HttpServletRequest HttpRequest = (httpservletrequest) request; HttpServletResponse HttpResponse = (httpservletresponse) response; 15. Boolean isValid = true; String uristr = Httprequest.getrequesturi (). toUpperCase (); An. if (uristr. IndexOf("LOGIN.") JSP ") = = 1 && Uristr.indexof ("SWITCH. JSP ") = = 1 && Httprequest.getsession (). getattribute ("ses_userbean") = = null) 21. { IsValid = false; 23.} if (isValid) 25. { Filterchain.dofilter (request, response); An. Else 28. { Httpresponse.sendredirect ("/webmodule/login.jsp"); .} 31. The catch (servletexception SX) 33. { Filterconfig.getservletcontext (). log (Sx.getmessage ()); catch (IOException Iox) 36. { Filterconfig.getservletcontext (). log (Iox.getmessage ()); 38.} 39.} 40... 41.} |
Because login.jsp is the user login page, and switch.jsp is the user login processing page, when accessing these two pages, the user information object has not been produced, so should be excluded from these two pages in the filter test rules this outside. We determine the request path by determining whether it is login.jsp and switch.jsp, as shown in line 18th to 19th.
If they are not, they must be validated to determine whether the session object of the page contains an object named Ses_userbean (line 20th). If you do not include an object named Ses_userbean, redirect to the login page (line 29th), otherwise the request is passed to the requested target program (line 26th).
Open Web.xml, and you will see the deployment description configuration information about the Logincheckfilter filter, as shown in bold:
Code Listing 5 Deployment Description configuration information for filters
1. The 2. Webmodule 3. The 4. Logincheckfilter 5. Bookstore.servlet.LoginCheckFilter 6. The 7. The 8. Logincheckfilter 9. /* - 11... A. |
The <filter-name> configures the name and implementation class of the filter, and <filter-mapping> configures the path-matching pattern for the filter.