登录-注销-注册



1. [代码]登录-注销jsp源码     

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<%
     //获取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>
         <%
     }
   %>

2. [代码]注销Servlet     

?
1
2
3
4
5
6
7
8
9
10
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" );
}

3. [代码]登录jsp     

?
1
2
3
4
5
<form action= "${pageContext.request.contextPath }/servlet/LoginServlet" method= "POST" >
     用户名:<input type= "text" name= "username" />
     密码:<input type= "password" name= "password" />
     <input type= "submit" value= "登录" />
</form>

4. [代码]登录Servlet     

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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( "用户名密码不正确!" );
     }
}

5. [代码]用户Dao包     

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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);
     }
}

6. [代码]注册jsp     

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
     
     <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>

7. [代码]注册Servlet     

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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:不要重复提交!!" );
     }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值