Free Data Structures and Algorithms Course









Subscribe below and get all best seller courses for free !!!










OR



Subscribe to all free courses

How to display Request url Information through a Servlet ?

Program to display Request Url Information through a Java Servlet

package com.hubberspot.jsp.servlets.examples;

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

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

@WebServlet("/UrlInformationServlet")
public class UrlInformationServlet extends HttpServlet {
  
 protected void doGet(
   HttpServletRequest request, 
   HttpServletResponse response) 
   throws ServletException, IOException {
  
  doPost(request, response);
 }

 
 protected void doPost(
   HttpServletRequest request, 
   HttpServletResponse response) 
   throws ServletException, IOException {
  //
  // For getting servlet request URL, call 
  // getRequestURL() method on request object
  //
  String urlInfo = request.getRequestURL().toString();
  
  //
  // For getting servlet request URI, call 
  // getRequestURI() method on request object
  //
  String uriInfo = request.getRequestURI();
    
  //
  // For getting servlet request scheme, call 
  // getScheme() method on request object
  //
  String schemeInfo = request.getScheme();
  
  //
  // For getting servlet request Server port, call 
  // getServerPort() method on request object
  //
  int portNumber = request.getServerPort();
  
  //
  // For getting servlet request extra query, call 
  // getQueryString() method on request object
  //
  String query = request.getQueryString();
   
  //
  // For getting servlet request Server name, call 
  // getServerName() method on request object
  //
  String serverName = request.getServerName();
    
  //
  // For getting servlet request context path , call 
  // getContextPath() method on request object
  //
  String contextPath = request.getContextPath();
  
  //
  // For getting servlet path, call 
  // getServletPath() method on request object
  //
  String servletPath = request.getServletPath();
  
  //
  // For getting servlet request path , call 
  // getPathInfo() method on request object
  //
  String path = request.getPathInfo();
  
  response.setContentType("text/html");
  PrintWriter out = response.getWriter();
  out.print("Url of the request : " +
                      urlInfo + "<br/>");
  out.print("Uri of the request : " + 
                      uriInfo + "<br/>");
  out.print("Scheme of the request : " + 
                      schemeInfo + "<br/>");
  out.print("Server Name of the request : " 
                      + serverName + "<br/>");
  out.print("Port Number of the request : "
                      + portNumber + "<br/>");
  out.print("Context Path of the request : "
                      + contextPath + "<br/>");
  out.print("Servlet Path of the request : " +
                      servletPath + "<br/>");
  out.print("Path Info of the request : "
                      + path + "<br/>");
  out.print("Query of the request : "
                      + query);
  }
 }




Output of the program :



 
© 2021 Learn Java by Examples Template by Hubberspot