The example in this article describes how the JSP uses the servlet filter for authentication. Share to everyone for your reference, specific as follows:
1, the role of the servlet filter description
(1) Intercept the customer's httpservletrequest before HttpServletRequest arrives in the servlet.
Check httpservletrequest as needed, or modify httpservletrequest headers and data.
(2) Intercept HttpServletResponse before HttpServletResponse arrives at the client.
Check HttpServletResponse as needed to modify HttpServletResponse headers and data.
2. Apply servlet filter for authentication
Assume that the login1.htm, longin1.jsp in the root directory of the Web site are used for user logons, and files under the Chap08 directory require a user login to access them.
(1) Writing servlet filters
@WebFilter ("/filterstation") public
class Filterstation extends HttpServlet implements Filter {
private Filterconfig Filterconfig;
Public filterstation () {
super ();
}
public void Destroy () {
} public
void Dofilter (ServletRequest request, servletresponse response, Filterchain Chain) throws IOException, servletexception {HttpSession
session= ((httpservletrequest) request). GetSession ();
Response.setcharacterencoding ("gb2312");
if (Session.getattribute ("Me") ==null) {
printwriter out=response.getwriter ();
Out.print ("<script>alert" Please login!) '); location.href= '. /login1.htm ' </script> ');
}
else{
//Pass the request along the filter chain
chain.dofilter (request, response);
}
public void init (Filterconfig fconfig) throws Servletexception {/
/TODO auto-generated method stub
This.filterconfig=fconfig
}
}
(2) Configure Web.xml
<filter>
<filter-name>filterstation</filter-name>
<filter-class>zhou. filterstation</filter-class>
</filter>
<filter-mapping>
<filter-name> filterstation</filter-name>
<url-pattern>/chap08/*</url-pattern>
</filter-mapping >
(3) Login1.htm code
(4) Login1.jsp code
<%@ page contenttype= "text/html;charset=gb2312"%>
I hope this article will help you with JSP program design.