Jsp傳遞參數的方法

今天老師講了jsp中四種傳遞參數的方法,我以爲總結一下,挺好的,以備後用!html

一、form表單java

二、request.setAttribute();和request.getAttribute();數組

三、超連接:<a herf="index.jsp"?a=a&b=b&c=c>name</a>服務器

四、<jsp:param>jsp

 

下面一一舉例說明:函數

一、form表單post

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:code

 

 
<%@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,如:orm

 

 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"%>,這樣就混合了兩種編碼方式,因此會出現亂碼,因此解決之道就是統一傳遞和接收的編碼方式。

 

二、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指令來實現的。

 

三、超連接:<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方式同樣。

 

四、<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>傳遞的參數自己就是在服務器端的,不須要經歷由客戶端到服務端這麼一個過程,可是服務器裏的參數傳遞是這麼回事呢?這個問題,我不知道了!真是知識是一個擴大的圓圈,你知道的越多,那麼不知道的就越多!努力吧!

相關文章
相關標籤/搜索