Klarow Klarow - 11 days ago 5
Java Question

Why dispatch from one servlet to another double the pathInfo?

Hi and please excuse my english,

I have one main servlet which dispatch all user action and one to manage request to DAO Java class. First action (click on menu) works and give me the right URL: projet/admin/groupes/gerer (project/admin/bands/manage). Second action (click on button) double the pathInfo: projet/admin/groupes/admin/groupes/creer (create).

If I take the double path into account, it works but it's not a good solution (and if I try to click on the menu again, it triple!)

Calls to servlet come from a JavaScript :

switch (name) {
case 'Groupe': // Click on band menu from administration side
console.log('JS: Click on admin menu Groupe');
var url = "admin/groupes/gerer";
location.href = url;
break;

case 'Creer': // Click on 'Creer' button from liste-groupe page
console.log('JS : click on Creer');
var url = "admin/groupes/creer";
location.href = url;
break;

default:
console.log('JS: no click source');
}


Main servlet with urlPattern: "/admin/groupes/*", I should write "/creer" on the second case.

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

pathInfo = request.getPathInfo();
servletPath = request.getServletPath();
System.out.println("BandController, pathInfo: " + pathInfo); // TEST CODE
System.out.println("BandController, servletPath: " + servletPath); // TEST CODE

switch (pathInfo) {

// Dispatch to the management servlet
case "/gerer":
System.out.println("BandController : groupes (menu) > BandManagement"); // TEST CODE
dispatch = request.getRequestDispatcher("/action/gerer/listergrp");
dispatch.forward(request, response);
break;

// FIXME fix double URL call!
case "/admin/groupes/creer" :
System.out.println("BandController : liste-groupes.jsp > nouvellefiche.jsp"); // TEST CODE
dispatch = request.getRequestDispatcher("/WEB-INF/jsp/admin/nouvellefiche.jsp");
dispatch.forward(request, response);
break;

default:
System.out.println("BandController : page 404."); // TEST CODE
break;

}


I hope that's enough information, I tried several different kind of path in the RequestDispatcher without success. And I just find this topic Understanding URL mapping using DispatcherServlet using alwaysUseFullPath property which talk about double path but didn't understand the answer.

Thanks for any help (by the way I'm a Java beginner!)

Answer

That happens because you're not referencing the context path at the JavaScript calls. Change your JS code to:

switch (name) {
    case 'Groupe': // Click on band menu from administration side
        console.log('JS: Click on admin menu Groupe');
        var url = "${pageContext.request.contextPath}/admin/groupes/gerer";
        location.href = url;
        break;

    case 'Creer': // Click on 'Creer' button from liste-groupe page
        console.log('JS : click on Creer');
        var url = "${pageContext.request.contextPath}/admin/groupes/creer";
        location.href = url;
        break;

    default:
        console.log('JS: no click source');
}

This way, you'll always guarantee that the request URL will be formed from the project context path.


EDIT:

In case you're making the call from a JS file, the EL won't work once it is not inside a JSP page, directly. For this, you can store the context path in a hidden input field ang get its value at your JavaScript code:

HTML:

<input type="hidden" id="contextPath" value="${pageContext.request.contextPath}"/>

JS:

var contextPath = document.getElementById('contextPath').value;
switch (name) {
    case 'Groupe': 
        var url = contextPath;
        location.href = url;
        break;

// ...
Comments