HttpSession案例笔记
来源:互联网 发布:好吃的零食 知乎 编辑:程序博客网 时间:2024/05/18 20:09
login.jsp
<body> <%--本页面提供登陆表单,还要显示错误信息 --%> <h1>登陆</h1><% /* 读取名为name的Cookie 如果为空显示:"" 如果不为空:Cookie的值 */ String uname="";Cookie[] cs = request.getCookies();//注意:判断null,否则空指针if(cs!=null){ //遍历 for(Cookie c:cs){ if("uname".equals(c.getName())){ uname = c.getValue(); } }} %> <% String message=""; String msg=(String)request.getAttribute("msg"); //获取request域中的名为msg的属性 if(msg!=null){ message=msg; } %> <font color="red"> <b><%=message %></b> </font> <form action="/day11_2/LoginServlet" method="post"> 用户名:<input type="text" name="username" value="<%=uname %>"/><br/> 密码:<input type="text" name="password"/><br/> <input type="submit" value="登陆"/> </form> </body></html>LoginServlet.java
public class LoginServlet extends HttpServlet {public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {/* * 获取表单数据 * 处理中文问题 */request.setCharacterEncoding("UTF-8");// 1.获取:从request中获取String username=request.getParameter("username");String password=request.getParameter("password");//校验用户名和密码是否正确if(!"itcast".equalsIgnoreCase(username)){ //登陆成功/* * 1附加项:把用户保存到cookie中,发送给客户端浏览器 * 2当再次打开login.jsp时,login.jsp中会读取request中的cookie, * 把它显示到用户名文本框 */Cookie cookie=new Cookie("uname", username);//创建Cookiecookie.setMaxAge(60*60*24); //设置cookie命长1天response.addCookie(cookie);//保存cookie/* * 如果成功 * 1.保存用户信息到session中 * 2.重定向succ1.jsp */HttpSession session=request.getSession();//获取sessionsession.setAttribute("username", username);//向session域中保存用户名response.sendRedirect("/day11_2/session2/succ1.jsp");}else{ //如果失败/* * 1。保存错误信息到request域中 * 2.转发到login.jsp中 */request.setAttribute("msg", "用户或密码错误");RequestDispatcher qr=request.getRequestDispatcher("/session2/login.jsp");//得到转发器qr.forward(request, response); //转发}}}succ1.jsp
<body><h1>succ1</h1><%String username=(String)session.getAttribute("username");if(username==null){/*1。向request域中保存错误信息。转发到login.jsp*/request.setAttribute("msg", "您还没有登陆,不要冒充");request.getRequestDispatcher("/session/login.jsp");return;}%>欢迎用户<%=session.getAttribute("username") %> </body></html>succ2.jsp
<body> <h1>succ2</h1><%String username=(String)session.getAttribute("username");if(username==null){/*1。向request域中保存错误信息。转发到login.jsp*/request.setAttribute("msg", "您还没有登陆,不要冒充");request.getRequestDispatcher("/session2/login.jsp").forward(request, response);return;}%>欢迎用户<%=session.getAttribute("username") %> </body></html>
阅读全文
1 0
- HttpSession案例笔记
- HttpSession笔记
- HttpSessionListener案例: 自定义HttpSession扫描器
- Cookie HttpSession 学习笔记
- JavaWeb(九)HttpSession和登陆案例
- HttpSession
- HttpSession
- HttpSession
- httpsession
- HttpSession
- HttpSession
- HttpSession
- HttpSession
- HttpSession
- HTTPSession
- HttpSession
- HttpSession
- HttpSession
- c结构体
- Android使用JobService实现后台服务
- Android系统之@SuppressWarnings的用途
- 有效回文串
- POJ 3279(Fliptile)题解
- HttpSession案例笔记
- Android判断App是否在前台运行
- spring面试题
- orientdb 第一次启动设置密码
- H
- Android使用SoundPool播放短音效
- 视觉资源整理
- QtCreator最常用快捷键
- Android 自定义View实现圆形进度条 深入理解onDraw和onMeasure及自定义属性