<%@ 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> <% String name=null; Cookie[] cks = request.getCookies() ;//獲得cookie的集合 if(cks != null) {
// 如果已經設置了cookie , 則得到它的值,將該值放在登入名文本框的value中
//遍歷
for(Cookie c:cks)
{
if(c.getName().equals("name"))
name = c.getValue();
}
} %> <form action="ShoppCheck.jsp" method="post"> 用戶名:<input type="text"name="name" value="<%=name%>"><br> 密碼:    <input type= "password"name="password"><br> <input type="submit" value="登入"> </form> </body> </html>
<%@page import="com.hanqi.web.ShoppDAO"%>
<%@ 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>
<%
//設置不緩存頁面
//response.setHeader("Cache_Control", "no-cache");
//接受數據
String name =request.getParameter("name");
String password =request.getParameter("password");
if(name==null || password == null||name.equals("")||password.equals(""))
{
//排除用戶名和密碼為null或 為空字符串的情況
out.write("請正確登錄系統");
}
else
{
//檢查登錄信息
ShoppDAO sd= new ShoppDAO ();//導包
if(sd.checkLogin(name, password))
{
//out.write("登錄成功");
//無緩存的直接發送
//response.getWriter().write("登入成功");
//創建cookie
Cookie ck= new Cookie("name", name);
//設置過期時間
ck.setMaxAge(10*24*60*60);
//發送
response.addCookie(ck);
//創建session
session.setAttribute("name", name);
//頁面跳轉
response.sendRedirect("xtMain.jsp");
}
else
{
out.write("登入失敗");
//2秒后跳回登入頁面
response.setHeader("refresh", "2;URL=shoppMain.jsp");
}
}
%>
</body>
</html>
<%@page import="java.net.URLDecoder"%> <%@ 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> 系統主頁面 <br> <% //檢查cookie //獲得cookie集合 Cookie[] cks= request.getCookies(); for(Cookie ck: cks) { //解碼 out.write(ck.getName()+"="+URLDecoder.decode(ck.getValue()) +"<br>"); } //判斷session
Object yonghuming = session.getAttribute("name");
if(yonghuming==null)
{
out.print("回話超時或為登入");
response.setHeader("refresh", "2;URL=shoppMain.jsp");
}
else
{
//out.print("name="+yonghuming);
out.print(yonghuming+",歡迎登入");
%>
<br>
<a href="tuichu.jsp">退出登入</a>
<%
}
%>
</body> </html>
<%@ 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> 系統將退出。。。。。
<%
//銷毀
session.invalidate();
out.print("退出成功");
response.setHeader("refresh", "6;URL=shoppMain.jsp");
%>
</body> </html>
如果兩秒鍾未任何操作,就會被退出,並自動返回主界面,防止利用URL訪問網站。
注意:在JSP中,所有的架包要放在WEB_INF下的lib 文件夾下,如圖: