0

Nothing happens when my Servlet use RequestDispatcher.forward

My servlet:

    request.setAttribute("mB", mB);
    
    RequestDispatcher rd = request.getRequestDispatcher("/index.jsp");
    try {
        rd.forward(request, response);
    } catch (ServletException|IOException e) {
        e.printStackTrace();
    }

The file index.jsp is in my WebContent folder, but the index.jsp is not forwarded when I call the Servlet.

My project structure:

my web.xml:

<servlet>
    <servlet-name>Servlet</servlet-name>
    <servlet-class>logic.Servlet</servlet-class>
</servlet>

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

How can I resolve this problem? How can I get information from forward error? I try to debug and I found that the rd has a requestURI value that coincides with MyProject/index.jsp that is the URI that I want.

EDIT: My Servlet is called by a JavaScript page, not how I say first. This is the js function:

function onSignIn(googleUser) {
    var xhr = new XMLHttpRequest();
    xhr.open('POST', '/MyProject/servlet');
    xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    xhr.send('idtoken=' + id_token + '&page=' + window.location.href);
}

EDIT 2: I think the problem is the Google Sign-in button that I use to call the js file.

<div class="googleLogin" id="signin-container">
    <div class="g-signin2" data-onsuccess="onSignIn"></div> 
</div>
<div class="googleLogout" id="signout-container">
    <h4 class="font"><a href="#" onclick="signOut();">Sign out</a></h4>
</div> 
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Simone
  • 425
  • 1
  • 6
  • 17

1 Answers1

0

EDIT: You should have mentioned you are using AJAX to call your servlet.

Based on the edit you made to the question, it seems you are not doing anything with the response you get from the server. An AJAX request is asynchronous and made in the background. The page doesn't refresh to get a response. You get a response on the AJAX connection. Various events are triggered for the response and you need to use the onload event to receive your response and do something with it.

Look at this example that shows you how to use the XMLHttpRequest to handle the response from the server.

Also, as the question from the duplicate shows, it's often useful to use JQuery instead of the more low level XMLHttpRequest object.

Answer before EDIT: If nothing happens when you forward to your JSP, it most likely means you have an error on forwarding (it might be some syntax error in the JSP or some other reason).

The problematic code is this:

try {
    rd.forward(request, response);
} catch (ServletException|IOException e) {
    e.printStackTrace();
}

If you get an exception here, it is just logged by the server, then nothing happens in your servlet. You should check the server logs.

The fastest way to see the error though would be to just remove the try-catch and let the server generate an error message in your browser page. You basically do just this code, without the try-catch:

rd.forward(request, response);

That way you can actually see what's going on. Catching exceptions like that and writing it to standard output is a bad practice anyway and you shouldn't do it.

Bogdan
  • 23,890
  • 3
  • 69
  • 61
  • No errors are shown again – Simone Jan 21 '21 at 16:11
  • That can't be. Are you calling the right servlet? Do you have any content inside the JSP file? Have you cleared your browser cache or opened the servlet in an incognito window to make sure the browser isn't caching the response of the servlet? – Bogdan Jan 21 '21 at 16:18
  • Yes, I can enter inside the servlet to call some methods, and no, my jsp is not empty. But I can't enter into the Servlet in the incognito window because the Google Sign in need the cookies – Simone Jan 21 '21 at 16:55
  • I just saw the updates to the question. Your JS code calls the `/loginGuiControlServlet` servlet, but you are showing a mapping for `/servlet`. Have you changed something? – Bogdan Jan 21 '21 at 17:04
  • was a typo for simplifying the code – Simone Jan 21 '21 at 17:41