getServletPath() Method Example

getServletPath() Method Example


Programmer uses getServletPath() method to retrieve the alias name used by client. getServletPath() Method is defined in ServletRequest inherited by HttpServletRequest.

This program uses getServletPath() of HttpServletRequest to retrieve the alias name (of <servlet-mapping>) tag. Client uses this alias name to call the Servlet.

Following is the method signature. Method is defined in HttpServletRequest interface.

public abstract java.lang.String getServletPath();

Example on getServletPath()

Client Program: ClientData.html

web.xml entry for ClientInformation servlet

Let us go to the coding part.

Server Program: ClientInformation.java

Output screen of ClientData.html with text field filled up.

getServletPath()

The output screen when submit button is clicked.

getServletPath()

Observe, the getServletPath() prints just the alias name and not complete URL. To retrieve complete URL, use getRequestURL() method of HttpServletRequest.

Pass your comments and suggestions on this tutorial "getServletPath() Method Example". It improves the quality of site.

Leave a Reply

Your email address will not be published. Required fields are marked *