Make a note of how to get the URL of the transition source. This time, while creating the login function, if the login was successful, I used it to move to the page that was open before login (the page where the login button was pressed).
The flow is as follows.
The page that was open before login (jsp) ↓ Login.java (** Get the URL of the page that was open before login **, transition to the login form) ↓ login.jsp (login form) ↓ LoginCheck.java (Determine if you can log in, ** Extract the URL of the page that was open before login **) ↓ The page that was open before login (return to this page if login is successful)
In conclusion, use the ** getHeader method ** to get the ** Referer ** of the request header.
//Get the transition source URL of the request header information request.getHeader("REFERER");
This time, we will use a session to hold the acquired URL, and if the login is successful, we will retrieve it and transition.
//Acquires the transition source URL of the request header information and stores it in the session session.setAttribute("referer", request.getHeader("REFERER")); request.getRequestDispatcher("/login.jsp").forward(request, response);
//If login is successful, take out the transition source URL from the session and transition String url = (String)session.getAttribute("referer"); request.getRequestDispatcher(response.encodeURL(url).substring(29)).forward(request, response);
When I tried to transition from LoginCheck.java to the page before login, I got a 404 HTTP error and had a hard time. The reason is that the URL obtained by referer is the whole URL, so I was trying to forward by specifying unnecessary parts. The workaround is simple, just use ** substring to cut out the required parts and it's OK **.
Since you don't need the "
http: // localhost: 8080 / sample "part, you can cut out only /top.jsp by giving ** substring (29) ** to the URL.