jsp常见的指令

Laughing
2017-11-13 / 3 评论 / 1,569 阅读 / 搜一下 / 正在检测是否收录...

在前面的章节中,我们介绍了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");  
%>  
1

评论 (3)

取消
  1. 头像
    夏日博客
    Windows 7 · Google Chrome

    居然能看懂JSP,哈哈。

    回复
    1. 头像
      Laughing 作者
      Linux · Google Chrome
      @ 夏日博客

      其实都是写最最基础的东西

      回复
    2. 头像
      Laughing 作者
      Linux · Google Chrome
      @ 夏日博客

      其实都是写最最基础的东西

      回复
  2. 头像
    夏日博客
    Windows 7 · Google Chrome

    基本上都是html.

    回复
  3. 头像
    夏日博客
    Windows 7 · Google Chrome

    居然能看懂JSP,哈哈。

    回复
  4. 头像
    夏日博客
    Windows 7 · Google Chrome

    基本上都是html.

    回复
  5. 头像
    newlw
    Windows 10 · Google Chrome

    看密码

    回复
  6. 头像
    多大的人
    MacOS · Safari

    thank you,谢谢!

    回复