getProtocol() vs getScheme()


Both are defined in ServletRequest interface. Using these methods, the servlet programmer can know the protocol used by the client to call the servlet. They differ slightly in the output.

getProtocol() returns the protocol with version and getScheme() returns protocol without version. See the output screen at the end.

Let us see what Java API says as defined in javax.servlet.ServletRequest interface.

  • public 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.
  • public String getScheme():
    Returns the name of the scheme used to make this request, for example, http, https, or ftp. Different schemes have different rules for constructing URLs, as noted in RFC 1738.
getProtocol() vs getScheme()

Let us see the difference programmatically.

HTML Program: ClientData.html


Enter Your Name

Observe, the client uses GET value for attribute METHOD.

web.xml entry for ClientInformation servlet


  efgh
  ClientInformation



  efgh
  /jasmine

Servelet Program: ClientInformation.java

import javax.servlet.*; 
import javax.servlet.http.*;
import java.io.*; 

public class ClientInformation extends HttpServlet
{
  public void service(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException
  {
    res.setContentType("text/html");
    PrintWriter out = res.getWriter();

    String str1 = req.getProtocol();
    out.println("req.getProtocol() : " + str1);

    String str2 = req.getScheme();
    out.println("

req.getScheme() : " + str2); out.close(); } }

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

getProtocol() vs getScheme()

The output screen when submit button is clicked.

getProtocol() vs getScheme()

Observe the difference between the methods – one with version of the protocol and the other without version.

Pass your comments and suggestions to improve the quality on this tutorial "getProtocol() vs getScheme()".

Leave a Comment

Your email address will not be published.