最近做JSP作业的时候尝试写了一下这样的代码:
<!-- login.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>login</title>
</head>
<body>
<form action="test_include2.jsp" method="post">
<table>
<tr>
<td>姓名:</td>
<td><input type="text" name="username" value=""/></td>
</tr>
<tr>
<td>密码:</td>
<td><input type="password" name="password" value=""/></td>
</tr>
<tr>
<td colspan="2" align="center">
<input type="submit" value="登录"/>
</td>
</tr>
</table>
</form>
</body>
</html>
<!--test_include2.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>test2</title>
</head>
<body>
<jsp:include page="check.jsp" flush="true">
<jsp:param name="username" value="<%=request.getParameter("username")%>"/>
<jsp:param name="password" value="<%=request.getParameter("password")%>"/>
</jsp:include>
</body>
</html>
<!--check.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>check</title>
</head>
<body>
<%
String username = request.getParameter("username");
String pwd = request.getParameter("password");
if (username.equals("admin")&&pwd.equals("123"))
{
session.setAttribute("username",username);
%>
<jsp:forward page="ok.jsp"/>
<%
}
else
{
%>
<jsp:include page="fail.jsp" flush="true"/>
<%
}
%>
</body>
</html>
首先报出了第一个错误:
属性值[request.getParameter("password")]引用["],在值内使用时必须被转义。
经查阅,是因为在tomcat 6.0版本后对双引号的处理机制引起的,如果出现双引号包含双引号的情况,就可能会出现这个错误。将双引号改为单引号即可:
<jsp:param name="username" value='<%=request.getParameter("username")%>'/>
<jsp:param name="password" value='<%=request.getParameter("password")%>'/>
也可以利用"\"转义字符,如:
<jsp:param name="name" value="<%=request.getParameter(\"name\")%>"/>
<jsp:param name="name" value="<%=request.getParameter("name")%>"/>
(参考自:https://blog.csdn.net/zkflame/article/details/51130338)
通过查阅网上的资料,我还发现 <%=request.getParameter("username")%> 可以写作 ${param.username} 或 ${requestSpoce.username}。
其中 ${param.username} 会依次调用 pageContext.getAttribute("username") -> request.getAttribute("username") ->session.getAttribute("username") -> application.getAttribute("username"),只要找到某一个不为空的值就立刻返回。
而${requestScope.username} 只返回 request.getAttribute("username")。
接着又出现了第二个错误:
严重: Servlet.service() for servlet [jsp] in context with path [/homework] threw exception [在 [18] 行处理 [/check.jsp] 时发生异常
18: <jsp:forward page="ok.jsp"/>
经查阅,错误的原因在于不能在 forward 动作前进行 flush 操作。故在此代码前将 flush 设置为 false 即可。
<jsp:include page="check.jsp" flush="false"/>
同理,当flush操作后,也无法使用 response.sendRedirect() 及 response.setHeader() 方法。
那么flush究竟有什么用呢?
经查阅,flush 具有清空缓存中的页面数据向客户端显示的功能。具体为可以设置是否开启页面缓存,如在include另一个jsp文件时,默认情况下,服务器会等待该文件被读到底端,然后才输出到客户端,并且销毁该次访问的request和response,而当把flush属性赋为真值 时,在缓存累积了一定数据时,服务器会先提供一部分数据给浏览器,并等待后续内容 。
外网的解释如下:(https://coderanch.com/t/287743/java/jsp-include-flush-true)
However there is a buffer size that you don't want to go beyond before sending data. There are times where you will exceed this buffer before a flush and your content will stop being sent. The two options are to flush or increase the buffer size (which I believe defaults at 8KB). The downside to flushing is at that point the response is comitted and cannot be forwarded.
由此可见,将 flush 设置为 false 并不是一个好的解决方案。那么如何实现在 flush=true 后再进行页面的跳转呢?
经查阅,我们仍可以使用 include 指令与 jsp:include动作进行页面的跳转。
<%@ include file="ok.jsp" %>
<jsp:include page="fail.jsp" flush="true"/>
不过在新跳转的页面中由于缓存被清空,同样无法使用 response 等功能。