练习一:session 实现登录注销
package com.dzq.session.logout; import java.util.*; public class UserDao { /** * 存储用户信息,代替数据库 */ private UserDao(){ } private static Map<String, String> map=new HashMap<String, String>(); static{ map.put("张三丰", "111"); map.put("Adele", "111"); map.put("小杜", "111"); } public static boolean valiNP(String username,String password){ return map.containsKey(username)&&map.get(username).equals(password); } }
我。。。。。。。。。。。。。。。。。。是。。。。。。。。。。。。分。。。。。。。。。。割。。。。。。。。。。线。。。。。。。。。。。。。。
package com.dzq.session.logout; 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; @WebServlet("/LoginServlet") public class LoginServlet extends HttpServlet { /** * 实现登录的servlet,登陆后重定向到主页index。jsp */ private static final long serialVersionUID = 1L; protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("utf-8"); response.setContentType("text/html;charset=utf-8"); String username=request.getParameter("username"); String password=request.getParameter("password"); if(UserDao.valiNP(username, password)){ request.getSession().setAttribute("user", username); response.sendRedirect(request.getContextPath()+"/logout/index.jsp"); }else{ response.getWriter().write("用户名或者密码错误"); } } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
我。。。。。。。。。。。。。。。。是。。。。。。。。。。。。分。。。。。。。。。。割。。。。。。。。。。线。。。。。。。。。。。。
package com.dzq.session.logout; 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; @WebServlet("/LogoutServlet") public class LogoutServlet extends HttpServlet { private static final long serialVersionUID = 1L; /** * 注销登录的servlet ,销毁session ,重定向到index。jsp */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { if(request.getSession(false)!=null&&request.getSession().getAttribute("user")!=null){ request.getSession().invalidate(); } response.sendRedirect(request.getContextPath()+"/logout/index.jsp"); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
我。。。。。。。。。。。。。。是。。。。。。。。。。。。分。。。。。。。。。。割。。。。。。。。。。线。。。。。。。。。。。
我 的 下 面 是 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> <!-- 主页index.jsp --> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <h1>我的网站</h1><hr> <% String user=(String)session.getAttribute("user"); %> <% if(user==null||"".equals(user)){ %> 欢迎光临,游客.... <a href="login.jsp">登录</a> <a href="#">注册</a> <% }else{ %> 欢迎回来,<%=user %> <a href="${pageContext.request.contextPath }/LogoutServlet">注销</a> <% } %> </body> </html>
我。。。。。。。。。。。。。。是。。。。。。。。。。。。分。。。。。。。。。。割。。。。。。。。。。线。。。。。。。。。。。。
我 的 下 面 还 是 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> <h1>我的网站</h1><hr> <form action="${pageContext.request.contextPath }/LoginServlet" method="post"> 用户名:<input type="text" name="username" /> 密码:<input type="password" name="password"/> <input type="submit" value="登录"/> </form> </body> </html>
练习二:session 实现防止表单重复提交:
package com.dzq.session.resubmit; 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; @WebServlet("/ResubServlet") public class ResubServlet extends HttpServlet { private static final long serialVersionUID = 1L; /** * 获取session 防止重复提交 */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("utf-8"); response.setContentType("text/html;charset=utf-8"); try { Thread.sleep(4*1000); } catch (Exception e) { e.printStackTrace(); } String username=request.getParameter("username"); String valinum=request.getParameter("valinum"); String valinum2=(String) request.getSession().getAttribute("valinum"); if(valinum2!=null&& !"".equals(valinum2)&&valinum.equals(valinum2)){ request.getSession().removeAttribute("valinum"); System.out.println("向数据库中注册一次"+username); }else{ response.getWriter().write("web不要重复提交"); } } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
我。。。。。。。。。。。。。。是。。。。。。。。分。。。。。。。。割。。。。。。。。。。线。。。。。。。。。。。。
我 的 下 面 是 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> <!-- <script type="text/javascript"> var isNotSub=true; function canSub() { if(isNotSub){ isNotSub=false; return true; }else{ alert("请不要重复提交"); return false; } } </script> --> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <% Random r=new Random(); int valinum=r.nextInt(); session.setAttribute("valinum", valinum+""); %> <form action="${pageContext.request.contextPath }/ResubServlet" method="post" onsubmit="return canSub()"> <input type="text" name="username"/> <input type="hidden" name="valinum" value="<%=valinum%>"/> <input type="submit" value="提交"/> </form> </body> </html>