20160330javaweb之session 小練習

破玉發表於2016-03-30

練習一: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>

  

相關文章