1. 程式人生 > >JavaWeb-form傳值(從一個jsp頁面傳資料到另一個jsp頁面)

JavaWeb-form傳值(從一個jsp頁面傳資料到另一個jsp頁面)

第一個頁面,login.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%-- 本行程式碼可以防止亂碼出現 --%>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://"
			+ request.getServerName() + ":" + request.getServerPort()
			+ path + "/";

	
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">

<title>login</title>

<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">

</head>

<body>

<!--傳值到"login_action.jsp"  方式是post隱式傳遞。-->
	<form action="login_action.jsp" method="post">
	使用者名稱:<input type="text" name="username"><br /> 
        密碼:<input type="password" name="password"><br /> 
            <input type="submit"  value="登入">

	</form>

</body>
</html>

第二個頁面 "login_action.jsp"

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://"
			+ request.getServerName() + ":" + request.getServerPort()
			+ path + "/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">

<title>My JSP 'login_action.jsp' starting page</title>

<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->

</head>

<body>
    <%--  使用此方法即可獲取名為"xxxx"的屬性的值 --%>
    使用者名稱:<%=request.getParameter("username")%><br> 
    密碼:<%=request.getParameter("password")%>
</body>
</html>

瀏覽器輸入http://localhost:8080/FridayJob/login/login.jsp

得到

可以看到自動跳轉到"login_action.jsp"頁面