JSP實現數據保存

xg48 9年前發布 | 22K 次閱讀 JSP 前端技術

1、概述

JSP實現數據保存

2、使用session保存用戶名

JSP實現數據保存

session工作方式:

JSP實現數據保存

會話的清除與過期:

JSP實現數據保存

createUser.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>
 <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.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.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:

<%@ 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:

<%@ 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>

3、使用cookie自動填寫用戶名

JSP實現數據保存

doCreateUser.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.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文件:

JSP實現數據保存

JSP實現數據保存

cookie文件中的內容:

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

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>
 <%
  }
 %>
 <%
  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>

5、三個對象的對比

JSP實現數據保存

JSP實現數據保存

JSP實現數據保存

6、jsp頁面的組成部分

JSP實現數據保存

7、常用內置對象

JSP實現數據保存

8、數據保存

JSP實現數據保存

9、客戶端請求新頁面

JSP實現數據保存

JSP實現數據保存

JSP實現數據保存

JSP實現數據保存

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

JSP實現數據保存

JSP實現數據保存

JSP實現數據保存

 JSP實現數據保存

 10、處理中文亂碼

JSP實現數據保存

JSP實現數據保存

JSP實現數據保存

JSP實現數據保存

來自:http://my.oschina.net/u/2320342/blog/398660

 本文由用戶 xg48 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!