JAVAWEB学习-session-用session实现禁止用户非法访问页面

今天学习了JAVAWEB中session的有关知识,能够实现禁止用户非法访问页面.

session可以理解为一个表,每条数据有两个属性,String 和 Object ,即名称和内容

session 的作用是可以实现不同的页面保存共享同一个数据,在许多方面十分有用

如:购物网站的购物车

  保存登录用户的信息

  防止用户非法登录到某一个页面

  将数据存入一个session中给同一用户的不同页面使用

首先我们先实现一个防止用户非法登录的功能

看代码

首先是实现一个简单的登录界面

然后是判断登录是否成功的部分(为了方便先不连接数据库,密码为123就能登录成功)

复制代码
import ...
@WebServlet("/LoginJudge")
public class LoginJudge extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        resp.setContentType("text/html;charset=UTF-8");
        try {
            PrintWriter out = resp.getWriter();

            String na = req.getParameter("put_name");
            String pw = req.getParameter("put_password");
            if (pw.equals("123")){

                //获得session空间
                HttpSession hs = req.getSession(true);
                //修改session的存在时间为30秒
                hs.setMaxInactiveInterval(30);
                //设置session 名为pass 值为ok
                hs.setAttribute("pass","ok");

                resp.sendRedirect("User1?lg_name="+na);
            }else {
                out.println("error");
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
复制代码

然后是登录成功的界面

复制代码
import ...;

@WebServlet("/User1")
public class User1 extends HttpServlet {

  

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

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        resp.setContentType("text/html;charset=UTF-8");
        PrintWriter out = resp.getWriter();
        String name =req.getParameter("lg_name");
        //得到session
        HttpSession hs = req.getSession(true);
        String value = (String) hs.getAttribute("pass");
        //判断
        if (value==null){
            try {
                //非法登录
                resp.sendRedirect("Login");
            }catch (Exception e){
                e.printStackTrace();
            }
        }
        try {

            String title1 = "登录完成 ";

            out.println("<!DOCTYPE html> \n" +
                        "<html>\n" +
                        "<head><title>" + title1 + "</title></head>\n" +
                        "<body bgcolor=\"#f0f0f0\">\n" +
                        "<h1 align=\"center\">" + title1 + "</h1>\n" +
                        "<h3 align=\"center\">" + "你的用户名是 " + name + "</h3>\n" +
                        "</body></html>"
            );
        }catch (Exception e){
            e.printStackTrace();
        }

    }

}
复制代码

重点部分写了注释

我们如果强行把url 改为User1,会被强制返回到登录页面,实现了禁止非法访问的功能

 

作者:冰稀饭Aurora

出处:https://www.cnblogs.com/rsy-bxf150/p/17165877.html

版权:本作品采用「署名-非商业性使用-相同方式共享 4.0 国际」许可协议进行许可。

posted @   冰稀饭Aurora  阅读(223)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
more_horiz
keyboard_arrow_up dark_mode palette
选择主题
点击右上角即可分享
微信分享提示