• 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

Servlets vs JSP's

 
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 2676
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 445
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Ranch Hand
Posts: 250
Python Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Doug Wang
Ranch Hand
Posts: 445
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Debashish,
Thanks for your reply. But how does JSP custom tag fit into these scenario?
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 121
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Ranch Hand
Posts: 8945
Firefox Browser Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Debashish Chakrabarty
Ranch Hand
Posts: 250
Python Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 2676
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Ranch Hand
Posts: 2823
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic