• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

RequestDispatcher help

So im trying to uncouple my rather tightly coupled webpage by using a FrontController servlet that will route all requests to the other servlets, it comtains code like:

Code:
if (currentRequestString.equals(GlobalValues.getDesktopLoginPage()))
        {
            System.out.println("Desktop login path");
            rd = request.getRequestDispatcher("/"+"LoginPageServlet");
            rd.forward(request, response);
        }
        else if (currentRequestString.equals(GlobalValues.getMobileLoginPage()))
        {
            System.out.println("Mobile login path");
            rd = request.getRequestDispatcher("/"+"LoginPageServlet");
            rd.forward(request, response);
        }

And so on... They both go to the same servlet right now, thats okay though. Here is the LoginPageServlet code that the FrontController sends these particular requests to:

Code:
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException
    {   
        System.out.println("LoginPageServlet executing");
        HttpSession session = request.getSession(false);
        if(session == null) //path for invalid session
        {
            response.sendRedirect("desktopLogin.html");
        }
        else if(session.getAttribute("username") == null) //path for invalid username
        {
            //no username in session
            //user probably hasn't logged in properly
            response.setContentType("text/html");
            response.sendRedirect("desktopLogin.html");
        }
        else //path for valid session
        {
            response.sendRedirect("alreadyLoggedIn.html");
            
        }
    }

Unfortunately this redirects to the URL: "http://localhost:8080/fitness_tracker_servlet_maven/FrontController/desktopLogin.html"

When what I want it to do is redirect to:
"http://localhost:8080/fitness_tracker_servlet_maven/desktopLogin.html"

I assume this is because I am using RequestDispatcher and I theres something im missing about how it all works. I can fix it by doing this:

Code:
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException
    {   
        System.out.println("LoginPageServlet executing");
        HttpSession session = request.getSession(false);
        if(session == null) //path for invalid session
        {
            response.sendRedirect[B]("/fitness_tracker_servlet_maven/desktopLogin.html")[/B];
        }
        else if(session.getAttribute("username") == null) //path for invalid username
        {
            //no username in session
            //user probably hasn't logged in properly
            response.setContentType("text/html");
            response.sendRedirect[B]("/fitness_tracker_servlet_maven/desktopLogin.html")[/B];
        }
        else //path for valid session
        {
            response.sendRedirect("alreadyLoggedIn.html");
            
        }
    }

But it would still be good to know why that happens with "FrontController" being part of the URL response.
 
Hmm I found ServletContext and I guess I can do this with LoginPageServlet:

Code:
public class LoginPageServlet extends HttpServlet
{

    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
     * methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException
    {   
        System.out.println("LoginPageServlet executing");
        HttpSession session = request.getSession(false);
        if(session == null) //path for invalid session
        {
            ServletContext sc = this.getServletContext();
            response.sendRedirect(sc.getContextPath() +"/"+ GlobalValues.getDesktopLoginPage());
        }
        else if(session.getAttribute("username") == null) //path for invalid username
        {
            //no username in session
            //user probably hasn't logged in properly
            ServletContext sc = this.getServletContext();
            response.sendRedirect(sc.getContextPath() +"/"+ GlobalValues.getDesktopLoginPage());
        }
        else //path for valid session
        {
            response.sendRedirect("alreadyLoggedIn.html");
            
        }
    }

Weird thing though... It needs that "/" to be directly there in the servlet to work. GlobalValues.getDesktopLoginPage() returns the string "desktopLogin.html"

It will not work if I put the slash into the GlobalValues class and GlobalValues.getDesktopLoginPage() returns "/desktopLogin.html" the slash just gets ignored 😕
 
from the docs...

void sendRedirect(String location)
throws IOException

Sends a temporary redirect response to the client using the specified redirect location URL. This method can accept relative URLs; the servlet container must convert the relative URL to an absolute URL before sending the response to the client. If the location is relative without a leading '/' the container interprets it as relative to the current request URI. If the location is relative with a leading '/' the container interprets it as relative to the servlet container root.
 
Back
Top