jsp之間數據傳遞

1.       怎麼在多個JSP頁面之間進行參數傳遞?需要使用JSP的內置作用域對象session。利用它的兩個方法setAttribute(),getAttribute()

2.       下面的這個實例實現了把第一個JSP頁面的參數傳遞給第三個頁面的功能

3.       代碼如下:1.jsp

<html>
       <form method=get action=2.jsp>
       what’s your name<input type=text name=username>
       <input type=submit value=submit>
       </form>
</html>

4.       2.jsp
<html>
       <form method=post action=”3.jsp?pass=11″>
       <%
              String name=request.getParameter(”username”);
              session.setAttribute(”username”,name);
       %>
       Your name is:<%=request.getParameter(”username”)%>
       <br>what’s your hobby<input type=text name=hobby>
       <input type=submit value=submit>
       </form>
</html>

5.       3.jsp

</pre><p></p><p style="margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; font-family: Helvetica, Tahoma, Arial, sans-serif; font-size: 14px; line-height: 25.2000007629395px;"><span style="color: rgb(0, 0, 255); font-size: 12px;"><span style="color: rgb(51, 51, 0);"><html>       your name is:<%=session.getAttribute(”username”)%>       <br>       your hobby is:<%=request.getParameter(”hobby”)%>       <br>       your password is:<%=request.getParameter(”pass”)%>       <br>       </form></html></span></span></p><p style="margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; font-family: Helvetica, Tahoma, Arial, sans-serif; font-size: 14px; line-height: 25.2000007629395px;"><span style="color: rgb(0, 0, 255); font-size: 12px;"><span style="color: rgb(51, 51, 0);"></span></span></p><p style="margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; font-family: Helvetica, Tahoma, Arial, sans-serif; font-size: 14px; line-height: 25.2000007629395px;">以下是jsp跟servlet之間數據的傳送</p><p style="margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; font-family: Helvetica, Tahoma, Arial, sans-serif; font-size: 14px; line-height: 25.2000007629395px;"></p><pre name="code" class="html" style="color: rgb(51, 51, 0); line-height: 25.2000007629395px;"><%@ 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 action="shenfenChuli" method="post">
<input type="text" name="id"/>
<input type="submit" value="提交"/>
</form>
</body>
</html>
package com.LiDi.xhj;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class ShenfenChuli
 */
@WebServlet("/shenfenChuli")
public class ShenfenChuli extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String id=request.getParameter("id");
		int len=0;
		if (id.length()==15) {
			len=15;
		}else if(id.length()==18){
			len=18;
		}else{
			len=-1;
		}
		request.setAttribute("b", len);
		request.setAttribute("a", id);
		String path="One.jsp";
		RequestDispatcher rd=request.getRequestDispatcher("/"+path);
		rd.forward(request, response);
		
	}

}
<%@ 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>
<%-- <jsp:useBean id="sfz" class="com.LiDi.xhj.ShenfenChuli" scope="session"></jsp:useBean>
<jsp:param >
 --%>
<%
int len;
String str=request.getAttribute("a").toString();
len=Integer.parseInt(request.getAttribute("b").toString());
String str5;
if(len==18){
	String str1=str.substring(6, 14);
	String str2=str1.substring(0, 4);
	String str3=str1.substring(4, 6);
	String str4=str1.substring(6, 8);
	str5=str2+"/"+str3+"/"+str4;
}else{
	out.print("輸入的身份證號非法");
	return;
}
%>
<table width="322" border="0">
  <tr align="center" bgcolor="#66FF00">
    <td width="161">身份證</td>
    <td width="151">出生日期</td>
  </tr>
  <tr>
    <td>
 <%
 out.print(str);
 %>
 </td>
    <td>
  <%
  out.print(str5);
  %>
  </td>
  </tr>
</table>
</body>
</html>



發佈了21 篇原創文章 · 獲贊 28 · 訪問量 2萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章