Showing posts with label http or https. Show all posts
Showing posts with label http or https. Show all posts

Tuesday, 12 August 2014

Find if a url is http or https


How to find if a url is http or https in a J2EE application ?

 This can be done using request.getScheme() 

This method is defined in ServletRequest interface from javax.servlet package inherited by HttpServletRequest. 
public java.lang.String getScheme(): Returns the name of the scheme used to make this request, for example, http, https, or ftp .

eg - 
  import javax.servlet.*;
 import javax.servlet.http.*;
 import java.io.*;  
 public class SchemeInfo 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(); 
}