今天老师讲了jsp中四种传递参数的方法,我觉得总结一下,挺好的,以备后用!
1、form表单
2、request.setAttribute();和request.getAttribute();
3、超链接:<a herf="index.jsp""color: #800000">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,如:
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:
<%@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">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" rel="external nofollow" >传递参数</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>传递的参数本身就是在服务器端的,不需要经历由客户端到服务端这么一个过程,但是服务器里的参数传递是这么回事呢?这个问题,我不知道了!真是知识是一个扩大的圆圈,你知道的越多,那么不知道的就越多!努力吧!
以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,同时也希望多多支持!
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
《魔兽世界》大逃杀!60人新游玩模式《强袭风暴》3月21日上线
暴雪近日发布了《魔兽世界》10.2.6 更新内容,新游玩模式《强袭风暴》即将于3月21 日在亚服上线,届时玩家将前往阿拉希高地展开一场 60 人大逃杀对战。
艾泽拉斯的冒险者已经征服了艾泽拉斯的大地及遥远的彼岸。他们在对抗世界上最致命的敌人时展现出过人的手腕,并且成功阻止终结宇宙等级的威胁。当他们在为即将于《魔兽世界》资料片《地心之战》中来袭的萨拉塔斯势力做战斗准备时,他们还需要在熟悉的阿拉希高地面对一个全新的敌人──那就是彼此。在《巨龙崛起》10.2.6 更新的《强袭风暴》中,玩家将会进入一个全新的海盗主题大逃杀式限时活动,其中包含极高的风险和史诗级的奖励。
《强袭风暴》不是普通的战场,作为一个独立于主游戏之外的活动,玩家可以用大逃杀的风格来体验《魔兽世界》,不分职业、不分装备(除了你在赛局中捡到的),光是技巧和战略的强弱之分就能决定出谁才是能坚持到最后的赢家。本次活动将会开放单人和双人模式,玩家在加入海盗主题的预赛大厅区域前,可以从强袭风暴角色画面新增好友。游玩游戏将可以累计名望轨迹,《巨龙崛起》和《魔兽世界:巫妖王之怒 经典版》的玩家都可以获得奖励。
更新日志
- 小骆驼-《草原狼2(蓝光CD)》[原抓WAV+CUE]
- 群星《欢迎来到我身边 电影原声专辑》[320K/MP3][105.02MB]
- 群星《欢迎来到我身边 电影原声专辑》[FLAC/分轨][480.9MB]
- 雷婷《梦里蓝天HQⅡ》 2023头版限量编号低速原抓[WAV+CUE][463M]
- 群星《2024好听新歌42》AI调整音效【WAV分轨】
- 王思雨-《思念陪着鸿雁飞》WAV
- 王思雨《喜马拉雅HQ》头版限量编号[WAV+CUE]
- 李健《无时无刻》[WAV+CUE][590M]
- 陈奕迅《酝酿》[WAV分轨][502M]
- 卓依婷《化蝶》2CD[WAV+CUE][1.1G]
- 群星《吉他王(黑胶CD)》[WAV+CUE]
- 齐秦《穿乐(穿越)》[WAV+CUE]
- 发烧珍品《数位CD音响测试-动向效果(九)》【WAV+CUE】
- 邝美云《邝美云精装歌集》[DSF][1.6G]
- 吕方《爱一回伤一回》[WAV+CUE][454M]