JSP實現數據保存

一、概述html

二、使用session保存用戶名java

session工做方式:cookie

會話的清除與過時:session

createUser.jsp:app

<%<%@ 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>
 <form id="" action="doCreateUser.jsp" method="post">
  用戶名:<input type="text" name="userName"/>
  <input type="submit" value="提交"/>
 </form>
 <%
  // 取回提示信息
  Object oMess = request.getAttribute("mess");
  if (oMess != null) {
   out.print(oMess.toString());
  }
 %>
</body>
</html>

doCreateUser.jspjsp

 <%@ 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>
 <%
  request.setCharacterEncoding("UTF-8");
  String userName = request.getParameter("userName");
  //out.print(userName);
  
  if (userName.equals("admin")) {
   // 加入提示信息
   request.setAttribute("mess","註冊失敗,更換用戶名。");
   request.getRequestDispatcher("createUser.jsp").forward(request, response);
   //response.sendRedirect("createUser.jsp");
  } else {
   session.setAttribute("user",userName);
   //request.getRequestDispatcher("default.jsp").forward(request, response);
   response.sendRedirect("default.jsp");
  }
 %>
</body>
</html>

default.jsp:post

<%@ 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>
 <%-- <%=(request.getAttribute("mess")).toString() %> --%>
 <% 
  Object o = session.getAttribute("user");
  if (o == null) {
 %>
  <form id="" action="doCreateUser.jsp" method="post">
  用戶名:<input type="text" name="userName"/>
  <input type="submit" value="提交"/>
 </form>
 <% 
  } else {
   out.print("歡迎你," + o.toString());
 %>
  <a href="doLoginOut.jsp">註銷</a>
 <%
  }
 %>
</body>
</html>

doLoginOut.jsp:ui

<%@ 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.removeAttribute("user");// 釋放session
  //session.invalidate();
  response.sendRedirect("default.jsp");
 %>
</body>
</html>

三、使用cookie自動填寫用戶名spa

doCreateUser.jsp:code

<%@ 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>
 <%
  request.setCharacterEncoding("UTF-8");
  String userName = request.getParameter("userName");
  //out.print(userName);
  
  if (userName.equals("admin")) {
   // 加入提示信息
   request.setAttribute("mess","註冊失敗,更換用戶名。");
   request.getRequestDispatcher("createUser.jsp").forward(request, response);
   //response.sendRedirect("createUser.jsp");
  } else {
   // 建立cookie
   Cookie cookie = new Cookie("user",userName);
   // 設置cookie的有效期,單位秒
   cookie.setMaxAge(10);
   response.addCookie(cookie);
   
   session.setAttribute("user",userName);
   //request.getRequestDispatcher("default.jsp").forward(request, response);
   response.sendRedirect("default.jsp");
  }
 %>
</body>
</html>

default.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>
 <%-- <%=(request.getAttribute("mess")).toString() %> --%>
 <% 
  // 獲取cookie
  Cookie[] cookies = request.getCookies();
  String user = "";
  for (int i = 0 ; i < cookies.length ; i++) {
   if (cookies[i].getName().equals("user")) {
    user = cookies[i].getValue();
   } 
  }
 
  Object o = session.getAttribute("user");
  if (o == null) {
 %>
  <form id="" action="doCreateUser.jsp" method="post">
  用戶名:<input type="text" name="userName" value="<%=user%>"/>
  <input type="submit" value="提交"/>
 </form>
 <% 
  } else {
   out.print("歡迎你," + o.toString());
 %>
  <a href="doLoginOut.jsp">註銷</a>
 <%
  }
 %>
</body>
</html>

 查看cookie文件:

cookie文件中的內容:

user
qwe
localhost/news/jsp/
1024
1578087680
30438420
1482467680
30438420
*
四、application實現計數器

<%@ 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>
 <%-- <%=(request.getAttribute("mess")).toString() %> --%>
 <% 
  // 獲取cookie
  Cookie[] cookies = request.getCookies();
  String user = "";
  for (int i = 0 ; i < cookies.length ; i++) {
   if (cookies[i].getName().equals("user")) {
    user = cookies[i].getValue();
   } 
  }
 
  Object o = session.getAttribute("user");
  if (o == null) {
 %>
  <form id="" action="doCreateUser.jsp" method="post">
  用戶名:<input type="text" name="userName" value="<%=user%>"/>
  <input type="submit" value="提交"/>
 </form>
 <% 
  } else {
   out.print("歡迎你," + o.toString());
 %>
  <a href="doLoginOut.jsp">註銷</a>
 <%
  }
 %>
 <%
  Object count = application.getAttribute("count");
  if (count == null) {
   // 第一次訪問
   application.setAttribute("count", new Integer(1));
  } else {
   Integer i = (Integer)count;
   // 每一次訪問+1
   application.setAttribute("count", i.intValue() + 1);
  }
  Integer icount = (Integer)application.getAttribute("count");
  out.print("頁面被訪問了" + icount.intValue() + "次");
 %>
</body>
</html>

五、三個對象的對比

六、jsp頁面的組成部分

七、經常使用內置對象

八、數據保存

九、客戶端請求新頁面

注:修改onclick="return fun();"

 

 十、處理中文亂碼

相關文章
相關標籤/搜索