[Java] How to get the URL of the transition source

Introduction

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)

How to get

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.

Login.java


   //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);

LoginCheck.java


   //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);

Where it was difficult

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 **.

For example

http://localhost:8080/sample/top.jsp

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.

Recommended Posts

[Java] How to get the URL of the transition source
[Rails] How to get the URL of the transition source and redirect
[Java] How to get the redirected final URL
[Java] How to get the authority of the folder
[Java] How to get the maximum value of HashMap
Source used to get the redirect source URL in Java
[Java] How to get the current directory
How to get the date in java
How to get the length of an audio file in java
How to get today's day of the week
[Java] Memo on how to write the source
[Java] How to get to the front of a specific string using the String class
How to get the absolute path of a directory running in Java
Get the URL of the HTTP redirect destination in Java
How to write Scala from the perspective of Java
As of April 2018 How to get Java 8 on Mac
[Android] How to get the setting language of the terminal
[Rails] How to get the contents of strong parameters
[Swift] How to get the document ID of Firebase
[Java] How to easily get the longest character string of ArrayList using stream
How to use Java HttpClient (Get)
How to get the class name / method name running in Java
An unsupported Java version How to get rid of errors
Get to the abbreviations from 5 examples of iterating Java lists
How to get the longest information from Twitter as of 12/12/2016
How to derive the last day of the month in Java
[jsoup] How to get the full amount of a document
How to get the class name of the argument of LoggerFactory.getLogger when using SLF4J in Java
Get the result of POST in Java
[Java] How to use the File class
[Java] How to use the hasNext function
[Swift5] How to get an array and the complement of arrays
[Java] Get the day of the specific day of the week
[java] Summary of how to handle char
[Java] How to use the HashMap class
[Java] How to use the toString () method
Studying how to use the constructor (java)
[Processing × Java] How to use the loop
How to determine the number of parallels
[Swift UI] How to get the startup status of the application [iOS]
[Java] How to set the Date time to 00:00:00
How to get the contents of Map using for statement Memorandum
How to get the id of PRIMAY KEY auto_incremented in MyBatis
Get the public URL of a private Flickr file in Java
[Java] [Maven3] Summary of how to use Maven3
[Processing × Java] How to use the class
How to sort the List of SelectItem
How to install the legacy version [Java]
Output of the book "Introduction to Java"
[Processing × Java] How to use the function
[Java] How to use the Calendar class
[Java] How to convert from String to Path type and get the path
How to check for the contents of a java fixed-length string
How to increment the value of Map in one line in Java
[Java] How to get the current date and time and specify the display format
How to switch the display of the header menu for each transition page
[Java] [ibatis] How to get records of 1-to-N relationship with List <Map <>>
How to find out the Java version of a compiled class file
How to find the total number of pages when paging in Java
How to get the value after "_" in Windows batch like Java -version
How to constrain the action of the transition destination when not logged in