jsp页面,数据传递四种方式

1》通过cookie传递数据,页面ab.jsp中的数据,使用cookie来传递到ac.jsp中

第一步:需要在工程中导入    servlet.api.jar架包。

第二步:引入     <%@ page import="javax.servlet.http.Cookie"%>

ab.jsp

 1 <%@ page language="java" contentType="text/html; charset=utf-8"
 2     pageEncoding="utf-8"%>
 3 <%@ page import="java.util.*" %>
 4 <%@ page import="javax.servlet.http.Cookie" %>
 5 
 6 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 7 
 8 <html>
 9 <head>
10 <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
11 <title>Insert title here</title>
12 </head>
13 <body>
14     <%
15         String str = "12";
16         int number = Integer.parseInt(str);
17         //将str 存入cookie
18         Cookie cookie = new Cookie("number",str);
19         cookie.setMaxAge(600);
20         response.addCookie(cookie);
21     %>
22     <a href="ac.jsp">链接</a>
23 
24 </body>
25 </html>

ac.jsp

 1 <%@ page language="java" contentType="text/html; charset=utf-8"
 2     pageEncoding="utf-8"%>
 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 4 <html>
 5 <head>
 6 <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
 7 <title>Insert title here</title>
 8 </head>
 9 <body>
10 <%String str=null;
11 Cookie[] cookies = request.getCookies();
12 for(int i=0;i<cookies.length;i++){
13     if(cookies[i].getName().equals("number")){
14         str = cookies[i].getValue();
15         break;
16     }
17 }
18 
19 
20 int a = Integer.parseInt(str);
21 %>
22 该数值的平方是:<%=a*a %>
23 </body>
24 </html>

2》通过url模式来传递数据,a1.jsp内容传到a2.jsp中去

使用方法为:页面2的路径?参数名1=参数值1&参数名2=参数值2&....

a1.jsp

 1 <%@ page language="java" contentType="text/html; charset=utf-8"
 2     pageEncoding="utf-8"%>
 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 4 <html>
 5 <head>
 6 <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
 7 <title>Insert title here</title>
 8 </head>
 9 <body>
10 <%
11     String a = "12";
12     int b = Integer.parseInt(a);
13 %>
14 <%=b %>
15 <a href="a2.jsp?b=<%=b %>">前往a2.jsp页面</a>
16 </body>
17 </html>

a2.jsp

 1 <%@ page language="java" contentType="text/html; charset=utf-8"
 2     pageEncoding="utf-8"%>
 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 4 <html>
 5 <head>
 6 <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
 7 <title>Insert title here</title>
 8 </head>
 9 <body>
10 <% String str = request.getParameter("b");
11    int c = Integer.parseInt(str);
12 %>
13 <%=c %>
14 </body>
15 </html>


3》通过表单进行传递

能够较为熟练的使用,略

4》使用session对象进行传递数据,使用session的方法将assesion.jsp的数据传递到assesion1.jsp去assesion.jsp

 1 <%@ page language="java" contentType="text/html; charset=utf-8"
 2     pageEncoding="utf-8"%>
 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 4 <html>
 5 <head>
 6 <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
 7 <title>Insert title here</title>
 8 </head>
 9 <body>
10     <%
11         String str = "12";
12         int number = Integer.parseInt(str);
13         session.setAttribute("number", str);
14     %>
15     <a href="assesion1.jsp">前往session1.jsp</a>
16 
17 </body>
18 </html>

assesion1.jsp

 1 <%@ page language="java" contentType="text/html; charset=utf-8"
 2     pageEncoding="utf-8"%>
 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 4 <html>
 5 <head>
 6 <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
 7 <title>Insert title here</title>
 8 </head>
 9 <body>
10     <%
11         String str = (String) session.getAttribute("number");
12         int number = Integer.parseInt(str);
13     %>
14     <%=number * number%>
15 </body>
16 </html>

posted @ 2013-10-21 09:52  最是那一杯红酒  阅读(3066)  评论(0)    收藏  举报