jsp中四种传递参数的方法
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:
- <%@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/>
- <!--在爱好前空一个空格,是为了排版好看些-->
- 爱好:<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>传递的参数本身就是在服务器端的,不需要经历由客户端到服务端这么一个过程。
jsp中四种传递参数的方法的更多相关文章
- (转)JSP中四种传递参数的方法:
1.form表单 2.request.setAttribute();和request.getAttribute(); 3.超链接:<a herf="index.jsp"?a= ...
- JSP中四种传递参数中文乱码问题
查看来源:http://blog.csdn.net/hackerain/article/details/6776083
- C++中三种传递参数方法的效率分析
众所周知,在C++中有三种参数传递的方式: 按值传递(pass by value) #include <iostream> using namespace std; void swap(i ...
- JSP中四种属性保存范围(1)
一.四种属性范围 在JSP中提供了四种属性保存范围 page:在一个页面内保存属性,跳转之后无效request:在一次服务请求范围内,服务器跳转后依然有效session:-在一次会话范围内,无论何种跳 ...
- Java中四种遍历List的方法
package com.ietree.basic.collection.loop; import java.util.ArrayList; import java.util.Iterator; imp ...
- JSP中四种属性保存范围(2)
1.session <%@ page language="java" contentType="text/html" pageEncoding=" ...
- Jsp传递参数的方法
今天老师讲了jsp中四种传递参数的方法,我觉得总结一下,挺好的,以备后用! 1.form表单 2.request.setAttribute();和request.getAttribute(); 3.超 ...
- SpringMVC 学习 十一 springMVC控制器向jsp或者别的控制器传递参数的四种方法
以后的开发,大部分是发送ajax,因此这四种传递参数的方法,并不太常用.作为了解吧 第一种:使用原生 Servlet 在控制器的响应的方法中添加Servlet中的一些作用域:HttpRequestSe ...
- JSP页面之间传递参数的方法有哪些?
JSP页面之间传递参数的方法有哪些? 解答: 1)request 2)session 3)application 4)提交表单 5)超链接
随机推荐
- 详解C/C++预处理器
C/C++编译系统编译程序的过程为预处理.编译.链接.预处理器是在程序源文件被编译之前根据预处理指令对程序源文件进行处理的程序.预处理器指令以#号开头标识,末尾不包含分号.预处理命令不是C/C++语 ...
- java实现合并两个已经排序的列表
相对于C++来说,Java的最大特点之一就是没有令人困惑的指针,但是我们不可否认,在某些特定的情境下,指针确实算的上一把利刃.虽然Java中没有明确定义出指针,但是由于类的思想,我们可以使用class ...
- Java并发编程:Lock(上)
在上一篇文章中我们讲到了如何使用关键字synchronized来实现同步访问.本文我们继续来探讨这个问题,从Java 5之后,在java.util.concurrent.locks包下提供了另外一种方 ...
- NGUI基础之button(按钮)
1,button的创建:2,button组件的基本属性:3,button的事件监听 原位地址:http://blog.csdn.net/dingkun520wy/article/details/504 ...
- DB天气app冲刺二阶段第九天
今天是第九天了 不管怎么样也要收尾了赶紧,毕竟不可能做到尽善尽美了,时间不够了所以要把该砍掉的砍点,然后应对下周的大二同学的面试.尽量做好界面的美化工作这是最基本的了.毕竟我一直崇尚的就是UI设计了. ...
- OC的类方法、对象方法和函数
OC语言中的方法和函数是有区别的:类内部叫方法,单独定义的叫函数,定义的格式也不同 类方法:+ (void) 方法名.对象方法:- (void) 方法名.函数:void 函数名(参数列表) #impo ...
- iOS点击cell查看大图,点击大图还原小图-b
一.项目需求 用collectionView展示很多照片,点击某个照片,用全屏scrollView无限循环的方式查看图片.点击放大的图片,图片缩小到原先的尺寸. 如图gif1.gif所示,点击中间的图 ...
- iOS8中的UIAlertController
转: iOS8推出了几个新的“controller”,主要是把类似之前的UIAlertView变成了UIAlertController,这不经意的改变,貌似把我之前理解的“controlle ...
- 初尝backbone
backbone的基础知识在此将不再进行介绍.自己后续应该会整理出来,不过今天先把这几天学的成果用一个demo进行展示. 后续可运行demo将会在sinaapp上分享,不过近期在整理sinaapp上d ...
- PE文件结构详解(五)延迟导入表
PE文件结构详解(四)PE导入表讲 了一般的PE导入表,这次我们来看一下另外一种导入表:延迟导入(Delay Import).看名字就知道,这种导入机制导入其他DLL的时机比较“迟”,为什么要迟呢?因 ...