5 Replies Latest reply on Oct 9, 2002 9:40 AM by hermit

    URL/Servlet location problem

    kenb

      My hunch is that the answer I am seeking is simple, I just can't seem to figure it out. I am fairly new to this and am still learning.

      I am running JBoss with Catalina. I have JSPs, Servlets, and EJBs involved in the application. Initially I have a JSP page for my login screen. This JSP page includes:

      <form action ="servlet/LoginServlet" method=POST>

      Upon selecting the form request, the browser location field shows:

      http://localhost:8080/cd/servlet/LoginServlet

      The 'LoginServlet' then does some verification involving EJBs. Upon successful login, the 'LoginServlet' does a:

      getRequestDispatcher("/DoCollection.html")

      and forwards it to this HTML page.

      Note that after loading this HTML page, the browser location field still shows the above servlet address. From the user perspective, everything is working okay thus far.

      The next step is where I am incurring a problem. From the 'DoCollection.html' page, I have a form action:

      <form action ="servlet/ListCDServlet" method=POST>

      When this action is selected, what is occurring is that the URL in the browser location field becomes:

      http://localhost:8080/cd/servlet/servlet/ListCDServlet

      Notice the additional 'servlet' component added to the path? The server cannot locate the 'ListCDServlet' at this point and I get a 404 error.

      I am stumped. Any help in this area would be appreciated.

        • 1. Re: URL/Servlet location problem
          eccolafilosofiadelpedale

          better late than never...try adding a backslash / like this:

          "/servlet/LoginServlet"

          that should do it...

          • 2. Re: URL/Servlet location problem
            mlapolla

            That doesn't work. I can get it to work on a forward but not with a form on an action.

            • 3. Re: URL/Servlet location problem
              hermit

              Yes, there is such problem because the browser only can decide the relative url through the location bar's url which is passed not exactly by the server which has disconnected with the browser. I haven't read the JSP&Servlet specification and don't know if there is a compelling demand and if the problem is a bug.

              Now the way I deal with it is only writing the absolute url. But you should notice that the difference of the context path, the request url and the servlet url and which url is read by server and which one is read by browser. The url read by browser should be written always absolutely. For example like yours, for the action attribute of form which is read by browser should be written like this:
              , but you had better write it as:
              , so to avoid the case changing the context path sometime.
              for the url which are read by server such as ones located in the tags <jsp:forward>, <jsp:include>, <%@include>, getRequestDispatcher method, etc, you should write them without the context because the server knows it. write them like yours:
              getRequestDispatcher("/DoCollection.html") or <jsp:forward page="/DoCollection.html">

              But we still hope there is a natural method. After all, always writing absolute url is a bothering thing. The best resolvent is the server passes the exact url to browser. Maybe we can do some http response header work by ourselves to give the browser an excat url? But since it can, it still not natural way.

              Han Yi

              • 4. Re: URL/Servlet location problem
                hermit

                Yes, there is such problem because the browser only can decide the relative url through the location bar's url which is passed not exactly by the server which has disconnected with the browser. I haven't read the JSP&Servlet specification and don't know if there is a compelling demand and if the problem is a bug.

                Now the way I deal with it is only writing the absolute url. But you should notice that the difference of the context path, the request url and the servlet url and which url is read by server and which one is read by browser. The url read by browser should be written always absolutely. For example like yours, for the action attribute of form which is read by browser should be written like this:
                , but you had better write it as:
                , so to avoid the case changing the context path sometime.
                for the url which are read by server such as ones located in the tags <jsp:forward>, <jsp:include>, <%@include>, getRequestDispatcher method, etc, you should write them without the context because the server knows it. write them like yours:
                getRequestDispatcher("/DoCollection.html") or <jsp:forward page="/DoCollection.html">

                But we still hope there is a natural method. After all, always writing absolute url is a bothering thing. The best resolvent is the server passes the exact url to browser. Maybe we can do some http response header work by ourselves to give the browser an excat url? But since it can, it still not natural way.

                Han Yi

                • 5. Re: URL/Servlet location problem
                  hermit

                  Yes, there is such problem because the browser only can decide the relative url through the location bar's url which is passed not exactly by the server which has disconnected with the browser. I haven't read the JSP&Servlet specification and don't know if there is a compelling demand and if the problem is a bug.

                  Now the way I deal with it is only writing the absolute url. But you should notice that the difference of the context path, the request url and the servlet url and which url is read by server and which one is read by browser. The url read by browser should be written always absolutely. For example like yours, for the action attribute of form which is read by browser should be written like this:
                  , but you had better write it as:
                  , so to avoid the case changing the context path sometime.
                  for the url which are read by server such as ones located in the tags <jsp:forward>, <jsp:include>, <%@include>, getRequestDispatcher method, etc, you should write them without the context because the server knows it. write them like yours:
                  getRequestDispatcher("/DoCollection.html") or <jsp:forward page="/DoCollection.html">

                  But we still hope there is a natural method. After all, always writing absolute url is a bothering thing. The best resolvent is the server passes the exact url to browser. Maybe we can do some http response header work by ourselves to give the browser an excat url? But since it can, it still not natural way.

                  Han Yi