Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 544709
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T10:41:03+00:00 2026-05-13T10:41:03+00:00

I am a newbie servlet programmer. I am trying to do this right. I

  • 0

I am a newbie servlet programmer. I am trying to do this right.

I wrote a filter to intercept a servlet request and check if the URL needs the user to be logged in. If so the user gets directed to the login page. This is working. But then I want to redirect the user back the page he wanted to go to in the first place. What is the correct way to keep this state? Do I just store the URL in a data structure indexed using the session id from the cookie?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T10:41:03+00:00Added an answer on May 13, 2026 at 10:41 am

    I wrote a filter to intercept a servlet request and check if the URL needs the user to be logged in.

    This doesn’t seem to sound right. You should rather map the Filter on the same url-pattern of the Servlet, or better yet, on the servlet-name of the Servlet. This way the Filter is only invoked whenever the Servlet is called.

    Back to your actual problem: when the user is not logged in, you have two options:

    1. Store the URL in session:

      if (session.getAttribute("user") == null) {
          session.setAttribute("back", httpRequest.getRequestURI());
          httpRequest.sendRedirect("login");
      } else {
          chain.doFilter(request, response);
      }
      

      which you use on login:

      User user = userDAO.find(username, password);
      if (user != null) {
          session.setAttribute("user", user);
          String back = (String) session.getAttribute("back");
          if (back != null) {
              session.removeAttribute("back");
              response.sendRedirect(back);
          } else {
              response.sendRedirect("home"); // Home page?
          }
      } else {
          // Show error?
          request.setAttribute("message", "Unknown user, please retry");
          request.getRequestDispatcher("login").forward(request, response);
      }
      
    2. Pass the URL as request parameter:

      if (session.getAttribute("user") == null) {
          httpRequest.sendRedirect("login?back=" + httpRequest.getRequestURI());
      } else {
          chain.doFilter(request, response);
      }
      

      which you pass through to subsequent request as hidden input field:

      <input type="hidden" name="back" value="${param.back}">
      

      which you use on login:

      User user = userDAO.find(username, password);
      if (user != null) {
          session.setAttribute("user", user);
          String back = request.getParameter("back");
          if (back != null) {
              response.sendRedirect(back);
          } else {
              response.sendRedirect("home"); // Home page?
          }
      } else {
          // Show error?
          request.setAttribute("message", "Unknown user, please retry");
          request.getRequestDispatcher("login").forward(request, response);
      }
      

    URL encoding as some suggest is not needed as the getRequestURI() won’t be decoded.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Newbie C++ programmer here. I'm trying to write a command line application that takes
I got this big newbie question. When i try the following; the 'servlet' turns
Newbie question... I'm trying to get form validation working with Spring Web using this
I am newbie to servlets and JSP. I am trying to call logger servlet
I m trying to embed Equinox in a Servlet Container,being a newbie, I am
Newbie question. I have Django models that look like this: class Video(models.Model): uploaded_by =
Newbie question since I'm not up to speed using maven at all. I'm trying
Newbie question. I am trying to use ActiveRecord::Base.connection.execute(..) in a ruby 3.1 app and
Newbie here (to C, objective-C, and iOS)... For this code in my AppDelegate.m, why
I'm a jstl newbie so probably this question will sound to you funny. Anyway,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.