[JAVAWEB]12.JSP中的JavaBean和动作元素(五)

本文介绍了如何使用JSP实现动态网页的基本操作,包括设置和获取变量、页面跳转及注释使用等。通过实例展示了如何修改公司名称为变量、解决乱码问题、使用jsp:forward进行条件跳转等内容。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

课堂笔记

视频笔记
链接:http://pan.baidu.com/s/1c1JuK5U 密码:3614
链接:http://pan.baidu.com/s/1c1KGCTE 密码:h1dk

一.修改公司名字为变量.
1.首先在main里面设置了一个变量
<jsp:include page="footer.jsp">
<jsp:param value="北京甲骨文科技有限公司" 

name="firm"/>
</jsp:include>
2.其次在footer中接收变量
<p>版权所有<%=request.getParameter("firm") %>有

限公司2000-2017</p>
看懂啥意思了吧,这个直接就OK了.
3.然后运行出现了乱码,这个要在body中加上这个
<%
    request.setCharacterEncoding("UTF-8");
%>
小结:program设置变量,footer中引用,然后解决乱码问

题.

二.jsp:forward 动作元素
<%
if(request.getParameter("url")!=null)
{
%>
<jsp:forward page="hello.jsp"></jsp:forward>
<%
}
%>

<!---->这是html注释,不能放在jsp中
<%-- --%>这是jsp注释,可以放在jsp中
这两个注释在里面都会出现页面无法加载的问题
动作元素里面是不能包含注释的

三.稍微改一改,我们在uname上做文章
如果名字不为空,那么就把名字传到hello.jsp中,并且显示你好:张三(uname)
<jsp:param value="<%=request.getParameter(\"uname\") %>" name="a"/>
测试

uname不为空结果?跳转到hello.jsp并显示您好:uname
uname为空结果?还是跳转到hello.jsp,显示你好:
http://blog.youkuaiyun.com/zeephom/article/details/77905675


为什么还是能跳转过来?这个有问题啊!

原因:当<jsp:include>标签没有参数时,<jsp:include page="include/header.jsp">的配对标签</jsp:include>不能换行

else
{
%>
<jsp:forward page="second.jsp"></jsp:forward>
<%
}
%>

代码

main.jsp

<%@page import="javafx.scene.control.Alert"%>
<%@ 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>Insert title here</title>
</head>
<body>
<form action="" method="post">
地址:<input type="text" name="url"><br/>
姓名:<input type="text" name="uname"><br/>
<input type="submit" value="页面跳转">
<%
if(request.getParameter("uname")!=null && !request.getParameter("uname").equals(""))
{
    System.out.println(request.getParameter("uname"));
%>
<jsp:forward page="hello.jsp">
<jsp:param value="<%=request.getParameter(\"uname\") %>" name="a"/>
</jsp:forward>
<%
}
%>
</form>

<%
    request.setCharacterEncoding("UTF-8");
%>

<h1 align="center">这是主页面</h1>

<!---->
<%----%>
<jsp:include page="footer.jsp">

<jsp:param value="北京甲骨文科技有限公司" name="firm"/>
</jsp:include>

</body>
</html>

footer.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>Insert title here</title>
</head>
<body>
<%
    request.setCharacterEncoding("UTF-8");
%>
<hr>
<br></br>
<p>版权所有<%=request.getParameter("firm") %>有限公司2000-2017</p>
</body>
</html>

hello.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>Insert title here</title>
</head>
<body>
<%
request.setCharacterEncoding("UTF-8");
%>
你好:<%=request.getParameter("a") %>
</body>
</html>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值