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:

  1. <%@page contentType="text/html; charset=GB2312"%>
  2. <html>
  3. <head>
  4. <title>
  5. form.jsp file
  6. </title>
  7. </head>
  8. <body style="background-color:lightblue">
  9. <h2 style="font-family:arial;color:red;font-size:25px;text-align:center">登录页面</h2>
  10. <form action="result.jsp" method="get" align="center">
  11. 姓名:<input type="text" name="name" size="20" value="" maxlength="20"><br/>
  12. 密码:<input type="password" name="password" size="20" value="" maxlength="20"><br/>
  13. <!--在爱好前空一个空格,是为了排版好看些-->
  14. &nbsp;爱好:<input type="checkbox" name="hobby" value="唱歌">唱歌
  15. <input type="checkbox" name="hobby" value="足球">足球
  16. <input type="checkbox" name="hobby" value="篮球">篮球<br/><br/>
  17. <input type="submit" name="submit" value="登录">
  18. <input type="reset" name="reset" value="重置"><br/>
  19. </form>
  20. </body>
  21. </html>

result.jsp:

  1. <%@page language="java" import="java.util.*" pageEncoding="GB2312"%>
  2. <html>
  3. <head>
  4. <title>
  5. result.jsp file
  6. </title>
  7. </head>
  8. <body bgcolor="ffffff">
  9. <%
  10. request.setCharacterEncoding("GB2312");
  11. String name=request.getParameter("name");
  12. name=new String(name.getBytes("iso-8859-1"),"GB2312");
  13. String pwd=request.getParameter("password");
  14. String[] hobby=request.getParameterValues("hobby");//注意这里的函数是getParameterValues()接受一个数组的数据
  15. %>
  16. <%
  17. if(!name.equals("") && !pwd.equals(""))
  18. {
  19. %>
  20. 您好!登录成功!<br/>
  21. 姓名:<%=name%><br/>
  22. 密码:<%=pwd%><br/>
  23. 爱好:<%
  24. for(String ho: hobby)
  25. {
  26. ho=new String(ho.getBytes("iso-8859-1"),"GB2312");
  27. out.print(ho+" ");
  28. }
  29. %>
  30. <%
  31. }
  32. else
  33. {
  34. %>
  35. 请输入姓名或密码!
  36. <%
  37. }
  38. %>
  39. </body>
  40. </html>

注意:form表单的提交方式为get,在参数传递时会遇到中文乱码的问题,一个简单的解决方法是,将接受到的字符串先转换成一个byte数组,再用String构造一个新的编码格式的String,如:

  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:

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

get.jsp:

  1. <%@page contentType="text/html; charset=GB2312"%>
  2. <html>
  3. <head>
  4. <title>
  5. get.jsp file
  6. </title>
  7. </head>
  8. <body style="background-color:lightblue">
  9. <%
  10. out.println("传递过来的参数是:"+request.getAttribute("name"));
  11. %>
  12. </body>
  13. </html>

request.setAttribute()和request.getAttribute()是配合<jsp:forward>或是include指令来实现的。

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

href.jsp:

  1. <%@page contentType="text/html; charset=GB2312"%>
  2. <html>
  3. <head>
  4. <title>
  5. href.jsp file
  6. </title>
  7. </head>
  8. <body style="background-color:lightblue">
  9. <a href="getHerf.jsp?name=心雨&password=123">传递参数</a>
  10. </body>
  11. </html>

getHref.jsp:

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

这种传递参数的方法和form表单的get方式类似,是通过地址栏传递的参数,其乱码解决方法也和form 的get方式一样。

4、<jsp:param>

param.jsp:

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

getParam.jsp:

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

这里发现了一个奇怪的问题,还是在中文乱码的问题上,在form表单的例子中,如果传递方式为post,则只需要在接收参数的页面设置request的编码方式就可以了,即request.setCharacterEncoding("GB2312");,注意是在接收参数的页面,如果将该句放到form表单里,那么不起作用,仍然是乱码。而在本例中,为了使传递的参数不出现乱码,却是将request.setCharacterEncoding("GB2312");放在发送参数的页面中,才会正常显示中文,放在接收参数的页面中,不起作用。也许这就是<jsp:param>和form表单传递参数不同的地方。为什么会有这个不同呢?可能是因为form表单中的参数是由客户端传送到服务端上的,需要经过一个request的打包过程,但是<jsp:param>传递的参数本身就是在服务器端的,不需要经历由客户端到服务端这么一个过程。

jsp中四种传递参数的方法的更多相关文章

  1. (转)JSP中四种传递参数的方法:

    1.form表单 2.request.setAttribute();和request.getAttribute(); 3.超链接:<a herf="index.jsp"?a= ...

  2. JSP中四种传递参数中文乱码问题

    查看来源:http://blog.csdn.net/hackerain/article/details/6776083

  3. C++中三种传递参数方法的效率分析

    众所周知,在C++中有三种参数传递的方式: 按值传递(pass by value) #include <iostream> using namespace std; void swap(i ...

  4. JSP中四种属性保存范围(1)

    一.四种属性范围 在JSP中提供了四种属性保存范围 page:在一个页面内保存属性,跳转之后无效request:在一次服务请求范围内,服务器跳转后依然有效session:-在一次会话范围内,无论何种跳 ...

  5. Java中四种遍历List的方法

    package com.ietree.basic.collection.loop; import java.util.ArrayList; import java.util.Iterator; imp ...

  6. JSP中四种属性保存范围(2)

    1.session <%@ page language="java" contentType="text/html" pageEncoding=" ...

  7. Jsp传递参数的方法

    今天老师讲了jsp中四种传递参数的方法,我觉得总结一下,挺好的,以备后用! 1.form表单 2.request.setAttribute();和request.getAttribute(); 3.超 ...

  8. SpringMVC 学习 十一 springMVC控制器向jsp或者别的控制器传递参数的四种方法

    以后的开发,大部分是发送ajax,因此这四种传递参数的方法,并不太常用.作为了解吧 第一种:使用原生 Servlet 在控制器的响应的方法中添加Servlet中的一些作用域:HttpRequestSe ...

  9. JSP页面之间传递参数的方法有哪些?

    JSP页面之间传递参数的方法有哪些? 解答: 1)request 2)session 3)application 4)提交表单 5)超链接

随机推荐

  1. linux下bus,device,driver三者关系

    linux下bus,device,driver三者关系 1.bus: 总线作为主机和外设的连接通道,有些总线是比较规范的,形成了很多协议.如 PCI,USB,1394,IIC等.任何设备都可以选择合适 ...

  2. (菜鸟要飞系列)一,基于Asp.Net MVC5的后台管理系统(前言)

    今天真是个郁闷的日子,因为老师两个星期前给我的一个任务,用递归算法将Oracle数据库中用户信息及权限显示在jquery-treeView上,网上虽然有大神写出了这类算法,但是不贴全部代码,真的很难跟 ...

  3. IOS开发问题录:如何在Swift中引入Head文件?

    最近在学习IOS开发,从一个简单的登录开始,逐步解决了一个网络访问.获取控件值等问题,遇到了信息加密的问题. 做为IOS的入门者,信息加密需要解决如下几个问题: 1.IOS的MD5加密有没有固定函数, ...

  4. tomcat 7 用mod_jk做 负载均衡

    在Win7中使用apache为tomcat做负载均衡,各组件及版本如下: 两个tomcat v 7.0.57 一个apache v 2.2.14 一个mod_jk v 1.2.33(for windo ...

  5. HttpWebRequest,HttpWebResponse的用法和用途

    1.用途:HettpWebRequest,HettpWebResponse用途和webServers的作用差不多,都是得到一个页面传过来的值.HttpWebRequest 2.用法:--------- ...

  6. input框中value与placeholder的区别

    value:是input中预先放置的文字,当鼠标点击之后依然存在,是值的一部分. placeholder:是input中输入位置的默认显示的文字,鼠标点击后仍旧显示,但不属于值,类似于背景.

  7. The 10th Zhejiang Provincial Collegiate Programming Contest

    Applications http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5008 string set 专场 #include& ...

  8. Why am I able to change the contents of const char *ptr?

    http://stackoverflow.com/questions/3228664/why-am-i-able-to-change-the-contents-of-const-char-ptr I ...

  9. Unity3D脚本中文系列教程(一)

    原地址:http://dong2008hong.blog.163.com/blog/static/46968827201403115643431/?suggestedreading&wumii ...

  10. [转载]Spring Bean Configuration Inheritance

    转自: http://www.mkyong.com/spring/spring-bean-configuration-inheritance/ In Spring, the inheritance i ...