How to Get Referer URL Servlet

This example explains you how to get the referer url in Servlet. This tutorial explains you all the steps of getting the referer url in Java Servlet. In this example we will use the Eclipse IDE for compiling and Tomcat 7 as server for deploying the Servlet.

How to Get Referer URL Servlet

How to Get Referer URL Servlet

In this section we will discuss about getting the referer url in Java.

This example explains you how to get the referer url in Servlet. This tutorial explains you all the steps of getting the referer url in Java Servlet. In this example we will use the Eclipse IDE for compiling and Tomcat 7 as server for deploying the Servlet.

Example

Here I am giving a simple example which will demonstrate you about how to get the referer url in Java. To accomplish this task we will create a dynamic web project in Eclipse then we will create a html file in Web Content folder then create a package in src folder then created Servlet inside the package. In the first servlet I have used the RequestDispatcher to transfer the control to the other servlet where I have used the getHeader("referer") method which returns the string as referer url.

Directory Structure

Source Code

refererUrl.html

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<form action="FirstServlet" method="post">
<p>Click on the button : 
<input type="submit" value="Get Referer URL"/>
</p> 
</form>
</body>
</html>

FirstServlet.java

package roseindia.net;

import javax.servlet.*;
import javax.servlet.http.*;

import java.io.*;

public class FirstServlet extends HttpServlet{

	public void doPost(HttpServletRequest request, HttpServletResponse response)
	  throws IOException, ServletException
	  {
		response.setContentType("text/html");
		
		RequestDispatcher rd = request.getRequestDispatcher("SecondServlet");
		rd.forward(request, response);
	  }
}

SecondServlet.java

package roseindia.net;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


public class SecondServlet extends HttpServlet {
	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	
		PrintWriter out = response.getWriter();
		String url = request.getHeader("referer");
		out.println("Refered URL = "+url);
		
	}

}

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>refererUrlExample</display-name>
<welcome-file-list>
<welcome-file>refererUrl.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<servlet> 
<servlet-name>FirstServlet</servlet-name>
<servlet-class>roseindia.net.FirstServlet</servlet-class>
</servlet> 
<servlet>
<servlet-name>SecondServlet</servlet-name>
<servlet-class>roseindia.net.SecondServlet</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>FirstServlet</servlet-name>
<url-pattern>/FirstServlet</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>SecondServlet</servlet-name>
<url-pattern>/SecondServlet</url-pattern>
</servlet-mapping> 
</web-app>

Output

When you will execute the above example you will get the output as follows :

When you will click on the button you will get the output as follows :

You can download the source code as war file from the link given below.

Download Source Code