Servlet用于静态资源

时间:2014-03-11 05:39:29

标签: static

我已经为IBM WAS8制作了静态资源的servlet,但是当我进入网络时,我总是看到同样的错误:

“错误500:javax.servlet.ServletException:javax.servlet.ServletException:com.ibm.websphere.servlet.error.ServletErrorReport:javax.servlet.jsp.JspException:javax.servlet.ServletException:java.lang.IllegalStateException :无法转发。响应已经提交。“

我的servlet

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class test extends HttpServlet
{    
private static final long serialVersionUID = 1L;

// Tomcat, Jetty, JBoss, and GlassFish 
private static final String COMMON_DEFAULT_SERVLET_NAME = "default";

// Resin 
private static final String RESIN_DEFAULT_SERVLET_NAME = "resin-file";

// WebLogic 
private static final String WEBLOGIC_DEFAULT_SERVLET_NAME = "FileServlet";

// WebSphere 
private static final String WEBSPHERE_DEFAULT_SERVLET_NAME = "SimpleFileServlet";


public String scanDefaultServlet(){
    if(this.getServletContext().getNamedDispatcher(COMMON_DEFAULT_SERVLET_NAME) != null) {
        return COMMON_DEFAULT_SERVLET_NAME;
    } else if(this.getServletContext().getNamedDispatcher(RESIN_DEFAULT_SERVLET_NAME) != null) {
        return RESIN_DEFAULT_SERVLET_NAME;
    } else if(this.getServletContext().getNamedDispatcher(WEBLOGIC_DEFAULT_SERVLET_NAME) != null){
        return WEBLOGIC_DEFAULT_SERVLET_NAME;
    } else if(this.getServletContext().getNamedDispatcher(WEBSPHERE_DEFAULT_SERVLET_NAME) != null){
        return WEBSPHERE_DEFAULT_SERVLET_NAME;
    } else {
        throw new IllegalStateException("Cannot determine what Server you currently use");
    }       
}

public void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    doGet(req, resp);
}

public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
{
    RequestDispatcher rd = getServletContext().getNamedDispatcher(this.scanDefaultServlet());
    HttpServletRequest wrapped = new HttpServletRequestWrapper(req) {
            public String getServletPath() {return "";}
    };

    rd.forward(wrapped, resp);
}

¿有什么想法吗? ¿你建议我另一种解决方案吗?感谢。

1 个答案:

答案 0 :(得分:0)

这个servlet的想法似乎来自Spring框架的DefaultServletHttpRequestHandler。您可以查看代码,看看它与您的代码有何不同。你似乎正在做的额外不必要的事情就是这个

HttpServletRequest wrapped = new HttpServletRequestWrapper(req) {
            public String getServletPath() {return "";}
    };

这样做的具体原因是什么?