1.登陆页面

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>Insert title here</title>
</head>
<body>
<form action="LoginCheckServlet" method="post">
请输入用户名:<input type="text" name="username"/><br/>
请输入密码:<input type="password" name="userpwd"/><br/>
<input type="submit" value="登录"/>
<input type="reset"/>
</form>
</body>
</html>

2.登陆验证

LoginCheckServlet.java

package com.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("/LoginCheckServlet")
public class LoginCheckServlet extends HttpServlet {

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        String name=request.getParameter("username"); //获取表单传来的用户名
        String pwd=request.getParameter("userpwd"); //获取表单传来的密码
        String into="";
//验证
        if(name.equals("abc") && pwd.equals("123"))
        {
            into=name +" 用户 登陆成功,欢迎光临!";
            HttpSession session = request.getSession();
// 2.将用户名保存在session中
            session.setAttribute("u_name", name);
        }
        else
        {
            into=name +" 用户名 登陆失败!请注册";
        }
//将into设置 为request范围内对象的属性
        request.setAttribute("showinto", into);
//将into设置 为session范围内对象的属性

//跳转到显示页面

        request.getRequestDispatcher("yanz/demo_show.jsp").forward(request, response); //转向
//response.sendRedirect("demo_show.jsp");//重定向

    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

}

3.过滤器页面

LoginFilter.java

package com.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.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * Servlet Filter implementation class LoginFilter
 */
@WebFilter("/loginFilter")
public class LoginFilter implements Filter {

    public void destroy() {

    }

    public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterchain) throws IOException, ServletException {
        HttpServletRequest requ = (HttpServletRequest)request;
        HttpServletResponse resp = (HttpServletResponse)response;
        HttpSession session = requ.getSession(true);
        if(session.getAttribute("u_name")==null)
        {
            resp.sendRedirect("/benzhu/login.jsp");}
        else{filterchain.doFilter(request, response);}
    }
    public void init(FilterConfig arg0) throws ServletException {
    }

}

4.登陆成功页面

demo_show.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>
<%=request.getAttribute("showinto") %>
</body>
</html>

5.配置xml

web.xml

<filter>
<filter-name>loginfilter</filter-name>
<filter-class>com.filter.LoginFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>loginfilter</filter-name>
<url-pattern>/yanz/*</url-pattern>
</filter-mapping>