Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

When do I use servlets and when do I use JSPs  RSS feed

 
B Mampilli
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,
This was something I was asked in the interview.
1. Can everything that is done in a JSP be done in a servlet and vice versa?
2. When do I use servlets and when do I use JSPs?
Ans.1 I felt that everything that can be done in Java can be done in Servlets and vice versa. But, the interviewer wasn't thinking the same.
Regards,
Boney
 
B Mampilli
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry i made a mistake in typing...
what i meant was
" I felt that everything that can be done in JSPs can be done in Servlets and vice versa. But, the interviewer wasn't thinking the same."
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13078
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Since a JSP turns into a Servlet, you can use a servlet for anything a JSP does (ignoring questions of taglibs, etc.
However, it is very hard to make a JSP serve binary data such as zip files, pdf files, images, etc. so you can't say that a JSP can do everything a servlet can.
Bill
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The trend is for servlets to receive get & post requests and control the heavy lifting of handling whatever needs to be done, such as looking up data to display or doing updates. Control is a key word because they don't DO the work. They delegate to vanilla Java classes. If you're into Model-View-Controler stuff, the servlet asks the Model to do the real work. It is not unheard of to build an entire site with exactly one servlet that delegates everything to regular Java.
At the end of the work, stuff the results into a JavaBean and ask a JSP to render a page showing the results. The JSP can "usebean" to get the results.
A popular approach is to have NO JAVA in the JSP page, only tags from taglibs. The first argument I heard for this was to let people with HTML skills and no Java skills build the UI. That didn't do much for me, because we don't have HTML specialists. The better argument is that it helps you keep logic out of the view layer. If you put any Java in the JSP you may be on the slippery slope to inappropriate business logic and a NO JAVA rule helps avoid that.
The Struts framework is one possible (and very nice) implementation of all this. Reading a good Struts overview would give you more ideas on the roles of servlets and JSPs.
 
arul murug
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stan is right.In our project we don't have HTML specialists still we use taglibs.
One other advantage I noticed using Taglibs is the reusability of tags which we can't achieve by writing scriplets in JSP pages.
 
Ken Januski
Ranch Hand
Posts: 130
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've been studying for SCWCD and as I do so I keep reading about using JSP for the benefit of the HTML designers. But I've had my doubts about this because where I work I do everything. Now I see two more people also mentioning that they have no HTML people.
So at the risk of sending this off to a different topic I wonder what the general feeling is about the standard rationalizations for JSP. Do most people really use it so that it makes it easier for HTML designers to use? Or is it more for logical or other reasons?
Just curious.
 
arul murug
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do most people really use it so that it makes it easier for HTML designers to use?
I don't know the answer for that but I like using custom tags. My JSP code looks more neat and clean with tags. I can reuse tags without writing or cut pasting scriplets. Also as stan mentioned it avoids novice developers putting business logic in JSP pages.
 
B Mampilli
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks everyone for ure comments...
" Reading a good Struts overview would give you more ideas on the roles of servlets and JSPs. "
Thanks Stan for your comment..
Will do this and get to know more about struts ...
Regards to all.
Boney
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!