aspose file tools*
The moose likes Servlets and the fly likes Servlets vs JSP's Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Servlets vs JSP Watch "Servlets vs JSP New topic
Author

Servlets vs JSP's

Jawahar Rajan
Ranch Hand

Joined: Feb 28, 2002
Posts: 38
JSP's are really Servlets - is this true?
I know that when a JSP is run the JSP engine complies the translation unit into a servlet.
My question is How different is Tomcat or J2EE in this respect, or is there any difference.
When working with a Web application How can I dedcide how much java code needs to go into a JSP and how much in to Bean and or Servlet.
The Bean should be used for Business Logic if this is so then the Servlet can make the DB Transactiosn and excute the DB queries. IS this a good approach.
I am trying to keep my JSP's as light as possible
Are JSP always compiled each time because they do not have a class file unlike the Servlets. Hence would it be more efficient to rely on Servlets only.
Any Help is always appreciated
Jawahar
Matthew Phillips
Ranch Hand

Joined: Mar 09, 2001
Posts: 2676
JSPs are compiled only when they are changed. The resulting servlet is not stored in the WEB-INF/classes folder.
My unexpert opinion is to have as little code as possible in the JSP. That seems to be the going answer among the experts. The easier a page is to maintain, the better. It I need heavy code, I will either use a servlet or a custom tag.


Matthew Phillips
Naveen Sydney
Ranch Hand

Joined: Jan 21, 2002
Posts: 30
ok iam not sure where to start....every jsp is complied once until changed. depending on the servlet engine the compiled jsp(serlet) is stored in various locations.
As far as keeping the jsp clean of scriplets..it would be a good idea as it would be closer to a html and u can use html editors easily to change the layout easily.
I suggest that u read a lil about the MVC approach and also read about struts and the JSP Tablibs by manning
regards
syd
Doug Wang
Ranch Hand

Joined: Oct 05, 2001
Posts: 445
Originally posted by Matthew Phillips:
If I need heavy code, I will either use a servlet or a custom tag.

Matthew,
How do you compare these two solutions to heavy business logic?
Thanks in advance.


Creativity is allowing yourself to make mistakes. Art is knowing which ones to keep
Debashish Chakrabarty
Ranch Hand

Joined: May 14, 2002
Posts: 230

According to the Blueprints, use servlets strictly as a web server extension technology. This could include the implementation of specialized controller components offering services like authentication, database validation, and so forth. It is interesting to note that what is commonly known as the "JSP engine" itself is a specialized servlet running under the control of the servlet engine. Since JSP only deals with textual data, you will have to continue to use servlets when communicating with Java applets and applications.
Use JSP to develop typical web applications that rely upon dynamic content. JSP should also be used in place of proprietary web server extensions like server-side includes as it offers excellent features for handling repetitive content.


Debashish
SCJP2, SCWCD 1.4, PMP, ITIL Foundation
Doug Wang
Ranch Hand

Joined: Oct 05, 2001
Posts: 445
Debashish,
Thanks for your reply. But how does JSP custom tag fit into these scenario?
Sharat Madhav
Greenhorn

Joined: Mar 07, 2002
Posts: 3
Hi Jawahar,
1. JSP code is compiled by Tomcat and is stored in the work directory. The code will get compiled again only when there is a change to the JSP file.
2. You can identify code required in a bean, servlet or JSP.
a) All code that does not require user interaction can be written in a Bean.
b) All code that does interact with the user but no HTML code is required can be written in a Servlet.
c) All code that requires interaction by the user with HTML code can be written in a JSP.
Sharat.
sing
Ranch Hand

Joined: Nov 29, 2001
Posts: 121
Originally posted by sharatm:
1. JSP code is compiled by Tomcat and is stored in the work directory. The code will get compiled again only when there is a change to the JSP file.
2. You can identify code required in a bean, servlet or JSP.
a) All code that does not require user interaction can be written in a Bean.
b) All code that does interact with the user but no HTML code is required can be written in a Servlet.
c) All code that requires interaction by the user with HTML code can be written in a JSP.
Sharat.[/QB]


Sharat,
I agree with you.
Sometimes, althought JSPs are changed and compiled but it still catch the old JSPs in work directory. So, sometimes we need to clear JSPs in work directory if we found it's unchanged.
Steffy
Pradeep bhatt
Ranch Hand

Joined: Feb 27, 2002
Posts: 8919

One can use custom tags when there are common tasks such navigating through a collection and displaying their content in a custom tag because u can use the same logic across several pages. If u need to make changes then u will doing that at a central place. This is better than having a scriplet , which u need to copy to many places.Changing requires modification to be done at several places(rather than one in custom tag).
Using custom tags also makes it easier for web desinger who dont know java.
Sun is soon coming up with JSP standard libraries which will eliminate java code in jsp (some other expression langauge will be used) there by making it easier for web page developers. For more info visit http://www.theserverside.com/resources/articles/JSTL/article.html

Originally posted by Doug Wang:
Debashish,
Thanks for your reply. But how does JSP custom tag fit into these scenario?


Groovy
Edward Brode
Greenhorn

Joined: Apr 30, 2002
Posts: 11
It's more accurate to say that jsps themselves are parsed, rather than compiled. They are parsed into being source code for a servlet and then compiled into class files from the source code. What is cached in the work directory is the current and old versions of the servlet source code and the class files from their compilation. They are in a directory labelled "jsp" but that is due to the origin of the files in the directory being jsps. When you open the source code, you can see that the servlet is perfectly understandable once you get your head around how the parsing is done. It's more than a bit obscure, but the many comment lines reveal how the jsp is being read in by the parser, there being line numbers as indicators.
In regard to custom tags, they usually are only for routine functions like database connections and storage/retrieval of values of variables. Many software shops used them extensively as a way to separate the front and back ends, programmer-wise, having java programmers write the tags and their supporting code, and then having html developers use the tags without knowing any java at all!
Heavy business logic usually means transactions, and the ability to roll back everything involved in a transaction if any part of the transaction fails, something that is difficult to achieve with servlets and jsps, much less custom tags, this is where EJBs really reveal their worth.
Regards,
Edward
[ May 21, 2002: Message edited by: Edward Brode ]

Edward Brode<br />SCJP2<p>"It works on my machine"<br /> - Paul Symonds
Debashish Chakrabarty
Ranch Hand

Joined: May 14, 2002
Posts: 230

Debashish,
Thanks for your reply. But how does JSP custom tag fit into these scenario?

A JSP divides its content into two portions: the portion that can be created by a web designer, and the portion that must be created by a Java programmer. Simply put, HTML and Java are not dangerously co-mingled on the page.
Custom JSP tags take this idea a step further. A custom JSP tag library packages server-side Java capabilities into component libraries for easy deployment. They look and behave much like HTML tags consequently, web developers can dip into the tag library without having to wade into the unfamiliar waters of Java programming.
Matthew Phillips
Ranch Hand

Joined: Mar 09, 2001
Posts: 2676
Originally posted by Doug Wang:

Matthew,
How do you compare these two solutions to heavy business logic?
Thanks in advance.

Honestly, I wrote my first custom tag Saturday night. My feeling is that a custom tag is a great place for presentation logic and a servlet is a great place for controller logic. Business logic really belongs in a bean so that it may be reused outside of the application.
Paul Stevens
Ranch Hand

Joined: May 17, 2001
Posts: 2823
sharatm
The Java Ranch has thousands of visitors every week, many with surprisingly similar names. To avoid confusion we have a naming convention, described at JavaRanch's naming policy. . We require names to have at least two words, separated by a space, and strongly recommend that you use your full real name. Please change to a new name which meets the requirements.
Thanks.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Servlets vs JSP's