练习一:session 实现登录注销
package com.dzq.session.logout;import java.util.*;public class UserDao { /** * 存储用户信息,代替数据库 */private UserDao(){ }private static Mapmap=new HashMap ();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"%>Insert title here 我的网站
<%String user=(String)session.getAttribute("user");%><%if(user==null||"".equals(user)){ %> 欢迎光临,游客.... 登录 注册 <% }else{ %> 欢迎回来,<%=user %> 注销 <%}%>
我。。。。。。。。。。。。。。是。。。。。。。。。。。。分。。。。。。。。。。割。。。。。。。。。。线。。。。。。。。。。。。
我 的 下 面 还 是 jsp 页 面
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>Insert title here 我的网站
练习二: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"%>Insert title here <% Random r=new Random();int valinum=r.nextInt();session.setAttribute("valinum", valinum+"");%>