JSP uses a servlet filter for authentication _jsp programming

Source: Internet
Author: User

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.