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] 
  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] 
  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] 
  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] 
  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] 
  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] 
  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]
  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]
  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]
  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");放在发送参数的页面中,才会正常显示中文,放在接收参数的页面中,不起作用。

转载于:https://www.cnblogs.com/jiaocheng/p/4770954.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值