了jsp中四種傳遞參數的方法

 原文 :  http://blog.csdn.net/hackerain/article/details/6776083


jsp中四種傳遞參數的方法,我覺得總結一下,挺好的,以備後用!

1、form表單

2、request.setAttribute();和request.getAttribute();

3、超鏈接:<a herf="index.jsp"?a=a&b=b&c=c>name</a>

4、<jsp:param>


下面一一舉例說明:

1、form表單

form.jsp:

[html] view plain copy
  1. <%@page contentType="text/html; charset=GB2312"%>  
  2. <html>  
  3.     <head>  
  4.         <title>  
  5.             form.jsp file  
  6.         </title>  
  7.     </head>  
  8.   
  9.     <body style="background-color:lightblue">  
  10.   
  11.         <h2 style="font-family:arial;color:red;font-size:25px;text-align:center">登錄頁面</h2>  
  12.   
  13.         <form action="result.jsp" method="get" align="center">  
  14.             姓名:<input type="text" name="name" size="20" value="" maxlength="20"><br/>  
  15.       
  16.             密碼:<input type="password" name="password" size="20" value="" maxlength="20"><br/>  
  17.   
  18.              <!--在愛好前空一個空格,是爲了排版好看些-->  
  19.   
  20.             &nbsp;愛好:<input type="checkbox" name="hobby" value="唱歌">唱歌  
  21.                   <input type="checkbox" name="hobby" value="足球">足球  
  22.                   <input type="checkbox" name="hobby" value="籃球">籃球<br/><br/>  
  23.               
  24.             <input type="submit" name="submit" value="登錄">  
  25.             <input type="reset" name="reset" value="重置"><br/>  
  26.         </form>  
  27.   
  28.     </body>  
  29. </html>  


result.jsp:

[html] view plain copy
  1. <%@page language="java" import="java.util.*" pageEncoding="GB2312"%>  
  2. <html>  
  3.     <head>  
  4.         <title>  
  5.             result.jsp file  
  6.         </title>  
  7.     </head>  
  8.   
  9.     <body bgcolor="ffffff">  
  10.         <%  
  11.           request.setCharacterEncoding("GB2312");  
  12.   
  13.           String name=request.getParameter("name");  
  14.           name=new String(name.getBytes("iso-8859-1"),"GB2312");  
  15.   
  16.           String pwd=request.getParameter("password");  
  17.           String[] hobby=request.getParameterValues("hobby");//注意這裏的函數是getParameterValues()接受一個數組的數據  
  18.   
  19.         %>  
  20.             
  21.         <%  
  22.             if(!name.equals("") && !pwd.equals(""))  
  23.             {  
  24.         %>  
  25.                   
  26.                 您好!登錄成功!<br/>  
  27.                 姓名:<%=name%><br/>  
  28.                 密碼:<%=pwd%><br/>  
  29.                 愛好:<%  
  30.                          for(String ho: hobby)  
  31.                          {  
  32.                             ho=new String(ho.getBytes("iso-8859-1"),"GB2312");  
  33.                             out.print(ho+" ");  
  34.                          }  
  35.                        %>  
  36.         <%  
  37.             }  
  38.             else  
  39.             {  
  40.         %>  
  41.                     請輸入姓名或密碼!  
  42.         <%  
  43.             }  
  44.         %>  
  45.     </body>  
  46. </html>  
注意:form表單的提交方式爲get,在參數傳遞時會遇到中文亂碼的問題,一個簡單的解決方法是,將接受到的字符串先轉換成一個byte數組,再用String構造一個新的編碼格式的String,如:

[html] view plain copy
  1. String name=request.getParameter("name");  
  2. name=new String(name.getBytes("iso-8859-1"),"GB2312");  
如果form表單的提交方式爲post,解決亂碼問題的簡單辦法是,使用 request.setCharacterEncoding("GB2312");設置request的編碼方式。

爲什麼會出現中文亂碼問題呢?因爲Tomcat服務器默認的系統編碼方式爲iso-8859-1,你傳遞參數給服務器時,使用的是默認的iso-8859-1的編碼方式,但是服務器向你返回信息時,是按page指令中設置的編碼方式,如:<%@page language="java" import="java.util.*" pageEncoding="GB2312"%>,這樣就混合了兩種編碼方式,所以會出現亂碼,所以解決之道就是統一傳遞和接收的編碼方式。


2、request.setAttribute()和request.getAttribute()

set.jsp:

[html] view plain copy
  1. <%@page contentType="text/html; charset=GB2312"%>  
  2. <html>  
  3.     <head>  
  4.         <title>  
  5.             set.jsp file  
  6.         </title>  
  7.     </head>  
  8.   
  9.     <body style="background-color:lightblue">  
  10.         <%  
  11.             request.setAttribute("name","心雨");  
  12.         %>  
  13.         <jsp:forward page="get.jsp"/>  
  14.     </body>  
  15. </html>  

get.jsp:
[html] view plain copy
  1. <%@page contentType="text/html; charset=GB2312"%>  
  2. <html>  
  3.     <head>  
  4.         <title>  
  5.             get.jsp file  
  6.         </title>  
  7.     </head>  
  8.   
  9.     <body style="background-color:lightblue">  
  10.         <%  
  11.             out.println("傳遞過來的參數是:"+request.getAttribute("name"));  
  12.         %>  
  13.     </body>  
  14. </html>  

request.setAttribute()和request.getAttribute()是配合<jsp:forward>或是include指令來實現的。


3、超鏈接:<a herf="index.jsp?a=a&b=b&c=c">name</a>

href.jsp:

[html] view plain copy
  1. <%@page contentType="text/html; charset=GB2312"%>  
  2. <html>  
  3.     <head>  
  4.         <title>  
  5.             href.jsp file  
  6.         </title>  
  7.     </head>  
  8.   
  9.     <body style="background-color:lightblue">  
  10.         <a href="getHerf.jsp?name=心雨&password=123">傳遞參數</a>  
  11.     </body>  
  12. </html>  

getHref.jsp:
[html] view plain copy
  1. <%@page contentType="text/html; charset=GB2312"%>  
  2. <html>  
  3.     <head>  
  4.         <title>  
  5.             getHref.jsp file  
  6.         </title>  
  7.     </head>  
  8.   
  9.     <body style="background-color:lightblue">  
  10.         <%  
  11.             String name=request.getParameter("name");  
  12.             name=new String(name.getBytes("iso-8859-1"),"gb2312");  
  13.   
  14.             out.print("name:"+name);  
  15.         %>  
  16.         <br/>  
  17.         <%  
  18.             out.print("password:"+request.getParameter("password"));  
  19.         %>  
  20.     </body>  
  21. </html>  

這種傳遞參數的方法和form表單的get方式類似,是通過地址欄傳遞的參數,其亂碼解決方法也和form 的get方式一樣。


4、<jsp:param>

param.jsp:

[html] view plain copy
  1. <%@page contentType="text/html; charset=GB2312"%>  
  2. <html>  
  3.     <head>  
  4.         <title>  
  5.             param.jsp file  
  6.         </title>  
  7.     </head>  
  8.   
  9.     <body style="background-color:lightblue">  
  10.   
  11.         <%request.setCharacterEncoding("GB2312");%>  
  12.   
  13.         <jsp:forward page="getParam.jsp">  
  14.             <jsp:param name="name" value="心雨"/>  
  15.             <jsp:param name="password" value="123"/>  
  16.         </jsp:forward>  
  17.   
  18.     </body>  
  19. </html>  

getParam.jsp:
[html] view plain copy
  1. <%@page contentType="text/html; charset=GB2312"%>  
  2. <html>  
  3.     <head>  
  4.         <title>  
  5.             getParam.jsp file  
  6.         </title>  
  7.     </head>  
  8.   
  9.     <body style="background-color:lightblue">  
  10.         <%  
  11.             String name=request.getParameter("name");  
  12.             out.print("name:"+name);  
  13.         %>  
  14.         <br/>  
  15.         <%  
  16.             out.print("password:"+request.getParameter("password"));  
  17.         %>  
  18.     </body>  
  19. </html>  

這裏發現了一個奇怪的問題,還是在中文亂碼的問題上,在form表單的例子中,如果傳遞方式爲post,則只需要在接收參數的頁面設置request的編碼方式就可以了,即request.setCharacterEncoding("GB2312");,注意是在接收參數的頁面,如果將該句放到form表單裏,那麼不起作用,仍然是亂碼。而在本例中,爲了使傳遞的參數不出現亂碼,卻是將request.setCharacterEncoding("GB2312");放在發送參數的頁面中,纔會正常顯示中文,放在接收參數的頁面中,不起作用。也許這就是<jsp:param>和form表單傳遞參數不同的地方。爲什麼會有這個不同呢?可能是因爲form表單中的參數是由客戶端傳送到服務端上的,需要經過一個request的打包過程,但是<jsp:param>傳遞的參數本身就是在服務器端的,不需要經歷由客戶端到服務端這麼一個過程,但是服務器裏的參數傳遞是這麼回事呢?這個問題,我不知道了!真是知識是一個擴大的圓圈,你知道的越多,那麼不知道的就越多!努力吧!
發佈了11 篇原創文章 · 獲贊 10 · 訪問量 6萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章