Wednesday, August 4, 2010

Create Servlet Filter Wizard



Filters can be reached in the request or response before the JSP or Servlet before returned to the client to add some additional operations. If there are multiple JSP pages and Servlet needs to be an identical or similar operations, we can extract this action to a Servlet filter, and the path intercepted by matching interested in JSP or Servlet.

We give some typical examples of filter applications, the reader can, by extension:

鈼?Transcoding

Garbage problems a lot of Web application developers, this is because the Web-based Java application may run on different Web application, device, operating system or hardware server platforms, different environments has its own default encoding type the data conversion process in a different default encoding is the initiator of the problem garbled, so JSP developers often need to encode the data conversion. We can create a Servlet filter to enter the service request procedures are conducted before the data conversion.

鈼?single title or footnote added

Some pages need to add the title of the first unified or footnotes, Servlet filters can be returned to the client in response to the page before adding the unified title and footnote.

鈼?safety control

Login system, usually placed in the user information object to the session, you can filter through the Servlet into the JSP or Servlet request session before the judge of whether a user information object, and if so, then the user has already logged in, if not, then the user has not logged in, redirect the request to the login page.

This section provides Servlet filters is to achieve security control, here we have to develop the Servlet.

1. Open the Create Servlet Filter guide.

And create a standard Servlet similar step in this class to specify the filter name and package name. In the Class name, type LoginCheckFilter, package name according to the bookstore.Servlet. Press Next to the next step.

2. Specify the path to match the filter pattern

鈼?Name: logincheckfilter, specify the name of the filter

鈼?URL pattern: / *, specify the path to match the filter pattern, where we make filters to filter all the requests.

Press Finish to create this filter.

Open the wizard to create the LoginCheckFilter.java file and input the code shown in bold, as follows:

package bookstore.servlet;
...
public class LoginCheckFilter
extends HttpServlet implements Filter
(
public void doFilter (ServletRequest request, ServletResponse
response, FilterChain filterChain)
(
try
(
/ / For the type of change request and response
HttpServletRequest httpRequest = (HttpServletRequest) request;
HttpServletResponse httpResponse = (HttpServletResponse) response;
boolean isValid = true;
String uriStr = httpRequest.getRequestURI (). ToUpperCase ();
if (uriStr.indexOf ("LOGIN.JSP") == -1 & &
uriStr.indexOf ("SWITCH.JSP") == -1 & &
httpRequest.getSession (). getAttribute ("ses_userBean") == null)
(
isValid = false;
)
if (isValid)
(
filterChain.doFilter (request, response);
) Else
(
httpResponse.sendRedirect ("/ webModule / login.jsp");
)
) Catch (ServletException sx)
(
filterConfig.getServletContext (). log (sx.getMessage ());
) Catch (IOException iox)
(
filterConfig.getServletContext (). log (iox.getMessage ());
)
)
...
)





相关链接:



Dell's earnings report late THREE times or face Nasdaq delisting



Youtube To Mpg



"Mirrors Edge" Cheats



Convert 3gp to mp4



JSN-1 (A) Digital Switching Introduction



free Mp4 to 3gp converter



ping command Usage



CSS box model



CSS Mouse Properties



Report Languages Education



Picked Audio Recorders



FREEHAND instance - Drawing Football



3G Version Of IPhone Experience "quality Gate"



How to read wine dealer, "and establish its own"



avi TO mp4