Fabio Olivetto
Fabio Olivetto

Reputation: 616

Servlet with package doesn't work and copies the package in the url twice

EDIT : Was marked as duplicate (Servlet returns "HTTP Status 404 The requested resource (/servlet) is not available") I don't have any issue on setting up the servlet the first time, everything works and when i submit it loads the results properly. The duplicate shows how to set up xml and jsp and i have them set in the same way. If i don't fill them once i have the normal retry but the second time regardless if it's correct or not the url becomes .../one/one/main, resulting in 404.

If i set the url pattern of xml to the class name without the package ( in the example <url-pattern>/servlet</url-pattern> ) the servlet doesn't even start at the first correct try. Regardless of the url pattern and referencing from form, on url pattern i have /one/Main, on form one/Main. In the duplicate any of the example of form i tried result in a 404 after the submit. I have tried to remove the xml servlet configuration and using @WebServlet from the servlet with this ( @WebServlet(description = "Main", urlPatterns = { "/one/Main" }) ), I have the same issue, first time works, second try it's one/one/Main in the url.

In this simple servlet whenever i use a package it changes the url to ../one/one/Main when i don't fill both input text fields the second time.

public void doPost(HttpServletRequest request
        , HttpServletResponse response) throws ServletException, IOException{

    String action = request.getParameter("action");
    String url = "/index.jsp";
    if(action==null)
        action="join";

    if(action.equals("join"))
        url="/index.jsp";
    else if(action.equals("ok")){
        String fName = request.getParameter("firstName");
        String sName = request.getParameter("secondName");
        if(fName==null||sName==null||fName.equals("")||sName.equals("")){
            url = "/index.jsp";
            request.getSession().setAttribute("message"
                    , "Please fill all the fields");
        }
        else{
            url = "/thanks.jsp";
            request.getSession().setAttribute("user"
                    , new User(fName, sName));
        }
    }
    getServletContext().getRequestDispatcher(url)
        .forward(request, response);
}

jsp form part

<form action="one/Main" method="post">..</form>

xml servlet mapping

<servlet>
  <servlet-name>Main</servlet-name>
  <servlet-class>one.Main</servlet-class>
</servlet>
<servlet-mapping>
  <servlet-name>Main</servlet-name>
  <url-pattern>/one/Main</url-pattern>
</servlet-mapping>

I have tried to copy the classes outside the one package and reconfigure the form tag and xml accordingly ( set servlet class to Main, pattern to /Main and jsp action to Main ), at that point it works perfectly, i can miss the filling of the fields how many times i want, it will keep reloading the proper page with the message, but as soon as the servlet and User javabean are inside the one package when i run and miss the inputs twice, the url changes to double package /Main and i have a 404.

Upvotes: 0

Views: 77

Answers (1)

Roman C
Roman C

Reputation: 1

The problem is that you are using relative path in the JSP to submit the form. As soon as JSP is not on the same base path from your servlet you have different base path after you submit the form.

You should read 5.1.2 Relative URLs to understand how relative URLs work:

A relative URL (defined in RFC1808) doesn't contain any protocol or machine information. Its path generally refers to a resource on the same machine as the current document. Relative URLs may contain relative path components (".." means one level up in the hierarchy defined by the path), and may contain fragment identifiers.

Relative URLs are resolved to full URLs using a base URL. RFC1808 defines the normative algorithm for this process.

As an example of relative URL resolution, assume we have the base URL http://www.acme.com/support/intro.html. The relative URL in the following markup for a hypertext link:

 <A href="suppliers.html">Suppliers</A>

would expand to the full URL http://www.acme.com/support/suppliers.html, while the relative URL in the following markup for an image

<IMG src="../icons/logo.gif" alt="logo">

would expand to the full URL http://www.acme.com/icons/logo.gif.

Upvotes: 0

Related Questions