实现功能
- 用户访问login.jsp页面登陆时,要实现登陆成功跳转到success.jsp页面,登陆失败跳转到fail.jsp页面。
- 用户直接访问success.jsp页面,跳转到login.jsp页面,直接访问fail.jsp跳转到fail.jsp
- 用户访问/LoginServlet,登陆成功跳转到success.jsp页面,登陆失败跳转到fail.jsp页面。
工程目录结构
实现代码
login.jsp代码
<%@ 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>用户登录</title>
</head>
<body>
<form action="<%=request.getContextPath() %>/LoginServlet" method="post">
用户名:<input type="text" name="username"><br>
密码: <input type="password" name="password"><br>
<input type="submit" value="提交">
<input type="reset" value="重置">
</form>
</body>
</html>
success.jsp代码
<%@ 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>登陆成功</title>
</head>
<body>
<h3>登陆成功,欢迎您${username }</h3>
</body>
</html>
fail.jsp代码
<%@ 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>登陆失败</title>
</head>
<body>
<h3>登陆失败,请检查用户名和密码!</h3>
</body>
</html>
LoginServlet.java代码
package com.sun.login.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(urlPatterns = { "/LoginServlet" })
public class LoginServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String username = request.getParameter("username"); // 获取用户名
String password = request.getParameter("password"); // 获取密码
if ("admin".equals(username) && "admin".equals(password)) {
// 校验通过
HttpSession session = request.getSession();
session.setAttribute("username", username);
response.sendRedirect(request.getContextPath() + "/success.jsp");
} else {
// 校验失败
response.sendRedirect(request.getContextPath() + "/fail.jsp");
}
}
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
LoginFilter.Java代码
package com.sun.login.filter;
import java.io.IOException;
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.annotation.WebInitParam;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
@WebFilter(urlPatterns = { "/*" }, initParams = {
@WebInitParam(name = "noLoginPaths", value = "login.jsp;LoginServlet;fail.jsp"),//直接访问这些页面时,不校验session,直接通过过滤器
@WebInitParam(name = "charset", value = "UTF-8")})//编码初始化参数
public class LoginFilter implements Filter {
private FilterConfig config;
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();//获取session
String noLoginPaths = config.getInitParameter("noLoginPaths");
String charset=config.getInitParameter("charset");//获取初始化编码参数
if(charset==null){//如果初始参数为null,设置编码格式为"UTF-8"
charset="UTF-8";
}
req.setCharacterEncoding(charset);
if (noLoginPaths != null && noLoginPaths.length() != 0) {
String[] strings = noLoginPaths.split(";");
for (int i = 0; i < strings.length; i++) {
if (strings[i] == null || "".equals(strings[i])) {
continue;
}
if (req.getRequestURI().indexOf(strings[i]) != -1) {
chain.doFilter(request, response);
return;
}
}
}
if (session.getAttribute("username") != null) {
chain.doFilter(request, response);
} else {
resp.sendRedirect(req.getContextPath() + "/login.jsp");//直接访问页面时,校验是否有session,有的话通过,没有的话跳转到到login.jsp页面
}
}
public void init(FilterConfig fConfig) throws ServletException {
config = fConfig;
}
}