Question

I have a simple program with:

  • Login

  • Process

  • Result

As servlets and a filter:

  • AuthentifFilter

This is my .xml:

<?xml version="1.0" encoding="UTF-8"?>

<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
         version="2.4">

  <display-name>PDPJ Servlet Application</display-name>

  <description>
        Not in english so skipping this
    </description>

    <listener>
        <listener-class>listeners.InitServlet</listener-class>
    </listener>

    <servlet>
        <servlet-name>LoginServlet</servlet-name>
        <servlet-class>servlet.LoginServlet</servlet-class>
    </servlet>

    <servlet-mapping>
        <servlet-name>LoginServlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

    <servlet>
        <servlet-name>ProcessingServlet</servlet-name>
        <servlet-class>servlet.ProcessingServlet</servlet-class>
    </servlet>

    <servlet-mapping>
        <servlet-name>ProcessingServlet</servlet-name>
        <url-pattern>/PDPJ_-_Servlet_-_V4/process/</url-pattern>
    </servlet-mapping>

    <servlet>
        <servlet-name>ResultsServlet</servlet-name>
        <servlet-class>servlet.ResultsServlet</servlet-class>
    </servlet>

    <servlet-mapping>
        <servlet-name>ResultsServlet</servlet-name>
        <url-pattern>/process/results</url-pattern>
    </servlet-mapping>

    <session-config>
        <session-timeout>1200</session-timeout>
    </session-config>

    <filter>
        <filter-name>AuthFilter</filter-name> <!-- mandatory -->
        <filter-class>filter.AuthentificationFilter</filter-class> <!-- mandatory -->

    </filter>

    <filter-mapping>
        <filter-name>AuthFilter</filter-name>
        <url-pattern>/PDPJ_-_Servlet_-_V4/process/*</url-pattern>
        <dispatcher>REQUEST</dispatcher>

    </filter-mapping>

</web-app>

Now, the processing servlet has an url of "/PDPJ_-Servlet-_V4/process/", used to be just "/process/".

Thing is, after I log in, the url should look like this http://localhost:8080/PDPJ_-_Servlet_-_V4/process/?username=admin&password=admin and it works for a manual input, however if I just click the submit button the url becomes http://localhost:8080/process/?username=admin&password=admin which I just don't get.

Here is the form:

PrintWriter pw = resp.getWriter();
pw.println("<html><head></head><body>");

pw.println("<form action=\"/process/\">  " +
        "Username:<input type=\"text\" name=\"username\"/><br/>  " +
        "Password:<input type=\"password\" name=\"password\"/><br/>  " +
        "<input type=\"submit\" value=\"login\">  " +
        "</form>  ");
pw.println("</body></html>");

Could someone point out what I'm missing ?

EDIT Also, should the .xml contain /PDPJ_-Servlet-_V4/ or can I tell it I want a "relative path" or something similar ?

Was it helpful?

Solution

You're setting it to that URL in the form (see below). The leading slash makes it absolute.


It's almost never right to generate HTML in Java; use JSP or similar for the view layer.

OTHER TIPS

Here:

pw.println("<form action=\"/process/\">

You're telling the form to post back to the URL /process/ which is an absolute path.

Well, your form's action is action="/process/", so when submit is pressed it submits the request to : <server-url>:<server-port>/process/ and there is no '/PDPJ_-Servlet-_V4in the URL, by the way there is no context name in the URL too, so even if you change theactionvalue, you can only deploy your application with nocontext` name, otherwise same thing happens.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top