(轉)JSP中四種傳遞參數的方法:


1form表單

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

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

4<jsp:param>

下面一一舉例說明:

1、form表單

form.jsp:

<%@page contentType="text/html; charset=GB2312"%>  
<html>  
    <head>  
        <title>  
            form.jsp file  
        </title>  
    </head>  
  
    <body style="background-color:lightblue">  
  
        <h2 style="font-family:arial;color:red;font-size:25px;text-align:center">登錄頁面</h2>  
  
        <form action="result.jsp" method="get" align="center">  
            姓名:<input type="text" name="name" size="20" value="" maxlength="20"><br/>  
      
            密碼:<input type="password" name="password" size="20" value="" maxlength="20"><br/>  
  
             <!--在愛好前空一個空格,是為了排版好看些-->  
  
            &nbsp;愛好:<input type="checkbox" name="hobby" value="唱歌">唱歌  
                  <input type="checkbox" name="hobby" value="足球">足球  
                  <input type="checkbox" name="hobby" value="籃球">籃球<br/><br/>  
              
            <input type="submit" name="submit" value="登錄">  
            <input type="reset" name="reset" value="重置"><br/>  
        </form>  
  
    </body>  
</html>  

 

result.jsp:

<%@page language="java" import="java.util.*" pageEncoding="GB2312"%>  
<html>  
    <head>  
        <title>  
            result.jsp file  
        </title>  
    </head>  
  
    <body bgcolor="ffffff">  
        <%  
          request.setCharacterEncoding("GB2312");  
  
          String name=request.getParameter("name");  
          name=new String(name.getBytes("iso-8859-1"),"GB2312");  
  
          String pwd=request.getParameter("password");  
          String[] hobby=request.getParameterValues("hobby");//注意這里的函數是getParameterValues()接受一個數組的數據  
  
        %>  
            
        <%  
            if(!name.equals("") && !pwd.equals(""))  
            {  
        %>  
                  
                您好!登錄成功!<br/>  
                姓名:<%=name%><br/>  
                密碼:<%=pwd%><br/>  
                愛好:<%  
                         for(String ho: hobby)  
                         {  
                            ho=new String(ho.getBytes("iso-8859-1"),"GB2312");  
                            out.print(ho+" ");  
                         }  
                       %>  
        <%  
            }  
            else  
            {  
        %>  
                    請輸入姓名或密碼!  
        <%  
            }  
        %>  
    </body>  
</html>  

注意:form表單的提交方式為get,在參數傳遞時會遇到中文亂碼的問題,一個簡單的解決方法是,將接受到的字符串先轉換成一個byte數組,再用String構造一個新的編碼格式的String,如:

String name=request.getParameter("name");  
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:

<%@page contentType="text/html; charset=GB2312"%>  
<html>  
    <head>  
        <title>  
            set.jsp file  
        </title>  
    </head>  
  
    <body style="background-color:lightblue">  
        <%  
            request.setAttribute("name","心雨");  
        %>  
        <jsp:forward page="get.jsp"/>  
    </body>  
</html>  

get.jsp:

<%@page contentType="text/html; charset=GB2312"%>  
<html>  
    <head>  
        <title>  
            get.jsp file  
        </title>  
    </head>  
  
    <body style="background-color:lightblue">  
        <%  
            out.println("傳遞過來的參數是:"+request.getAttribute("name"));  
        %>  
    </body>  
</html>  

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

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

href.jsp:

<%@page contentType="text/html; charset=GB2312"%>  
<html>  
    <head>  
        <title>  
            href.jsp file  
        </title>  
    </head>  
  
    <body style="background-color:lightblue">  
        <a href="getHerf.jsp?name=心雨&password=123">傳遞參數</a>  
    </body>  
</html>  

getHref.jsp:

<%@page contentType="text/html; charset=GB2312"%>  
<html>  
    <head>  
        <title>  
            getHref.jsp file  
        </title>  
    </head>  
  
    <body style="background-color:lightblue">  
        <%  
            String name=request.getParameter("name");  
            name=new String(name.getBytes("iso-8859-1"),"gb2312");  
  
            out.print("name:"+name);  
        %>  
        <br/>  
        <%  
            out.print("password:"+request.getParameter("password"));  
        %>  
    </body>  
</html>  


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

4、<jsp:param>

param.jsp:

<%@page contentType="text/html; charset=GB2312"%>  
<html>  
    <head>  
        <title>  
            param.jsp file  
        </title>  
    </head>  
  
    <body style="background-color:lightblue">  
  
        <%request.setCharacterEncoding("GB2312");%>  
  
        <jsp:forward page="getParam.jsp">  
            <jsp:param name="name" value="心雨"/>  
            <jsp:param name="password" value="123"/>  
        </jsp:forward>  
  
    </body>  
</html>  

getParam.jsp:

<%@page contentType="text/html; charset=GB2312"%>  
<html>  
    <head>  
        <title>  
            getParam.jsp file  
        </title>  
    </head>  
  
    <body style="background-color:lightblue">  
        <%  
            String name=request.getParameter("name");  
            out.print("name:"+name);  
        %>  
        <br/>  
        <%  
            out.print("password:"+request.getParameter("password"));  
        %>  
    </body>  
</html>  

這里發現了一個奇怪的問題,還是在中文亂碼的問題上,在form表單的例子中,如果傳遞方式為post,則只需要在接收參數的頁面設置request的編碼方式就可以了,即request.setCharacterEncoding("GB2312");,注意是在接收參數的頁面,如果將該句放到form表單里,那么不起作用,仍然是亂碼。而在本例中,為了使傳遞的參數不出現亂碼,卻是將request.setCharacterEncoding("GB2312");放在發送參數的頁面中,才會正常顯示中文,放在接收參數的頁面中,不起作用。也許這就是<jsp:param>和form表單傳遞參數不同的地方。為什么會有這個不同呢?可能是因為form表單中的參數是由客戶端傳送到服務端上的,需要經過一個request的打包過程,但是<jsp:param>傳遞的參數本身就是在服務器端的,不需要經歷由客戶端到服務端這么一個過程,但是服務器里的參數傳遞是這么回事呢?這個問題,我不知道了!真是知識是一個擴大的圓圈,你知道的越多,那么不知道的就越多!努力吧!

參考文獻; 

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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM