JSP实现数据保存

本文介绍了使用JSP进行会话管理的方法,包括利用session保存用户名、使用cookie自动填写用户名等,并展示了如何通过实例代码实现这些功能。此外还讨论了application对象作为计数器的应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、概述

164926_WQi5_2320342.png

2、使用session保存用户名

165402_pHuM_2320342.png

session工作方式:

171108_8lej_2320342.jpeg

会话的清除与过期:

171552_uhmy_2320342.jpeg

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自动填写用户名

110355_ILhC_2320342.png

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

130539_BqvZ_2320342.jpeg

130728_ckxd_2320342.png

cookie文件中的内容:

user
qwe
localhost/news/jsp/
1024
1578087680
30438420
1482467680
30438420
*
4、application实现计数器

165312_9hAz_2320342.png

<%@ 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、三个对象的对比

171808_TAd1_2320342.png

172233_yWBW_2320342.jpeg

172454_bLaf_2320342.jpeg

6、jsp页面的组成部分

173119_bb4w_2320342.jpeg

7、常用内置对象

173409_p52h_2320342.png

8、数据保存

173720_ZpEj_2320342.jpeg

9、客户端请求新页面

174043_uZ1M_2320342.jpeg

175357_PGGM_2320342.png

175507_l953_2320342.png

180139_Tq9A_2320342.png

注:修改οnclick="return fun();"

180300_JQD0_2320342.png

180816_nLGp_2320342.png

181308_YYR8_2320342.png

 181651_N1o1_2320342.png

 10、处理中文乱码

182018_PyoQ_2320342.png

183206_726L_2320342.png

183328_gj0x_2320342.png

183509_zt3I_2320342.png

转载于:https://my.oschina.net/u/2320342/blog/398660

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值