session測試

奇怪的知识增加了發表於2024-10-19

jsp1

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>session測試</title>
</head>

<body>

<form method="post" name="form">
    <table>
       <tr>
       		<td>
       			使用者名稱:<input type="text" name="user">
       		</td>
       </tr>
    </table>
</form>

<%
	String user=request.getParameter("user");
	session.setAttribute("user",user);
%>
</body>
</html>

jsp2

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8" import="java.lang.Math"%>

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>admin</title>
</head>
<body>
<form method="post" name="form">
    <table>
       <tr>
       		<td>
       			物品名稱:<input type="text" name="user">
       		</td>
       </tr>
    </table>
<%
	String user=request.getParameter("user");
	session.setAttribute("name",user);
%>
</form>
</body>
</html>

jsp3

<%@ page language="java" contentType="text/html; charset=UTF-8"
   pageEncoding="UTF-8" import="java.lang.Math"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>static</title>
</head>
<body>
<form method="post" name="form">
   <table>
      <tr>
      		<td>
      			<input type="submit" name="submit" value="下單">
      		</td>
      </tr>
   </table>

使用者名稱:<%=session.getAttribute("user") %><br>
商品名稱:<%=session.getAttribute("name") %>
</body>
</html>

三個頁面為獨立的頁面,彼此不傳遞資料
1和2都將資料儲存到session中
3讀取session中的資料

執行截圖
1

2

3

相關文章