Way2Java

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.

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.

The output screen when submit button is clicked.

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()".