JSP页面间传递参数是经常需要使用到的功能,有时还需要多个JSP页面间传递参数。下面介绍一下实现的方法。
(1)直接在URL请求后添加
如:< a href="thexuan.jsp?action=transparams&detail=directe">直接传递参数< /a>
特别的在使用response.sendRedirect做页面转向的时候,也可以用如下代码:
response.sendRedirect("thexuan.jsp?action=transparams&detail=directe") ,可用request.getParameter(name)取得参数 。
(2)jsp:param
它可以实现主页面向包含页面传递参数,如下:
< jsp:include page="Relative URL"> < jsp:param name="param name" value="paramvalue" /> < /jsp:include> 还可以实现在使用jsp:forward动作做页面跳转时传递参数,如下:
< jsp:forward page="Relative URL"> < jsp:param name="paramname" value="paramvalue" /> < /jsp:forward>
通过这种方式和一般的表单参数一样的,也可以通过request.getParameter(name)取得参数 。
(3)设置session和request
通过显示的把参数放置到session和request中,以达到传递参数的目的
session.setAttribute(name,value); request.setAttribute(name,value) 取参数:
value=(value className)session.getAttribute(name); value=(value className)request.getAttribute(name); 大家肯定已经注意到了,在取参数的时候,做了类型转换,这是因为放置在session和request中的对象的属性被看作 java.lang.Object类型的了,如果不转换,在将直付给value时会报classcastexception异常。
在多个JSP页面之间传递参数。
在多个JSP页面之间传递参数
1. 怎么在多个JSP页面之间进行参数传递?需要使用JSP的内置作用域对象session。利用它的两个方法setAttribute(),getAttribute()
2. 下面的这个实例实现了把第一个JSP页面的参数传递给第三个页面的功能
3. 代码如下:
1.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>1</title>
</head>
<body>
<form method="get" action="2.jsp">
what's your name<input type="text" name="username">
<input type="submit" value="submit">
</form>
</body>
</html>
2.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>2</title>
</head>
<body>
<form method="post" action="3.jsp?pass=11">
<%String name=request.getParameter("username");session.setAttribute("username",name);%>
Your name is:<%=request.getParameter("username")%> <br/>
<input name="username" type="text" width="50px">
what's your hobby<input type="text" name="hobby">
<input type="submit" value="submit">
</form>
</body>
</html>
3.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>3</title>
</head>
<body>
<form>
your name is:<%=session.getAttribute("username")%><br/>
your hobby is:<%=request.getParameter("hobby")%> <br/>
your password is:<%=request.getParameter("pass")%><br/>
</form>
</body>
</html>
2010-01-25
几种JSP页面传值方式:
文章分类:Web前端
几种JSP页面传值方式:
1. 隐藏域传值:
<form method="post" action="client_crud.jsp" >
<input type="hidden" name="id" value="<%=id %>">
2. URL传值:
用button
a.
<input name="btnModify" type="button" class="button1" onClick="self.location='client_modify.jsp?id=<%=id %>'"
value="修改分销商" />
b.把input的onClick=”modifyRegion()”
function modifyRegion() {
window.self.location = client_node_modify.jsp?id=<%=id%>";
}
3. JS方式传值
//取得form对象提交表单
with(document.getElementById("userForm")) {
method="post";
action="user_add.jsp?command=add";
submit();
}
function searchItem() {
with(document.forms[0]) {
action="servlet/basedata/SearchItemServlet";
method="post";
submit();
}
}
JSP的几种参数传值:
2009-10-15 09:21
JSP的几种参数传值:
一、超链接
<a href="P.jsp?username=zhangshan&pwd=123456&age=25"> 链接</a>
二、forma表单
1.可显示的控件
<input type="text" name="username">
2.如果要传递的值,不需要显示在页面上
(1)<input type="hidden" name="pwd" value="<%=pwd%>">
(2)<form action="XXX.jsp" method="post"></form>
三、JSP的include和forward标签
<jsp:include flush="true" page="T.jsp?username=zhangshan&pwd=123456678">
<jsp:param name="age" value="28"/>
</jsp:include>
四、javascript方式
<script type="text/javascript">
function demo(){
var v = document.all('username').value;
location.replace("V.jsp?username="+v+"&age=25");
}
</script>
<input type="text" name="username"><br>
<input type="button" value="href点击" οnclick="location.href='V.jsp?pwd=123456'"><BR>
<input type="button" value="replace点击"
οnclick="location.replac('V.jsp?pwd=123456789')">
<br>
<input type="button" value="动态取值" οnclick="demo()">
注:数据提交方式分为get和post两种
1、get和post的区别
A.GET请求只能处理小数据<2K
POST可以用来提交大数据
B.post提交方式相对而言,比较安全
get只是适合于小数据的传递,也就是说,只有post才能传递大数据,比如在文件上传时,就必须将method
指定为post
2、用post提交的情况:
当你的form表单的method属性指定为post,其提交方式才为post
其他的所有情况统统为get请求