0

I have the following setup. The folder itext is in the top level in webapps:

├───itext
│   └───WEB-INF
│       ├───classes
│       │   └───com
│       │       └───imparator
│       │           └───ist
│       └───lib

In the web-inf folder I have a web.xml file where I do the servlet mapping:

<servlet>
    <servlet-name>Itext Servlet</servlet-name>
    <servlet-class>com.imparator.ist.ItextServlet</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>Itext Servlet</servlet-name>
    <url-pattern>/fincc-itext</url-pattern>
</servlet-mapping>

Now when I want something to be served by this servlet here is my action attribute in a form:

<form method="POST" action="/itext/fincc-itext">

========================================================================================

Now, suppose that the itext folder was not in the top level folder in webapps but a couple of levels below:

├───level1
│   └───level2
│       └───itext
│           └───WEB-INF
│               ├───classes
│               │   └───com
│               │       └───imparator
│               │           └───ist
│               └───lib

How do I update the servlet mapping and the action attribute value to correspond to this directory structure?

Cratylus
  • 52,998
  • 69
  • 209
  • 339
oneiros
  • 3,527
  • 12
  • 44
  • 71
  • Note that the context path in URLs is not necessarily the folder name of the main webapp folder as it is been deployed. It's configureable in server config. You should always prefer to specify it dynamically by `HttpServletRequest#getContextPath()` or `${pageContext.request.contextPath}` instead of hardcoding it in your HTML/JSP. See also http://stackoverflow.com/questions/4764405/how-to-use-relative-paths-without-including-the-context-root-name – BalusC Mar 28 '12 at 20:29

2 Answers2

1

Those won't change unless the context changes. The context is itext since that's where you WEB-INF is.

Todd Murray
  • 423
  • 2
  • 7
  • meaning? I still send requests to /itext/fincc-itext ? – oneiros Mar 28 '12 at 20:26
  • That way the request is sent to http://localhost:8080/itext/fincc-itext which is actually not the location of the servlet ... level1 and level2 need to be included in the path ? yes/no? – oneiros Mar 28 '12 at 20:27
  • That indeed won't change. Besides that, the proposed folder structure does not comply servlet spec and the servletcontainer would fail to deploy the webapp successfully. – BalusC Mar 28 '12 at 20:28
  • BalusC - Why does it violate the servlet specs? Does it have to be in the root folder of webapps? – oneiros Mar 28 '12 at 20:39
  • Why should it not violate the specs? Why should the developers need to worry about setting the paths right in their code to avoid distaster when the enduser deploys it the different way? That's exactly why the proper folder structure is in detail specified in the servlet spec. So whenever you as being the developer intend to deviate from the standard, then everyone can simply say that you're doing it wrong, no excuses :) – BalusC Mar 28 '12 at 23:53
  • BalusC - I am going to ask a dumb question - is the proper folder structure in this case, itext residing in the root level of webapps? – oneiros Mar 29 '12 at 11:35
1

The setting of an web app have to be:

webapp
    -somefolders (you can put text, js, css, image files here)
    - WEB-INF
        - classes
        - lib (contains jars)
        - web.xml
        - xml or configuration files
    - META-INF (if needed)

It is the standard and you have to follow. If you want to change the servlet path you have change it in web.xml. In your situation:

+---itext (web app name)
¦   +---somefolders (you can put text, js, css, image files here)
¦   ¦
¦   +---META-INF (if needed)
¦   ¦
¦   +---WEB-INF
¦       +---classes
¦       ¦   +---com
¦       ¦       +---imparator
¦       ¦           +---ist
¦       +---lib
¦   ¦
¦   +---web.xml
¦   ¦   
¦   +---xml or configuration files

you still need to keep your app structure and the modify the servlet path in web.xml:

<servlet-mapping>
    <servlet-name>Itext Servlet</servlet-name>
    <url-pattern>/itext/fincc-itext</url-pattern>
</servlet-mapping>
mino.me
  • 305
  • 2
  • 6