今天老师讲了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/> <!--在爱好前空一个空格,是为了排版好看些--> &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>传递的参数本身就是在服务器端的,不需要经历由客户端到服务端这么一个过程,但是服务器里的参数传递是这么回事呢?这个问题,我不知道了!真是知识是一个扩大的圆圈,你知道的越多,那么不知道的就越多!努力吧!

Jsp传递参数的方法的更多相关文章

  1. jsp中四种传递参数的方法

    jsp中四种传递参数的方法如下: 1.form表单 2.request.setAttribute();和request.getAttribute(); 3.超链接:<a herf="i ...

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

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

  3. C++向main函数传递参数的方法(实例已上传至github)

    通常情况下,我们定义的main函数都只有空形参列表: int main(){...} 然而,有时我们确实需要给mian传递实参,一种常见的情况是用户设置一组选项来确定函数所要执行的操作.例如,假定ma ...

  4. Javascript 定时器调用传递参数的方法

    文章来源:  https://m.jb51.net/article/20880.htm 备注:先记下,以后整理: Javascript 定时器调用传递参数的方法,需要的朋友可以参考下. 无论是wind ...

  5. php cli传递参数的方法

    php cli传递参数的方法 <pre>$options = "f:g:"; $opts = getopt( $options ); print_r($opts); & ...

  6. springMVC controller间跳转 重定向 传递参数的方法

    springMVC controller间跳转 重定向 传递参数的方法 spring MVC框架controller间跳转,需重定向.有几种情况:不带参数跳转,带参数拼接url形式跳转,带参数不拼接参 ...

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

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

  8. jsp传递参数的四种方法

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

  9. SpringMvc中两个Controller类之间传递参数的方法

    使用SpringMvc框架,在访问ControllerA的时候,将A里面的参数传递到ControllerB中.适用于同一框架下两个不同Controller或者由rpc(dubbo)连接的两个工程里的C ...

随机推荐

  1. C#.NET学习笔记7--11---算术运算符,变量赋值,变量的交换,布尔表达式1,布尔表达式2

    C#.NET学习笔记7---算术运算符 2013/9/6 技术qq交流群:JavaDream:251572072  教程下载,在线交流:创梦IT社区:www.credream.com 1.Consol ...

  2. tky项目第①个半月总结

    增加tky项目开发组已经有半个月了,这半个月一直是伴随着加班度过,学习了不少东西,也有不少抱怨,这些都是宝贵的工作经验体会.有必要在此好好总结一下. 我是中途加进这个项目的.也就是说.组内其它人员已经 ...

  3. node.js(八) 有趣的东西才开始哦

    ### Express介绍 npm提供了大量的第三方模块,其中不乏许多Web框架,比如我们本章节要讲述的一个轻量级的Web框架 ——— Express. Express是一个简洁.灵活的node.js ...

  4. SSIS之Foreach循环容器用法

    要实现的业务:A数据库服务器上某库的T_GOODS_DECL的状态字段“Is_Delete”标记为“1”的时候删除B数据库服务器上对应库的T_GOODS_DECL表中的记录,二者的主键为“DECL_N ...

  5. 20151222--Ajax三级无刷新

    <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding= ...

  6. Android应用程序架构之res

    res/drawable 专门存放png.jpg等图标文件.在代码中使用getResources().getDrawable(resourceId)获取该目录下的资源. res/layout 专门存放 ...

  7. 读取本地HTML的小说阅读器应用源码项目

    该源码是一个不错的读取本地HTML的小说阅读器,读取本地HTML的小说阅读器,并且源码也比较简单的,非常适合我们的新手朋友拿来学习,有兴趣的朋友研究下. 源码下载: http://code.662p. ...

  8. Silverlight代码编写对控件的PlaneProjection.RotationY属性控制动画

    Canvas c; void btnDraw_Click(object sender, RoutedEventArgs e) { Storyboard story = new Storyboard() ...

  9. asp.net mvc 注册中的邮箱激活功能实现(二)

    邮件发送功能封装 /// <summary>        /// 发送注册邮件        /// </summary>        /// <param name ...

  10. 剑指offer——已知二叉树的先序和中序排列,重构二叉树

    这是剑指offer中关于二叉树重构的一道题.题目原型为: 输入某二叉树的前序遍历和中序遍历的结果,请重建出该二叉树.假设输入的前序遍历和中序遍历的结果中都不含重复的数字.例如输入前序遍历序列{1,2, ...