登录界面实现通过学号登陆
数据库账户为单独的表account
其中有三个字段:account(账号/学号),password(密码),type(用户类型)
在servlet中将账户添加到cookie,并设置全局使用
String account = req.getParameter("account"); String password = req.getParameter("password"); String type = req.getParameter("type"); Cookie account1 = new Cookie("account", account); Cookie password1 = new Cookie("password", password); Cookie type1 = new Cookie("type", type); account1.setMaxAge(24 * 60 * 60); password1.setMaxAge(24 * 60 * 60); type1.setMaxAge(24 * 60 * 60); account1.setPath("/"); password1.setPath("/"); type1.setPath("/"); resp.addCookie(account1); resp.addCookie(password1); resp.addCookie(type1);
在其他servlet或jsp中调用时,只需
Cookie[] cookies = request.getCookies(); String acc = ""; String pass = ""; String type = ""; if(cookies!=null) for (int d = 0; d <= cookies.length - 1; d++) { if(cookies[d].getName().equals("account")) acc = cookies[d].getValue(); if(cookies[d].getName().equals("password")) pass = cookies[d].getValue(); if(cookies[d].getName().equals("type")) type = cookies[d].getValue(); }
即可全局使用。
本文作者:又一岁荣枯
本文链接:https://www.cnblogs.com/java-six/p/15610896.html
版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步