How do I get servlet request URL information?
Category: javax.servlet , viewed: 139K time(s).
In the example 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().
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.
//
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();
}
}
Please enable JavaScript to view the comments powered by Disqus.
Powered by
More examples on javax.servlet