0
点赞
收藏
分享

微信扫一扫

JSP的7个动作指令--forward指令

您好 2022-03-17 阅读 70
javajava-ee

一、测试forward带参数传递

示例代码结构

basic.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Basic Page</title>
</head>
<body>
<h3> basic page</h3>
<jsp:forward page="destination.jsp">
    <jsp:param name="age" value="29"/>
</jsp:forward>

</body>
</html>

 destination.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Destination Page</title>
</head>
<body>
<h3>destionation page</h3>
<%=request.getParameter("age")%>
</body>
</html>

效果

 可以看到,最终的页面是destination中的内容,参数从base传递到了destination

二、form的action属性,测试其forward功能及参数传递

示例代码结构

 form.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Form Page</title>
</head>
<body>
<form id="login" method="post" action="basic.jsp">
    <input type="text" name="username">
    <input type="submit" value="login">
</form>
</body>
</html>

basic.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Basic Page</title>
</head>
<body>
<h3> basic page</h3>
<jsp:forward page="destination.jsp">
    <jsp:param name="age" value="29"/>
</jsp:forward>

</body>
</html>

destination.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Destination Page</title>
</head>
<body>
<h3>destionation page</h3>
<%=request.getParameter("username")%>
<%=request.getParameter("age")%>
</body>
</html>

效果,可以看到form中的username和basic中的age都传递到了destination页面

 

 总结:

<jsp:forward.../>并没有重新向新页面发送请求,只是采用了后面的新页面对用户生成响应。

请求依然是一次请求,所以请求参数、请求属性都不会丢失。

举报

相关推荐

0 条评论