简体   繁体   English

将请求解释为JSP而不是通过servlet

[英]Interpreting request as JSP instead of through servlet

This should be relatively simple to do but I've yet to find a description of how to do it. 这样做应该相对简单,但是我还没有找到如何做的描述。

My setup is a simple web app that processes every request through a servlet (I'll call it MyEverythingServlet for this question). 我的设置是一个简单的Web应用程序,它通过servlet处理每个请求(对于这个问题,我将其称为MyEverythingServlet)。 Here's a slightly modified version of my web.xml: 这是我的web.xml的稍微修改后的版本:

<servlet>
    <servlet-name>MyEverythingServlet</servlet-name>
    <servlet-class>blah.blah.blah.MyEverythingServlet</servlet-class>
</servlet>

<servlet-mapping>
    <servlet-name>MyEverythingServlet</servlet-name>
    <url-pattern>/*</url-pattern>
</servlet-mapping>

Right now, the servlet is pretty simple and either does some work (when work.do is part of the path) and if a .txt file is specified in the path, we'll do some validation and then load the file and send the text as the response: 现在,该Servlet非常简单,可以完成一些工作(当work.do是路径的一部分时),如果在路径中指定了.txt文件,我们将进行一些验证,然后加载该文件并发送文字作为回应:

response.getOutputStream().print( content );

What I'd like to do is either: 我想做的是:

  • Inside the servlet, if the request is a URL to a .jsp file, I'd like to be able to have the container interpret the JSP scriptlet parts/taglib stuff before I write the String to the response. 在Servlet内部,如果请求是指向.jsp文件的URL,那么我希望能够在将String写入响应之前让容器解释JSP scriptlet部件/ taglib内容。
  • Change my web.xml to have it process .jsp files outside of MyEverythingServlet. 更改我的web.xml以使其处理MyEverythingServlet之外的.jsp文件。
  • Inside the servlet, if the request is a URL to a .jsp file, I'd like to be able to have the container interpret the JSP scriptlet parts/taglib stuff before I write the String to the response. 在Servlet内部,如果请求是指向.jsp文件的URL,那么我希望能够在将String写入响应之前让容器解释JSP scriptlet部件/ taglib内容。

There's no direct API available which processes JSP files programmatically like that. 没有直接的API可以像这样以编程方式处理JSP文件。 In theory, you'd like to have the JSP in public webcontent and forward the request to the JSP. 从理论上讲,您希望在公共Web内容中包含JSP,并将请求转发到JSP。 You can do this with RequestDispatcher#forward() . 您可以使用RequestDispatcher#forward()

if (request.getServletPath().endsWith(".jsp")) {
    request.getRequestDispatcher(request.getServletPath()).forward(request, response);
} else {
    // Write "plain" to response as you did.
}

You may only want to do some checks on the correctness of the paths, but this should at least give you the generic idea. 您可能只想对路径的正确性进行一些检查,但这至少应该为您提供一般的想法。 There is however a little problem: the servlet will be invoked recursively since it's mapped on /* . 但是存在一个小问题:由于servlet映射在/*上,因此将递归调用它。 You'd rather replace this MyEverythingServlet by a Filter which just delegates the remnant of the job to the appserver's builtin default servlet. 您宁愿使用一个Filter代替此MyEverythingServlet ,该Filter只会将作业的剩余部分委派给应用服务器的内置默认servlet。 Having a Servlet to listen on /* is already a design-smell indication that it should have been a Filter from the beginning on ;) Servlet监听/*已经是一种设计嗅觉,表明它从一开始就应该是一个Filter ;)

  • Change my web.xml to have it process .jsp files outside of MyEverythingServlet. 更改我的web.xml以使其处理MyEverythingServlet之外的.jsp文件。

You can't have a "negative" url-pattern in web.xml . web.xml不能有“负” url-pattern Best what you can do is to let the servlet listen on a more specific url-pattern like *.txt or /static/* and keep the JSP files there outside. 最好的办法是让Servlet监听更具体的url-pattern例如*.txt/static/* ,并将JSP文件保存在外面。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM