jsp实现登录验证的过滤器

本文实例为大家分享了jsp实现登录验证的过滤器,供大家参考,具体内容如下

1.新建一个dynamic web project项目,里面新建1个filter文件、1个servlet文件和2个jsp文件,整体的框架如图所示:

2.loginfilter.java里面的代码如下所示:

package filter;
 
import java.io.ioexception;
import java.io.printwriter;
 
import javax.servlet.filter;
import javax.servlet.filterchain;
import javax.servlet.filterconfig;
import javax.servlet.servletexception;
import javax.servlet.servletrequest;
import javax.servlet.servletresponse;
import javax.servlet.annotation.webfilter;
import javax.servlet.http.httpservletrequest;
import javax.servlet.http.httpservletresponse;
import javax.servlet.http.httpsession;
 
@webfilter(filtername = "loginfilter", urlpatterns = { "/*" })
public class loginfilter implements filter {
 
 public void destroy() {
 }
 
 public void dofilter(servletrequest request, servletresponse response,
 filterchain chain) throws ioexception, servletexception {
 httpservletrequest req = (httpservletrequest) request;
 httpservletresponse resp = (httpservletresponse) response;
 httpsession session = req.getsession(true);
 resp.setcontenttype("text/html;");
 resp.setcharacterencoding("utf-8");
 printwriter out = resp.getwriter();
 string request_uri = req.getrequesturi();
 string ctxpath = req.getcontextpath();
 string uri = request_uri.substring(ctxpath.length());
 if (uri.contains("login.jsp") || uri.contains("loginservlet")) {
 chain.dofilter(request, response);
 } else {
 if (session.getattribute("user") != null) {
 chain.dofilter(request, response);
 } else {
 out.println("您没有登录,请先登录!3秒后回到登录页面。");
 resp.setheader("refresh", "3;url=" + ctxpath + "/login.jsp");
 return;
 }
 }
 }
 
 public void init(filterconfig fconfig) throws servletexception {
 }
 
}

3.loginservlet.java里面的代码如下所示:

package servlet;
 
import java.io.ioexception;
 
import javax.servlet.servletexception;
import javax.servlet.annotation.webservlet;
import javax.servlet.http.httpservlet;
import javax.servlet.http.httpservletrequest;
import javax.servlet.http.httpservletresponse;
import javax.servlet.http.httpsession;
 
@webservlet(name = "loginservlet", urlpatterns = { "/loginservlet" })
public class loginservlet extends httpservlet {
 private static final long serialversionuid = 1l;
 
 protected void doget(httpservletrequest request,
 httpservletresponse response) throws servletexception, ioexception {
 dopost(request, response);
 }
 
 protected void dopost(httpservletrequest request,
 httpservletresponse response) throws servletexception, ioexception {
 string username = request.getparameter("name");
 string password = request.getparameter("pwd");
 if ("filter".equals(username) && "filter".equals(password)) {
 httpsession session = request.getsession(true);
 session.setattribute("user", username);
 response.sendredirect("loginsuccess.jsp");
 } else {
 response.sendredirect("login.jsp");
 }
 }
 
}

4.login.jsp里面的代码如下所示:

<%@page import="java.util.date"%>
<%@ page language="java" contenttype="text/html; charset=utf-8"
 pageencoding="utf-8"%>
<!doctype html public "-//w3c//dtd html 4.01 transitional//en" "http://www.w3.org/tr/html4/loose.dtd">
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<title>example</title>
</head>
<body bgcolor="lightpink">
 <form action="loginservlet" method="post">
 <table>
 <tr>
 <td>用户名:</td>
 <td><input type="text" name="name" /></td>
 </tr>
 <tr>
 <td>密码:</td>
 <td><input type="password" name="pwd" />
 </tr>
 <tr>
 <td><input type="submit" value="提交" /></td>
 <td><input type="reset" value="重置" /></td>
 </tr>
 </table>
 </form>
</body>
</html>

5.loginsuccess.jsp里面的代码如下所示:

<%@page import="java.util.date"%>
<%@ page language="java" contenttype="text/html; charset=utf-8"
 pageencoding="utf-8"%>
<!doctype html public "-//w3c//dtd html 4.01 transitional//en" "http://www.w3.org/tr/html4/loose.dtd">
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<title>example</title>
</head>
<body>
 <%
 string username = (string) session.getattribute("user");
 %>
 恭喜<%=username%>登录成功!
</body>
</html>

6.在浏览器里面输入http://localhost:8080/servlet/login.jsp运行之后得到的结果如下所示:

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持硕编程。

相关文章