您的位置:首页 > Web前端 > JavaScript

JSP用户注册登陆注销JSP页面

2015-08-28 19:30 836 查看
一:JSP登陆页 login.jsp:
<%@ page contentType="text/html" pageEncoding="UTF-8" %>
<html>
 <head>
  <meta http-equiv="Context-Type" content="text/html; charset=UTF-8" />
  <title>登录页 - Java教程:http://www.javaweb.cc</title>
 </head>
 <body>
  <form action="login.jsp" method="post">
   用户名:<input type="text" name="uname" /><br />
   密  码:<input type="password" name="upass" /></br />
   <input type="submit" value=" 登 录 "  />
   <input type="reset" value=" 重 置 " />
  </form>
  <%
   // 用户名:admin 密码:123
   String name = request.getParameter("uname"); // 取得 name 的信息
   String password = request.getParameter("upass"); // 取得 password 的信息
   if(!(name == null || "".equals(name) || password == null || "".equals(password)))  // 进行用户名和密码的验证
   {
    response.setHeader("refresh", "2; URL = welcome.jsp");  // 定时跳转
    session.setAttribute("userid", name);  // 登录成功的用户名保存在 session 中
  %>
    <h3>用户登陆成功,两秒钟后跳转到欢迎页面!</h3>
    <h3>如果没有自动跳转,请按<a href="welcome.jsp">这里</a></h3>
  <%
   }
   else
   {
  %>
    <h3>错误的用户名或密码!</h3>
  <%
   }
  %>
 </body>
</html>


二、JSP登录成功页(欢迎页)welcome.jsp:
<%@ page contentType="text/html" pageEncoding="UTF-8" %>
<html>
 <head>
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
  <title>登陆欢迎页 - Java中文网:http://www.javaweb.cc</title>
 </head>
 <body>
  <%
   if(session.getAttribute("userid") != null) // 已经设置过的属性,所有不为空
   {
  %>
    <h3>欢迎 <%= session.getAttribute("userid") %> 光临本站,您的 SessionID 为:<%= session.getId() %> <a href="logout.jsp">注销</a>!</h3>
  <%
   }
   else  // 非法用户,没有登陆通过,则 session 范围内没有属性存在
   {
  %>
    <h3>请您先<a href="login.jsp">登录</a>!</h3>
  <%
   }
  %>
 </body>
</html>


3、JSP注销页 logout.jsp:
<%@ page contentType="text/html" pageEncoding="UTF-8" %>
<html>
 <head> 
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
  <title>注销页面 - J***AWEB.CC</title>
 </head>
 <body> 
  注销前 SessionID 为:<%= session.getId() %> <br /> 属性为:<%= session.getAttribute("userid") %> <br />
  <%
   response.setHeader("refresh", "2; URL = login.jsp");  // 定时跳转
   session.invalidate(); // 注销 session 
  %>
  注销后 SessionID 为:<%= session.getId() %> 属性为: <br />
  <h3>您已成功推出本系统,两秒钟后跳转到登陆页</h3>
  <h3>如果没有自动跳转,请点击<a href="login.jsp">这里</a></h3>
 </body>
</html>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: