How do I get servlet request URL information?

Category: javax.servlet, viewed: 44543 time(s).
package org.kodejava.example.servlet;

import java.io.IOException;

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

public class ServletUrlInformation extends HttpServlet implements Servlet {
    protected void doGet(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }

    protected void doPost(HttpServletRequest request,
	        HttpServletResponse response) throws ServletException, IOException {
        //
        // Getting servlet request URL
        //
        String url = request.getRequestURL().toString();

        //
        // Getting servlet request query string.
        //
        String queryString = request.getQueryString();

        //
        // Getting request information without the hostname.
        //
        String uri = request.getRequestURI();

        //
        // Below we extract information about the request object path
        // information. We extract the protocol user, server and and its 
        // assigned port number. We extract our application context path, 
        // servlet path, path info and the query string information. If we 
        // combaine all the information below we'll get someting equals to 
        // the request.getRequestURL().
        //
        String scheme = request.getScheme();
        String serverName = request.getServerName();
        int portNumber = request.getServerPort();
        String contextPath = request.getContextPath();
        String servletPath = request.getServletPath();
        String pathInfo = request.getPathInfo();
        String query = request.getQueryString();
    }
}
Click here to lend your support to: Kode Java Org and make a donation at www.pledgie.com !

 

Can't find what you are looking for? Join our FORUMS and ask some questions!
Download Hundreds of Complimentary Industry Resources

Get hundreds of popular Industry magazines, white papers, webinars, podcasts, and more; all available at no cost to you. With more than 600 complimentary offers, you'll find plenty of titles to suit your professional interests and needs. Click Here and Sign up today!

Sponsored Links

Our Friends

Statistics

Locations of visitors to this page
visitor stats