在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。

解决方案 »

  1.   

    把servlet完整代码贴出来看看,你前面应该用到了response.getOutputStream()
      

  2.   

      没有调用任何Servlet。 直接访问的JSP。
      刚刚测试了1下。 在HTML加入<BASE>标签。 JSP跳转就会开始出错了。 
      
      

  3.   

    jsp的话,你前边有空白了,尤其是<%%>外的空白
    比如
    <%%>后边不能回车换行等等
      

  4.   

    注意。 跳转到JSP是没有问题的。但是跳转到HTML就不行。
    这是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);
        }
      }
    }
      

  5.   

    不是都页面跳转了,还out.write干嘛?
      

  6.   

    这个是JSP生成的servlet.我是直接在JSP中写的跳转
      

  7.   

    你同时用了out,然后又用了response,所以出错了,
    而且没有必要两样都用。
    只能用一样
      

  8.   

    补充一下,你既然用了out,那么内容就被输出到当前页面了,还要跳转干吗?如果要跳转,那么out出的东西又有什么意义?
      

  9.   

    更正一下。 out是因为JSP中有HTML元素。 所以生成的输出。
    但是我现在把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了。 但是还是会出错。 
      

  10.   

    大哥。 说下你的思路? 为什么是路径? 为什么第1次可以。 后面有报错?
    为什么相同路径的JSP又没错,HTML就出错?
      

  11.   

    我觉得这应该是你没能理解两个关键词的含义:
    forward是服务器请求资源,服务器直接访问目标地址的URL,把那个URL的响应内容读取过来,然后把这些内容再发给浏览器,浏览器根本不知道服务器发送的内容是从哪儿来的,所以它的地址栏中还是原来的地址。
    redirect就是服务端根据逻辑,发送一个状态码,告诉浏览器重新去请求那个地址,一般来说浏览器会用刚才请求的所有参数重新请求,所以session,request参数都可以获取。
      

  12.   

      注意我讲的异常哦。 不是路径错误。
    java.lang.IllegalStateException: getOutputStream() has already been called for this response。
      

  13.   

    RequestDispatcher rd = request.getRequestDispatcher("dd.html");    跳到静态页面用这种方式是没必要的  静态页面能使用request?