在1个JSP中,跳转到1个HTML。 结果就报错。
java.lang.IllegalStateException: getOutputStream() has already been called for this response。RequestDispatcher rd = request.getRequestDispatcher("dd.html");
rd.forward(request,response);
但是使用 response.sendRedirect("dd.html"); 就不会出错哦。我想解决的问题是:怎么在HTML中绝对定位到资源。
就是JSP中的<base href="<%=basePath%>">效果。
在HTML怎么获得basePath呢。
我的环境是tomcat-6.0.26,J2EE5。
java.lang.IllegalStateException: getOutputStream() has already been called for this response。RequestDispatcher rd = request.getRequestDispatcher("dd.html");
rd.forward(request,response);
但是使用 response.sendRedirect("dd.html"); 就不会出错哦。我想解决的问题是:怎么在HTML中绝对定位到资源。
就是JSP中的<base href="<%=basePath%>">效果。
在HTML怎么获得basePath呢。
我的环境是tomcat-6.0.26,J2EE5。
刚刚测试了1下。 在HTML加入<BASE>标签。 JSP跳转就会开始出错了。
比如
<%%>后边不能回车换行等等
这是JSP生成的servlet代码
package org.apache.jsp;import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.jsp.*;
import java.util.*;public final class index_jsp extends org.apache.jasper.runtime.HttpJspBase
implements org.apache.jasper.runtime.JspSourceDependent { private static final JspFactory _jspxFactory = JspFactory.getDefaultFactory(); private static java.util.List _jspx_dependants; private javax.el.ExpressionFactory _el_expressionfactory;
private org.apache.AnnotationProcessor _jsp_annotationprocessor; public Object getDependants() {
return _jspx_dependants;
} public void _jspInit() {
_el_expressionfactory = _jspxFactory.getJspApplicationContext(getServletConfig().getServletContext()).getExpressionFactory();
_jsp_annotationprocessor = (org.apache.AnnotationProcessor) getServletConfig().getServletContext().getAttribute(org.apache.AnnotationProcessor.class.getName());
} public void _jspDestroy() {
} public void _jspService(HttpServletRequest request, HttpServletResponse response)
throws java.io.IOException, ServletException { PageContext pageContext = null;
HttpSession session = null;
ServletContext application = null;
ServletConfig config = null;
JspWriter out = null;
Object page = this;
JspWriter _jspx_out = null;
PageContext _jspx_page_context = null;
try {
response.setContentType("text/html;charset=gbk");
pageContext = _jspxFactory.getPageContext(this, request, response,
null, true, 8192, true);
_jspx_page_context = pageContext;
application = pageContext.getServletContext();
config = pageContext.getServletConfig();
session = pageContext.getSession();
out = pageContext.getOut();
_jspx_out = out; out.write('\r');
out.write('\n');String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
out.write("\r\n");
out.write("\r\n");
out.write("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">\r\n");
out.write("<html>\r\n");
out.write(" <head>\r\n");
out.write(" <base href=\"");
out.print(basePath);
out.write("\">\r\n");
out.write(" \r\n");
out.write(" <title>My JSP 'index.jsp' starting page</title>\r\n");
out.write("\t<meta http-equiv=\"pragma\" content=\"no-cache\">\r\n");
out.write("\t<meta http-equiv=\"cache-control\" content=\"no-cache\">\r\n");
out.write("\t<meta http-equiv=\"expires\" content=\"0\"> \r\n");
out.write("\t<meta http-equiv=\"keywords\" content=\"keyword1,keyword2,keyword3\">\r\n");
out.write("\t<meta http-equiv=\"description\" content=\"This is my page\">\r\n");
out.write("\t<!--\r\n");
out.write("\t<link rel=\"stylesheet\" type=\"text/css\" href=\"styles.css\">\r\n");
out.write("\t-->\r\n");
out.write("\t<script type=\"text/javascript\">\r\n");
out.write("\t\t\r\n");
out.write("\t</script>\r\n");
out.write(" </head>\r\n");
out.write(" \r\n");
out.write(" <body>\r\n");
out.write("\t\t\t\r\n");
out.write(" </body>\r\n");
out.write("</html>\r\n");
out.write("\t\t\t"); RequestDispatcher rd = request.getRequestDispatcher("/pages/sample/index.html");
//response.sendRedirect(basePath + "/pages/sample/index.html");
rd.forward(request,response);
} catch (Throwable t) {
if (!(t instanceof SkipPageException)){
out = _jspx_out;
if (out != null && out.getBufferSize() != 0)
try { out.clearBuffer(); } catch (java.io.IOException e) {}
if (_jspx_page_context != null) _jspx_page_context.handlePageException(t);
}
} finally {
_jspxFactory.releasePageContext(_jspx_page_context);
}
}
}
而且没有必要两样都用。
只能用一样
但是我现在把HTML元素全部干掉也是一样会报错。<%@ page language="java" pageEncoding="GBK" contentType="text/html;charset=gbk" import="java.util.*" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
RequestDispatcher rd = request.getRequestDispatcher("dd.html");
rd.forward(request,response);
%>这样就不会调用OUT了。 但是还是会出错。
为什么相同路径的JSP又没错,HTML就出错?
forward是服务器请求资源,服务器直接访问目标地址的URL,把那个URL的响应内容读取过来,然后把这些内容再发给浏览器,浏览器根本不知道服务器发送的内容是从哪儿来的,所以它的地址栏中还是原来的地址。
redirect就是服务端根据逻辑,发送一个状态码,告诉浏览器重新去请求那个地址,一般来说浏览器会用刚才请求的所有参数重新请求,所以session,request参数都可以获取。
java.lang.IllegalStateException: getOutputStream() has already been called for this response。