0

The problem

I'm creating a web app where the user is served a login page, login.html, and has to enter his credentials. The servlet class grabs the information using ajax in the form of a POST request, generated by the user clicking the submit button. After checking the correctness of the credentials, i wish to serve the user with a new HTML page, a welcome page, where the response of the servlet is transferred. The problem i'm facing is how to transfer the response from a starting LoginServlet class to a WelcomeServlet all the meanwhile, the client is projecting a new HTML page, welcome.html, and catch the response of the WelcomeServlet in an ajax call made by a js script in the new HTML page.

Background

I'm just starting to delve into the development of web-apps, so if i'm mistaken in any part of my logic and understanding of the whole frontend-to-backend process, please say so. I'm aware of the redirect() and forward() functions but i'm not understanding the way these functions can be used in conjunction, or the complete difference, with the client side of things.

I have the following servlets:

public class LoginServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        RequestDispatcher dispatcher = request.getRequestDispatcher("welcomeservlet");
        dispatcher.forward(request, response);
}

public class WelcomeServlet extends HttpServlet {
    private static final long serialVersionUID = 2L;

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //parse request and send response
}

In the login.html file i've included the following js code:

function loadNewPage() {
    // retrieve data and store them in a js Object
    $.ajax({
        url: "loginservlet",
        type: 'POST',
        dataType: 'json',
        data: JSON.stringify(jsObject),
        contentType: 'application/json',
        mimeType: 'application/json',

        success: function (data) {
            window.location.href = "welcome.html";
        },
        error: function (data, status, er) {
            alert("error: " + data.text + " status: " + status + " er:" + er);
        }
    });
}

I'm not including another js script which would be placed inside the welcome.html purposely since i don't know what would i have to add in there to catch the response of the new servlet. A possible reason why this whole thing isn't working could be that i'm missing something in the functionality of the forward() function but couldn't find an example online that does the exact thing that i want to do.

Invalid_Path
  • 321
  • 3
  • 8

1 Answers1

1

enter image description here

(Image credit w3schools)

The fact is you can't jump to another servlet. Ajax response will be sent back to the same webpage from which request was generated.

For your requirement what you can do is, Check whether login is success or not in LoginServlet. If yes create a token, save it in database along with the username and send the same token as response to the client.

Now in client save the token in localStorage and redirect to welcome.html.

On welcome.html page loading check whether token saved in localStorage exists or not. If yes, check whether it's valid or not. If valid the call WelcomeServlet . Else display login screen.

It's called token based authentication. You can read more about it here

Dev
  • 378
  • 2
  • 16