JSP使用Servlet過濾器進(jìn)行身份驗(yàn)證的方法
本文實(shí)例講述了JSP使用Servlet過濾器進(jìn)行身份驗(yàn)證的方法。分享給大家供大家參考,具體如下:
1、Servlet過濾器的作用描述
(1)在HttpServletRequest到達(dá)Servlet 之前,攔截客戶的HttpServletRequest。
根據(jù)需要檢查HttpServletRequest,也可以修改HttpServletRequest頭和數(shù)據(jù)。
(2)在HttpServletResponse 到達(dá)客戶端之前,攔截HttpServletResponse。
根據(jù)需要檢查HttpServletResponse,可以修改HttpServletResponse頭和數(shù)據(jù)。
2、應(yīng)用Servlet過濾器進(jìn)行身份驗(yàn)證
假設(shè)網(wǎng)站根目錄下的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('請(qǐng)登錄!');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 應(yīng)用演示</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('用戶名或密碼錯(cuò)誤');location.href='login1.htm'</script>");
}
}
%>
</html>
希望本文所述對(duì)大家JSP程序設(shè)計(jì)有所幫助。
- 通過過濾器(Filter)解決JSP的Post和Request中文亂碼問題
- JSP過濾器Filter配置過濾類型全部匯總
- JSP 開發(fā)中過濾器filter設(shè)置編碼格式的實(shí)現(xiàn)方法
- JSP過濾器防止Xss漏洞的實(shí)現(xiàn)方法(分享)
- JSP使用過濾器防止SQL注入的簡單實(shí)現(xiàn)
- jsp實(shí)現(xiàn)登錄驗(yàn)證的過濾器
- jsp中過濾器選擇過濾器的寫法詳解
- 詳解JSP中使用過濾器進(jìn)行內(nèi)容編碼的解決辦法
- servlet+jsp實(shí)現(xiàn)過濾器 防止用戶未登錄訪問
- JSP使用過濾器防止Xss漏洞
- jsp filter 過濾器功能與簡單用法示例
相關(guān)文章
JSP中常用的JSTL fmt(format格式化)標(biāo)簽用法整理
這篇文章主要介紹了JSP中常用的JSTL fmt(format格式化)標(biāo)簽用法整理,fmt的格式化處理遵循i18n國際化格式標(biāo)準(zhǔn),需要的朋友可以參考下2016-04-04
struts2中action實(shí)現(xiàn)ModelDriven后無法返回json的解決方法
struts2中action實(shí)現(xiàn)ModelDriven后無法返回json的解決方法,需要的朋友可以參考一下2013-03-03
Jsp結(jié)合XML+XSLT將輸出轉(zhuǎn)換為Html格式
Jsp結(jié)合XML+XSLT將輸出轉(zhuǎn)換為Html格式...2006-10-10
JSP對(duì)URL鏈接中的中文亂碼處理方法總結(jié)
這篇文章主要介紹了JSP對(duì)URL鏈接中的中文亂碼處理方法總結(jié),注意一下各瀏覽器的兼容顯示情況,需要的朋友可以參考下2016-01-01
jsp response.sendRedirect()用法詳解
這篇文章主要介紹了jsp response.sendRedirect()用法詳解,本篇文章通過簡要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-08-08
JSP + Servlet實(shí)現(xiàn)生成登錄驗(yàn)證碼示例
本篇文章主要介紹了JSP + Servlet實(shí)現(xiàn)登錄驗(yàn)證碼示例,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-03-03

