在前面的章节中,我们介绍了jsp的include的指令,除了包含页面的指令,jsp还有一些其他的指令,比如重定向、使用bean以及获取、设置属性等指令。
forward指令
forward指令用于页面重定向,
首页代码index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" session="true" isThreadSafe="true" autoFlush="true" info="我是首页" errorPage="errorPage.jsp"%>
<!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>首页</title>
</head>
<body>
<form action="index.jsp" name="form1" method="post">
<input type="text" name="userName" id="UserName">
<input type="submit" value="提交">
<jsp:forward page="forward.jsp"></jsp:forward>
</form>
</body>
</html>
跳转页面的代码forward.jsp
<%@page language="java" contentType="text/html; charset=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>首页</title>
</head>
<body>
<h1>跳转后的页面</h1>
</body>
</html>
温馨提示
forward
指令属于服务端跳转,所以我们会看到浏览器的地址还是index.jsp
forward
指令还可以传递参数
我们修改首页代码,如下
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" session="true" isThreadSafe="true" autoFlush="true" info="我是首页" errorPage="errorPage.jsp"%>
<!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>首页</title>
</head>
<body>
<form action="index.jsp" name="form1" method="post">
<input type="text" name="userName" id="UserName">
<input type="submit" value="提交">
<jsp:forward page="forward.jsp">
<jsp:param value="lisen.cc" name="website"/>
</jsp:forward>
</form>
</body>
</html>
以及跳转后的页面代码
<%@page language="java" contentType="text/html; charset=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>首页</title>
</head>
<body>
<h1>跳转后的页面<%=request.getParameter("website") %></h1>
</body>
</html>
我们可以看到,forward传递参数的形式跟包含页面传递参数的方式是一样的,都是通过param指令。
中文字符处理
当获取request的参数,如果中文出现乱码时,我们可以通过一下指令进行设置
<%
request.setCharacterEncoding("UTF-8");
%>
居然能看懂JSP,哈哈。
其实都是写最最基础的东西
其实都是写最最基础的东西
基本上都是html.
居然能看懂JSP,哈哈。
基本上都是html.
看密码
thank you,谢谢!