在前面的章节中,我们介绍了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
指令还可以传递参数
我们修改首页代码,如下
- <%@ 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="www.lisen.me" 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
指令。
useBean指令
关于useBean的指令可以参考
中文字符处理
当获取request的参数,如果中文出现乱码时,我们可以通过一下指令进行设置
- <%
- request.setCharacterEncoding("UTF-8");
- %>
发表于2017-11-14 at 17:52 板凳
基本上都是html.
发表于2017-11-14 at 17:52 沙发
居然能看懂JSP,哈哈。
@夏日博客其实都是写最最基础的东西