• Post Reply Bookmark Topic Watch Topic
  • New Topic

HttpServletRequest  RSS feed

 
vineet kaushik
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

If HttpServletRequest is an interface then how do we call the getSession() declared in this interface???

Thanks,
VIneet
 
Swetha Bhagavathula
Ranch Hand
Posts: 112
Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
vineet kaushik wrote:Hi All,

If HttpServletRequest is an interface then how do we call the getSession() declared in this interface???

Thanks,
VIneet


Yes HttpServletRequest is an interface,but please remember that it contains methods which are implemented by the underlying classes(that means contains contains abstract methods(ofcourse as it is interface no methods are provided implememtation) whose definition is found in the classes implementing that interface.. please refer source code.. I have given below please see this..(from source code of servlet-api)

// Source File Name: HttpServletRequest.java

package javax.servlet.http;

import java.security.Principal;
import java.util.Enumeration;
import javax.servlet.ServletRequest;

// Referenced classes of package javax.servlet.http:
// Cookie, HttpSession

public interface HttpServletRequest
extends ServletRequest
{

public abstract String getAuthType();

public abstract Cookie[] getCookies();

public abstract long getDateHeader(String s);

public abstract String getHeader(String s);

public abstract Enumeration getHeaders(String s);

public abstract Enumeration getHeaderNames();

public abstract int getIntHeader(String s);

public abstract String getMethod();

public abstract String getPathInfo();

public abstract String getPathTranslated();

public abstract String getContextPath();

public abstract String getQueryString();

public abstract String getRemoteUser();

public abstract boolean isUserInRole(String s);

public abstract Principal getUserPrincipal();

public abstract String getRequestedSessionId();

public abstract String getRequestURI();

public abstract StringBuffer getRequestURL();

public abstract String getServletPath();

public abstract HttpSession getSession(boolean flag);

public abstract HttpSession getSession();

public abstract boolean isRequestedSessionIdValid();

public abstract boolean isRequestedSessionIdFromCookie();

public abstract boolean isRequestedSessionIdFromURL();

/**
* @deprecated Method isRequestedSessionIdFromUrl is deprecated
*/

public abstract boolean isRequestedSessionIdFromUrl();

public static final String BASIC_AUTH = "BASIC";
public static final String FORM_AUTH = "FORM";
public static final String CLIENT_CERT_AUTH = "CLIENT_CERT";
public static final String DIGEST_AUTH = "DIGEST";
}
=====================================================================================
// Source File Name: HttpSessionEvent.java

package javax.servlet.http;

import java.util.EventObject;

// Referenced classes of package javax.servlet.http:
// HttpSession

public class HttpSessionEvent extends EventObject
{

public HttpSessionEvent(HttpSession source)
{
super(source);
}



public HttpSession getSession()
{
return (HttpSession)super.getSource();
}
}
 
Sumit Patil
Ranch Hand
Posts: 296
Eclipse IDE Firefox Browser Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As Swetha said,its the implementing class which provides the implementation which you can call in your application.
In case of HttpServletRequest, its the servlet container like Tomcat which will provide the implementation.
 
Tim Holloway
Bartender
Posts: 18662
71
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually, the HttpSession is an interface, as well!

Note: http://www.coderanch.com/how-to/java/UseCodeTags - be kind to our eyes!
 
Pete Nelson
Ranch Hand
Posts: 147
Debian Eclipse IDE Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Program to an interface, not an implementation

Says it all.
 
Swetha Bhagavathula
Ranch Hand
Posts: 112
Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tim Holloway wrote:Actually, the HttpSession is an interface, as well!


HttpSession in an interface but here the return type of the getSession() method is this interface that is HttpSession..
 
Swetha Bhagavathula
Ranch Hand
Posts: 112
Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Note: http://www.coderanch.com/how-to/java/UseCodeTags - be kind to our eyes!
Thank you . In order to highlight the method of that class(implementing the interface HttpServletRequest) I used the font size.. I change and obey from next post onwards..
 
Swetha Bhagavathula
Ranch Hand
Posts: 112
Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sumit Patil wrote:its the implementing class which provides the implementation which you can call in your application.
In case of HttpServletRequest, its the servlet container like Tomcat which will provide the implementation.


Hai. I hope Tomcat is web server not the container.. request you to please verify even IDEs where they give tomcat under servers and" catalina" is the servlet container in tomcat web server.
 
Tim Holloway
Bartender
Posts: 18662
71
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Swetha Bhagavathula wrote:
Sumit Patil wrote:its the implementing class which provides the implementation which you can call in your application.
In case of HttpServletRequest, its the servlet container like Tomcat which will provide the implementation.


Hai. I hope Tomcat is web server not the container.. request you to please verify even IDEs where they give tomcat under servers and" catalina" is the servlet container in tomcat web server.


Well, although I don't happen to either like or use the WTP plug-in that comes bundled with the JEE spin of Eclipse, I think that they're trying to make the distinction between the class of the server (Tomcat) and the specific implementation of that class (Catalina).

Since, as far as I can recall, all of the Tomcats (so far!) have been Catalina, it's an academic distinction. We usually use the generic term "Tomcat" to mean the server/container.
 
Prasoon Kumar Mishra
Greenhorn
Posts: 6
Java MySQL Database Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you have to use request object to call getSession() method
 
Swetha Bhagavathula
Ranch Hand
Posts: 112
Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
we can use "ses.getClass()" method in order to know the container supplied java class that is implementing HttpSession interface...............where ses here can be assumes as HttpSession object..
example:
HttpSession ses=request.getSession();
System.out.println(ses.getClass());//here we can get the underlying class implementing the HttpSession interface...

hope this is useful
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!