开发者

Set JSP location for Servlet

开发者 https://www.devze.com 2023-01-27 06:40 出处:网络
I have simple web application based on JSP. Root of application looks like this: --META-INF |--WEB-INF

I have simple web application based on JSP. Root of application looks like this:

|
|--META-INF
|--WEB-INF
|  `--web.xml
|--img
|--css
|--index.jsp
|--some1.jsp
|--some2.jsp
|--some3.jsp

Where web.xml contains lines below:

<servlet>
    <servlet-name>servlet-jsp</servlet-name>
    <servlet-class>org.apache.jasper.servlet.JspServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>servlet-jsp</servlet-name>
    <url-pattern>/*.jsp</url-pattern>
</servlet-mapping>

Now I want change file structure of project - move all *.jsp files to special directory:

|
|--META-INF
开发者_JS百科|--WEB-INF
|  `--web.xml
|--img
|--css
|--jsp
   |--index.jsp
   |--some1.jsp
   |--some2.jsp
   |--some3.jsp

Can I configure "servlet-jsp" to process jsp/some1.jsp when "/some1.jsp" url requested?


I think many (all?) containers already map *.jsp (in any directory) to the JSP servlet, so writing such an explicit servlet-mapping is only necessary if you want to use custom file extensions for your JSPs. To state it more directly: you can probably just remove the servlet-mapping you have written.

Forwarding requests for JSP files in / to /jsp might best be accomplished by defining a filter mapping. You will also need to write your own filter class. Filters are a little like Servlets, but instead of generating content like a JSP or Servlet would, they are a bit more like a traffic controller, [re]directing requests.


You can create a servlet that is mapped to /jsp/ then parse the path after the servlet mapping and forward to the JSP, using request.getRequestDispatcer(targetJsp).forward()


<servlet-mapping>
    <servlet-name>servlet-jsp</servlet-name>
    <url-pattern>/jsp/*.jsp</url-pattern>
</servlet-mapping>

I think this should do the trick.

0

精彩评论

暂无评论...
验证码 换一张
取 消