• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

What Servlet can do which a jsp cant or viceversa ?

 
Praveen Babu
Ranch Hand
Posts: 138
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi friends,

My question is obvious from the subject line. what a servlet can do which a jsp cant and viceversa.

One more question is that I have heard a jsp cannot have servlet listeners(Session, Attribute et). Is this true ? Please clarify

Bye & Regards,
Praveen
 
Em Aiy
Ranch Hand
Posts: 226
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
well as far as i know, you can have any thing in JSP than can be done in JAVA using the scriplet tags <%[javacode]%>.

The only difference that come in to my mind is that, jsp is much more for WebDeveolpers (i.e persons who are good at HTML) ... we desgin tags for them and they use those tags to make the page.

In jsp we can define out customtags, which are ultimately controls for html programmer to use them.
 
Praveen Babu
Ranch Hand
Posts: 138
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Thanks for the reply. I know that we can do everything in a jsp that can be done in a servlet. But i am still not convinced whether we can use Listeners in particular in JSP. If so how ?

Bye & Regards,

Praveen
 
Ulf Dittmer
Rancher
Pie
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I know that we can do everything in a jsp that can be done in a servlet.

I wouldn't put it that way. The other way around, yes - a servlet can do everything a JSP can. But JSP pages are geared towards text output. It's hard, if not impossible, to use them for generating binary content, with which servlets have no problem.
 
ramprasad madathil
Ranch Hand
Posts: 489
Eclipse IDE Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

But i am still not convinced whether we can use Listeners in particular in JSP. If so how ?


Listener are not for Servlets or Jsps.
Listeners are configured to listen to Session/Context events. Thus you can add listeners to notify you about sessions/context being created and(or)destroyed and also for event handling when attributes are added to session and(or) context object(s).

ram.
 
Arun Somasundaram
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
JSP'S are purely HTTP based but servlets are not.The servlet can be neither HTTP based nor non HTTP based.It depends upon the class you extend to your servlet class.

1.)If you extend HttpServlet class your servlet becomes a HTTP servlet for use on the web.

2.) If you extend GenericServlet class your servlet becomes
protocol-independent servlet.

If you inherit GenericServlet you have to only override the abstract service method. There is no need to define doGet(),doPost()etc.. because all these methods are HTTP based and not defined in the GenericServlet class.More over you can write FTP servlet,TCP/IP servlet etc..

The "Generic Servlet" has the abstract service methods that can be implemented to process multiple protocols. To process the FTP, TCP/IP, HTTP etc protocols we can implement Servlets that can extend Generic Servlets. The FTP, TCP/IP, HTTP Servlets can implement the service methods to accept the request from the customer in different protocols.


You cannot write protocol independent JSP like FTP JSP,TCP/IP JSP etc.JSP'S are always HTTP based.There is no change in it.
[ August 01, 2006: Message edited by: Arun Somasundaram ]
 
Ulf Dittmer
Rancher
Pie
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
JSP's are purely HTTP based but servlets are not. The servlet can be either HTTP based or non HTTP based


It's only theoretically possible to use servlets with other protocols, see some information about this here.

JSP'S are always HTTP based.

Current servlet container implementations may work this way, but if the server supported other protocols for servlets, why couldn't they do that for JSPs as well? Just like HttpJspPage extends JspPage, so could SmtpJspPage. (This is just an academic point, though, since no such server exists.)
[ August 01, 2006: Message edited by: Ulf Dittmer ]
 
Arun Somasundaram
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I replied in the academic point of view.But I don't know why there is a need to quote my reply?. Any way I am not wrong with my reply.At this time it is said that JSP'S are purely HTTP based and Servlets can be HTTP based or non HTTP based(Protocol-Independent).Its true.
[ August 01, 2006: Message edited by: Arun Somasundaram ]
 
Em Aiy
Ranch Hand
Posts: 226
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by praveen babu:
Hi,

Thanks for the reply. I know that we can do everything in a jsp that can be done in a servlet. But i am still not convinced whether we can use Listeners in particular in JSP. If so how ?

Bye & Regards,

Praveen

Listeneres are something that you implement using the Descriptor files of the particular server, there is no special implementation of LISTENER thing in servlet. I am not sure about this thing, since i don't have any idea regarding it.
 
Ulf Dittmer
Rancher
Pie
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
At this time it is said that JSP'S are purely HTTP based and Servlets can be HTTP based or non HTTP based (Protocol-Independent). Its true.


I don't know by whom it is said to be so, but it is not so. If there was a non-HTTP servlet engine then it could be used to implement non-HTTP JSPs. Since there isn't, it's not possible.

Which part of the JSP Specification (not of any particular implementation) do you reckon to be HTTP-specific?

By the way, I'm quoting your post so that someone following this thread knows what I'm responding to.
[ August 01, 2006: Message edited by: Ulf Dittmer ]
 
Arun Somasundaram
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok let it be.Theoratically every one knows JSP'S are used for only generating dynamic web contents but servlets can generate dynamic web contents which are HTTP specific as well as contents which are not HTTP specific(protocol-independent).I don't want to argue with you further and I wont interrupt in this discussion any more.
[ August 01, 2006: Message edited by: Arun Somasundaram ]
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Instead of asking what each can and/or can't do, wouldn't it be better to ask "How do I make the best use of these technologies?"?

JSP is a templating mechanism that is built on top of servlets.
It is best used for formatting an HTML response to an HTTP request.

For anything other than formatting HTML (or some other textual output) servlets should be your first choice.
The two, most common tasks that are best performed with servlets are controlling web flow and streaming binary data.
 
Ulf Dittmer
Rancher
Pie
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
but servlets can generate dynamic web contents which are HTTP specific as well as contents which are not HTTP specific(protocol-independent).

Can you give an example of what you mean by HTTP-specific content and non HTTP specifc content? I'm curious, because I have never heard of this distinction being made.

The content generated by JSPs/servlets has nothing to do with the transport protocol used - that's the reason why it's theoretically possible to base them on other protocols, even though in practice that hasn't happened.
 
Arun Somasundaram
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
YOU ARE ABSOLUTELY RIGHT Mr.BEN!!!
 
Praveen Babu
Ranch Hand
Posts: 138
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks All for the replies

And before the discussion between Arun & Ulf gets hotter i would like to say that i have found some interesting points from you people.

- There is no special implementation of LISTENER thing in servlet ( although not confirmed, can any one please confirm this because this was my actual question )

However some other interesting points i liked most are

- JSP'S are purely HTTP based

and one has yet to decide about the servlets.


So finally please confirm
A Listener is a servlet?
true or false


If so why ?

Thank you one and all for the replies.

Bye & Regards,
Praveen
 
Ulf Dittmer
Rancher
Pie
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Praveen,
yes, we got a little sidetracked there. Thanks for sitting it out patiently

Listeners are not servlets, they are regular Java classes that implement specific ...Listener interfaces.

As to JSPs being HTTP-based, well, read the discussion above. Both servlets and JSPs are HTTP-based in all existing servers, but neither needs to be in principal.
 
Arun Somasundaram
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mr. praveen.If you check the servlets api documentation you can see the Listeners are defined as interfaces.So listeners can only be implemented and not overided.The JAVA classes can implement the listener interfaces and can define the listener interface methods in the implemented class according to application needs.Up to my knowledge the listeners are implemented in ordinary JAVA classes and not directly implemented in the servlet classes.It is a general practice to call the listener methods from the servlet class when needed.

Here is the URL for servlets API documentation:

Servlet API
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Arun Somasundaram:
It is a general practice to call the listener methods from the servlet class when needed.


The container calls the listener's methods.
There is no need to call them from a servlet.
[ August 02, 2006: Message edited by: Ben Souther ]
 
Arun Somasundaram
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes the container calls the listener interface methods,Its true.What I am trying to say is we can add some other methods to the class which implements the listener interface and those methods can be called from the servlet classes when needed,the user defined methods in the implemented class has to be called explicitly by the user and the server never calls it.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64620
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Arun Somasundaram:
What I am trying to say is we can add some other methods to the class ...


While true, this really has nothing to do with the original discussion. Please try to stay on-topic and not introduce spurious side points.
 
Arun Somasundaram
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok officer!!! Wow you have opened my eyes
 
Praveen Babu
Ranch Hand
Posts: 138
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Friends,

Thanks for a good discussion i have ever been to.

Thanks all for your replies.

Now i know what are Listeners and they are not servlets.

Happy Coding,

Bye & Regards,
Praveen
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic