登录-注销-注册

2019PHP高薪工程师学习路线图....>>>

<无详细内容>
  	<%
  		//获取session中的登录状态
		String user = (String)session.getAttribute("user");
  	 %>
  	 <%
  	 	if(user == null || "".equals(user)){//用户没有登录
  	 		%>
  	 			欢迎光临!游客!
  	 			<a href="${pageContext.request.contextPath }/loginout/login.jsp">登录</a>
  	 			<a href="#">注册</a>
  	 		<%
  	 	}else{//用户登录过
  	 		%>
  	 			欢迎回来!<%=user %>!
  	 			<a href="${pageContext.request.contextPath }/servlet/LogoutServlet">注销</a>
  	 		<%
  	 	}
  	  %>
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		//1.杀死session
		if(request.getSession(false)!=null 
				&& request.getSession().getAttribute("user")!=null){
			request.getSession().invalidate();
		}
		//2.重定向到主页
		response.sendRedirect(request.getContextPath()+"/loginout/index.jsp");
	}
  	<form action="${pageContext.request.contextPath }/servlet/LoginServlet" method="POST">
  		用户名:<input type="text" name="username"/>
  		密码:<input type="password" name="password"/>
  		<input type="submit" value="登录"/>
  	</form>
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		//1.获取用户名密码
		String username = request.getParameter("username");
		String password = request.getParameter("password");
		//2.查询数据库检查用户名密码
		if(UserDao.valiNamePsw(username, password)){
			//3.如果正确登录后重定向到主页
			request.getSession().setAttribute("user", username);
			response.sendRedirect(request.getContextPath()+"/loginout/index.jsp");
			return;
		}else{
			//4.如果错误提示
			response.getWriter().write("用户名密码不正确!");
		}
	}
public class UserDao {
	private UserDao() {
	}
	private static Map <String,String>map = new HashMap<String, String>();
	static{
		map.put("111", "111");
		map.put("222", "222");
		map.put("333", "222");

	}
	public static boolean valiNamePsw(String username,String password){
		return map.containsKey(username) && map.get(username).equals(password);
	}
}
  	
  	<script type="text/javascript">
  		var isNotSub = true;//表单是否已经提交标识,默认为false
  		function canSub(){
  			if(isNotSub){
  				isNotSub = false;//提交表单后,将表单是否已经提交标识设置为true
  				return true;//返回true让表单正常提交
  			}else{
  				alert("请不要重复提交!!!");
  				return false;//返回false那么表单将不提交
  			}
  		}
  	</script>


  <%
  	Random r = new Random();
  	int valinum = r.nextInt();
  	session.setAttribute("valinum",valinum+"");
   %>

<form action="${pageContext.request.contextPath }/servlet/ResubServlet" method="POST" onsubmit="return canSub()">
  	用户名:<input type="text" name="username"/>
  	<input type="hidden" name="valinum" value="<%=valinum %>"/>
  	<input type="submit" value="注册"/>
</form>
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		try {
			Thread.sleep(4*1000);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			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("from web:不要重复提交!!");
		}
	}