用户登录检查实现

1、登录页面
Java代码

public class login extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        if (username.equals("wjj") && password.equals("123")) {
            req.getSession().setAttribute(Constant.USER_SESSION, req.getSession().getId());
            resp.sendRedirect("/sys/success.jsp");
        } else {
            resp.sendRedirect("/error.jsp");
        }

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req, resp);
    }
}

Jsp代码

<form action="/servlet/login" method="post" style="text-align: center">
    账号:<input type="text" name="username">
    <br>
    密码:<input type="password" name="password">
    <br>
    <input type="submit" value="登录">
</form>

2、成功页面

 <h1 style="text-align: center">主页</h1>
    <h3 style="text-align: center">登陆成功,欢迎你。</h3>
    <p style="text-align: center"> <a href="/servlet/loginout">注销</a> </p>

3、失败页面

<h2 style="text-align: center">登陆失败,用户名或者密码错误。请重试!</h2>

<p style="text-align: center"> <a href="/login.jsp">返回登录页面。</a> </p>

4、用户是否非法登录过滤器实现
Java代码

public class LoginFilter implements Filter {

    public void init(FilterConfig filterConfig) throws ServletException {

    }

    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain)
            throws IOException, ServletException {

        HttpServletRequest request = (HttpServletRequest) servletRequest;
        HttpServletResponse response = (HttpServletResponse) servletResponse;
        Object userSession = request.getSession().getAttribute(Constant.USER_SESSION);
        if (userSession == null) {
            response.sendRedirect("/error.jsp");
        }


        filterChain.doFilter(servletRequest, servletResponse);
    }

    public void destroy() {

    }
}

web.xml代码

    <filter>
        <filter-name>LoginFilter</filter-name>
        <filter-class>com.wjj.filter.LoginFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>LoginFilter</filter-name>
        <url-pattern>/sys/*</url-pattern>
    </filter-mapping>

5、注销登录

public class loginout extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Object user_session = req.getSession().getAttribute("USER_SESSION");
        if (user_session != null) {
            req.getSession().removeAttribute(Constant.USER_SESSION);
//            resp.sendRedirect("/login.jsp");
            resp.setHeader("refresh","1;/login.jsp");
        } else {
            resp.setHeader("refresh","1;/login.jsp");
        }

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req, resp);
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值