0

Possible Duplicate:
How to use relative paths without including the context root name?

In my Tomcat webapps directory, I have a context, app, that contains in its WEB-INFO/classes directory a tree named test, with a bunch of class files under it. app also contains an html file whose purpose is to collect user input and then invoke one of the servlets in the classes/test directory to generate HTML output. The web.xml describing the servlet is as follows:

<servlet>
  <servlet-name>foo</servlet-name>
  <servlet-class>test.DoIt</servlet-class>
</servlet>

<servlet-mapping>
  <servlet-name>foo</servlet-name>
  <url-pattern>/doit</url-pattern>
</servlet-mapping>

The body of the html file is

<FORM ACTION="/app/doit">
  First Parameter: <INPUT TYPE="TEXT" NAME="param1">
  <CENTER><INPUT TYPE="SUBMIT"></CENTER>
</FORM>

This all works. But it seems somehow wrong to have to specify the name of the app in the form action of the html file. Is there a way to avoid this, or can someone explain to me why it makes sense that you should have to? Thanks.

Community
  • 1
  • 1
rogerl
  • 185
  • 2
  • 8
  • 1
    Where did you learn HTML? All those uppercased tags/attributes transfers me back in time to early 90's, almost two decades back. Even more, the `
    ` element is deprecated since HTML 4.01 in 1998. Is your HTML learning resource really that old or poor?
    – BalusC Jan 10 '13 at 02:52

2 Answers2

0

If you are disciplined, you don't have to. Imagine having /app/form.html and /app/doit servlet. Since from the browser perspective they are in the same folder, you can simply say:

<FORM ACTION="doit">

Of course relative paths work as well, e.g. with /app/pages/form.html and /app/servlets/doit you can say:

<FORM ACTION="../servlets/doit">
Tomasz Nurkiewicz
  • 334,321
  • 69
  • 703
  • 674
  • So you propose putting the servlets and the associated html in the same directory, rather than putting servlets down the tree, where they would normally belong in their package directory under classes? If that's what you're saying, then presumably this is a deploy-time arrangement, since you clearly want to compile the java code in the proper place in its source tree. Right? – rogerl Jan 09 '13 at 19:20
  • @rogerl: No and no :-). Servlets path as seen by the browser is controlled via `web.xml` (which you already know), package name has nothing to do here. HTML pages go to `/src/main/webapp` (in maven), completely separated from classes. Also they don't have to be in the same package/directory, see my example with relative paths. – Tomasz Nurkiewicz Jan 09 '13 at 19:44
  • Sorry for being so dense, but if I just (as I think your comment implies) change the action path in the form to just "doit", things fail with a 404 (leaving the web.xml file as above). Clearly I'm misunderstanding your comments. Thanks for bearing with me. – rogerl Jan 10 '13 at 00:37
  • @rogerl: you can only change action to bare `doit` *if* your `form.html` is in the same path (from browser perspective) as servlet. Otherwise you will need a relative path (see my answer for two examples) – Tomasz Nurkiewicz Jan 10 '13 at 07:22
0

If you're working with JSP pages, the <c:url> tag of the Core JSTL automatically adds the deployment context to any absolute URL. So you can safely write:

<FORM ACTION="<c:url value="/doit"/>">

And the generated URL in the action attribute will be /app/doit.

Med
  • 628
  • 9
  • 29