getProtocol() Method Example

getProtocol() Method Example


getProtocol() method is defined in ServletRequest interface from javax.servlet package and inherited by HttpServletRequest.
With getProtocol() method, the Servlet Programmer can know what protocol (like HTTP/1.0 or HTTP/1.1) is used by the client from HTML file to call the Servlet.

Let us see what Java API says about this method.

  • java.lang.String getProtocol():
    Returns the name and version of the protocol the request uses in the form protocol/majorVersion.minorVersion, for example, HTTP/1.1. For HTTP servlets, the value returned is the same as the value of the CGI variable SERVER_PROTOCOL.

The method getProtocol() returns a string, the protocol used in ACTION attribute of of FORM tag used by the client to call the servlet.

Example on getProtocol(): Let us see the output by writing a program.

HTML Program: ClientData.html

Observe, the client uses GET value for attribute METHOD.

web.xml entry for ClientInformation servlet

Servelet Program: ClientInformation.java

Output screen of ClientData.html with text field filled up. Anyhow, the text field does not have any purpose here.

getProtocol()

The output screen when submit button is clicked.

getProtocol()

Leave a Reply

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