JSP使用Servlet过滤器进行身份验证的方法

栏目: JSP · 发布时间: 8年前

内容简介:JSP使用Servlet过滤器进行身份验证的方法

本文实例讲述了JSP使用Servlet过滤器进行身份验证的方法。分享给大家供大家参考,具体如下:

1、Servlet过滤器的作用描述

(1)在HttpServletRequest到达Servlet 之前,拦截客户的HttpServletRequest。

根据需要检查HttpServletRequest,也可以修改HttpServletRequest头和数据。

(2)在HttpServletResponse 到达客户端之前,拦截HttpServletResponse。

根据需要检查HttpServletResponse,可以修改HttpServletResponse头和数据。

2、应用Servlet过滤器进行身份验证

假设网站根目录下的login1.htm、longin1.jsp用于用户登录,而chap08目录下的文件需要用户登录后才能访问。

(1)编写Servlet过滤器

@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('请登录!');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)配置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代码

<html>
<head>
<title>用户登录</title>
</head>
<body>
<form method="POST" action="login1.jsp">
<p>用户名:<input type="text" name="user" size="18"></p>
<p>密码:<input type="text" name="pass" size="20"></p>
<p><input type="submit" value="提交" name="ok">
<input type="reset" value="重置" name="cancel"></p>
</form>
</body>
</html>

(4)login1.jsp代码

<%@ page contentType="text/html;charset=GB2312" %>
<html>
<head><title>Session 应用演示</title></head>
<% 
if (request.getParameter("user")!=null && request.getParameter("pass")!=null)
{
String strName=request.getParameter("user");
String strPass=request.getParameter("pass");
if (strName.equals("admin") && strPass.equals("admin"))
{
session.setAttribute("login","OK");
session.setAttribute("me",strName);
response.sendRedirect("chap08/welcome.jsp");
}
else
{
out.print("<script>alert('用户名或密码错误');location.href='login1.htm'</script>");
}
}
%>
</html>

希望本文所述对大家JSP程序设计有所帮助。


以上所述就是小编给大家介绍的《JSP使用Servlet过滤器进行身份验证的方法》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

深度解析淘宝运营

深度解析淘宝运营

刘涛 / 电子工业出版社 / 2015-9-1 / 49.00元

淘宝运营,仅有知识是不够的,还需要有系统的运营思路。为帮助广大电商从业者以及众多中小卖家更好地运营店铺,《深度解析淘宝运营》全面阐述了整个店铺运营的重点环节,包括淘宝搜索规则、打造爆款、店铺规划、客户服务、直通车、钻石展位、数据分析等内容。具体操作步骤翔实,并且结合笔者的实际操作经验,将各个环节最本质的一面透彻展现给读者,结合理论与实战,尽可能向读者展示一个最真实的运营核心。《深度解析淘宝运营》没......一起来看看 《深度解析淘宝运营》 这本书的介绍吧!

JSON 在线解析
JSON 在线解析

在线 JSON 格式化工具

HEX CMYK 转换工具
HEX CMYK 转换工具

HEX CMYK 互转工具

HSV CMYK 转换工具
HSV CMYK 转换工具

HSV CMYK互换工具