JSP基础语法-动作(Action)

本文详细介绍了JSP中的动作元素,包括forward、param、include和bean(useBean、setProperty、getProperty)动作。讲解了它们的语法、用途和执行原理,特别是forward动作与Servlet中request.getRequestDispatcher().forward()的等价性,param动作用于传递参数,以及include动作的动态包含机制。此外,还讨论了在现代开发中,尽管bean动作在Model1模式下曾被广泛应用,但在Model2(MVC)模式普及后,其使用已逐渐减少。
摘要由CSDN通过智能技术生成

JSP基础语法

   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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值