• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

HttpServletRequest

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 112
Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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();
}
}
 
Ranch Hand
Posts: 296
Eclipse IDE Firefox Browser Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Saloon Keeper
Posts: 27764
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Actually, the HttpSession is an interface, as well!

Note: https://coderanch.com/how-to/java/UseCodeTags - be kind to our eyes!
 
Ranch Hand
Posts: 147
Eclipse IDE Tomcat Server Debian
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Program to an interface, not an implementation

Says it all.
 
Swetha Bhagavathula
Ranch Hand
Posts: 112
Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Note: https://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
Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Saloon Keeper
Posts: 27764
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 6
Oracle MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
you have to use request object to call getSession() method
 
Swetha Bhagavathula
Ranch Hand
Posts: 112
Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
If you try to please everybody, your progress is limited by the noisiest fool. And this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic