如何从文件系统中的任何位置提供JSP-File?

时间:2016-10-18 10:10:38

标签: java jsp servlets

如标题所示。
如何提供JSP文件,可以在我的文件系统的任何位置?
当然,JSP-File之前应该解析

当网址类似于http://testserver:8080/MYTEST/test/_test_/path/to/file.jsp时 JSP-File应该来自我的文件系统的任何文件夹,我想在我的servlet中决定。

这是我的web.xml:

<servlet>
   <servlet-name>my_test</servlet-name>
      <servlet-class>my.package.MyServlet</servlet-class>
      <load-on-startup>2</load-on-startup>
      <async-supported>true</async-supported>
   </servlet>

   <servlet-mapping>
      <servlet-name>my_test</servlet-name>
      <url-pattern>/test/*</url-pattern>
   </servlet-mapping>
</servlet>

到目前为止这是我的servlet:

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import javax.servlet.http.HttpServletResponse;

public class MyServlet extends HttpServlet
{
   @Override
   protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws Exception {
      proceedRequest(req, resp);
   }

   @Override
   protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
      proceedRequest(req, resp);
   }

   private void proceedRequest(HttpServletRequest req, HttpServletResponse resp) throws Exception {
      if (req.getRequestURI().contains("_test_"))
      {
         //this file is completely outside my tomcat-folder and is unknown by any tomcat-config file.
         //how to serve this jsp file, so it will be parsed before?
         File jspfile = new File("/absolute/path/to/my/file.jsp"); 
         //???
      }
      else 
      {
         //use default request dispatcher
         RequestDispatcher rd = getServletContext().getNamedDispatcher("default");

         HttpServletRequest wrappedRequest = new HttpServletRequestWrapper(req) {
            @Override
            public String getServletPath() { return "";}
         };

         rd.forward(wrappedRequest, resp);
      }
   }
}

0 个答案:

没有答案