EmberTraveller
EmberTraveller

Reputation: 345

Page generation time in servlet

I have a login servlet that processes post request with username and password parameters, and does something like this:

    Instant pageGenStart = Instant.now();

    String username = req.getParameter("username");
    String password = req.getParameter("password");

    resp.setContentType("text/html;charset=utf-8");

    User user = null;
    try {
        user = userService.getByUsername(username);
    } catch (SQLException e) {
        e.printStackTrace();
    }
    if (user == null && !password.equals("")) {
        try {
            user = new User(username, password);
            user.setId(userService.addNewUser(username, password));

            charactersService.addNewCharacter(user);
            sessionsService.add(req.getSession().getId(), user.getId());
        } catch (SQLException e) {
            e.printStackTrace();
        }
        resp.setContentType("text/html;charset=utf-8");
        resp.setStatus(HttpServletResponse.SC_OK);
        Duration time = Duration.between(pageGenStart, Instant.now());

        resp.sendRedirect("/main");
    }

If user is not found in db, create new user and redirect him to main page. Normally i would just put this "time" variable into page, but i redirect my response to other servlet where doGet method is called. How do i let other servlet know how long login servlet took to proccess post request?

Upvotes: 0

Views: 50

Answers (1)

Kushagra Misra
Kushagra Misra

Reputation: 481

You can pass value from one servlet to another in many ways like :

  1. Storing values in session ( You have to take care of session management)
  2. Creating class having static ConcurrentHashMap and storing time gap per user session and fetching it using session ID. (same problem need to take care when to clear cache).
  3. Use already defined lib ( best option as you need not to worry about session management and cache clear).

I perfer EH-cache for these kind of perposes you can see a good example from the Link.

Upvotes: 1

Related Questions