3.6、动作(Action)
动作的语法格式: <jsp:动作名称 属性名=属性值.... ></jsp:动作名称>
3.6.1、forward动作
例:<jsp:forward page="/2.jsp"></jsp:forward> 路径:和url-pattern相同
转发:等同Servlet中request.getRequestDispatcher(“/2.jsp”).forward(request, response)方法。
代替【3.1、scriptlet(小脚本)】的写法:
<%
request.getRequestDispatcher("/action/forward/2.jsp").forward(request,response);
%>
1.jsp代码:
<%@page contentType="text/html; charset=UTF-8"%>
<%
//request是九大内置对象之一,可以直接拿来使用【在service方法内部】
request.setAttribute("username","admin");
%>
<%
//转发
//request,response属于九大内置对象
//request.getRequestDispatcher("/2.jsp").forward(request,response);
%>
<jsp:forward page="/2.jsp"></jsp:forward>
<%-- 重定向没有对应的动作完成 --%>
<%
//response.sendRedirect(request.getContextPath() + "/2.jsp");
%>
2.jsp代码:
<%@page contentType="text/html; charset=UTF-8"%>
<%=request.getAttribute("username") %>
————————————————
3.6.2、param动作
<jsp:forward page="/2.jsp">
<jsp:param value="lili" name="username"/>
<jsp:param value="112233" name="password"/>
</jsp:forward>
使用param动作可以传参数,发送请求的时候向服务器传送参数。
1.jsp代码:
<%@page contentType="text/html; charset=UTF-8"%>
<html>
<head>
<title>param动作</title>
</head>
<body>
<%-- 转发:一次请求 --%>
<%--
<jsp:forward page="/2.jsp?username=admin"></jsp:forward>
--%>
<%--
<jsp:forward page="/2.jsp?use