getScheme() Method Example

getScheme() method is defined in ServletRequest interface from javax.servlet package inherited by HttpServletRequest.

With getScheme() method, the Servlet Programmer can know what protocol (like HTTP) is used by the client from HTML file to call the Servlet.

Let us see what Java API says about this method.

  • public java.lang.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.

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

Let us see the output by writing a program.

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 str = req.getScheme();
    out.println("req.getScheme() :  " + str);

    out.close();
  }
}

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

getScheme()

The output screen when submit button is clicked.

ima

To get the protocol with version, use getProtocol() method.

Leave a Comment

Your email address will not be published.