Share on Google+Share on Google+

Java Servlet : Get URL Example

In this tutorial, You will learn how to get url of servlet.

Java Servlet : Get URL Example

In this tutorial, You will learn how to get url of servlet.

Servlet getRequestURL :

getRequestURL() method reconstruct the requested client URL. It returns the current URL which containing protocol, server name, port number and server path. It doesn't include query string parameters. It returns URL in StringBuffer in place of String so if required you can add query string parameters.

Example :  In this example we are using getRequestURL() method to get current URL.

package net.roseindia;

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

public class GetURLExample extends HttpServlet {
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		PrintWriter out = response.getWriter();

		out.println("Current URL : " + request.getRequestURL());

web.xml -

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="" xmlns="" xmlns:web="" xsi:schemaLocation="" id="WebApp_ID" version="2.5">

<!--GetURL servlet mapping-->



Output :


Posted on: October 4, 2012 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.


Advertisement null