1:首先在eclipse中建立一个Web工程
2:创建一个登陆界面(login.jsp)和一个登录成功回显的页面(success.jsp)
3:创建一个Servlet用于接受前台数据和负责数据的验证,再创建一个Filter用于拦截请求,判断是否进行自动登录。
4 : 记住在Web.xml中配置Filter,否则无法发挥作用。
5:如图所示
6 部分代码展示
loginServlet代码展示:
package cm.itcast.servlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
public class LoginServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//设置字符编码
request.setCharacterEncoding("utf-8");
//收集数据
String username = request.getParameter("username");
String password = request.getParameter("password");
HttpSession session = request.getSession();
// 进行验证
if ("username".equals(username) && "password".equals(password))
{
String autoLogin = request.getParameter("autoLogin");
String rememberName = request.getParameter("rememberName");
//是否选择自动登录
if( autoLogin!=null)
{
//如果选择自动登录,存储到cookie中
Cookie cookies_username = new Cookie("username", username);
Cookie cookies_password = new Cookie("password", password);
//设置持久化时间
cookies_username.setMaxAge(60*60);
cookies_password.setMaxAge(60*60);
//设置cookie携带路径
cookies_password.setPath("/");
cookies_password.setPath("/");
//发送cookie到浏览器,也就是 回写cookie
response.addCookie(cookies_username);
response.addCookie(cookies_password);
session.setAttribute("username", username);
session.setAttribute("password", password);
}
if(rememberName!=null)
{
session.setAttribute("username", username);
session.setAttribute("password", password);
}
//验证通过,跳转到登录后的页面
response.sendRedirect(request.getContextPath()+"/success.jsp");
return;
}else
{
//验证没有通过,再次跳转到登录页面,进行登录
request.getRequestDispatcher("/login.jsp").forward(request, response);
}
}
}
7:Filter代码展示
package cm.itcast.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.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
/**
* 实现自动登录
*/
public class loginFilter implements Filter {
// 初始化执行方法
public void init(FilterConfig arg0) throws ServletException {
System.out.println("拦截到了");
}
// 拦截方法
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException {
HttpServletRequest req = (HttpServletRequest) request;
HttpServletResponse rep = (HttpServletResponse) response;
HttpSession session = req.getSession();
String cookie_username = null;
String cookie_password = null;
if (req.getCookies() != null){
Cookie[] cookie = req.getCookies();
for (Cookie cookies : cookie)
{
if ("username".equals(cookies.getName()))
cookie_username = cookies.getValue();
if ("password".equals(cookies.getName()))
cookie_password = cookies.getValue();
}
if (cookie_username != null && cookie_password != null)
{
if (cookie_username.equals("username") && cookie_password.equals("password"))
{
session.setAttribute("username", cookie_username);
session.setAttribute("password", cookie_password);
req.getRequestDispatcher("/success.jsp").forward(req, rep);
}
}else
{
req.getRequestDispatcher("/LoginServlet").forward(req, rep);
}
}
chain.doFilter(request, response);
}
// 销毁执行方法
public void destroy() {
System.out.println("拦截结束了");
}
}
8 login.jsp代展示
9: success.jsp代码展示
10:自己也是新手,参考别人的一些代码,最后终于实现了,这不是最新的技术,仅供参考,欢迎大家留言讨论。