jQuery in Action, 3rd edition
The moose likes JSP and the fly likes JSP VS. SERVLETS Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "JSP VS. SERVLETS" Watch "JSP VS. SERVLETS" New topic


Fred Abbot
Ranch Hand

Joined: Jun 01, 2000
Posts: 300
CAn any one tell me when to use one over the other
I have been using servlets but i have never used JSP although i do know a little about it.
Iam faced with designing a new project and i am not sure which to use
thnak you for all your help
Roy Ben Ami
Ranch Hand

Joined: Jan 13, 2002
Posts: 732
jsp and servlets are very similar, ull have no problem writing jsp if u know servlets.in fact a jsp page is compiled into servlet (so jsp is actually just another way of writing a servlet).
the reason we can use jsp (well the main one anyway) is because say in a servlat u want to output an html page u have to use a lot of PrintWriter out statements, right? so its a bother..
in jsp u can combine in a page both the html and the servlet code, so u dont need to use a lot of out.println("<HTML> <BODY> etc tags..") but u just write in your jsp page those tags like regular html.
mostly ull do the presentation stuff (ie the visual) in jsp and leave the rest of the srious hard coding to another regular servlet.
hope i made it a bit clearer.
Fred Abbot
Ranch Hand

Joined: Jun 01, 2000
Posts: 300
Thank you
so if i understand you correctly i will have to write servlets even if i use JSP but instead of a servlet calling aservlet i will have a jsp calling the servlet.
and in the JSP page i can use HTML tags instead of a printwriter
Bear Bibeault
Author and ninkuma

Joined: Jan 10, 2002
Posts: 63852

Fred, unless you want to, there is no need to use servlets at all in a JSP environment. The JSP container takes your JSP page and turns it into a servlet that spits out the result of processing the page.
JSP pages are a template for creating the page that will be sent to the browser. To me, they are the preferred way to implement the "view" part of a web application since it is a lot easier to maintain the HTML of the page when it is expressed in a JSP rather than being built up by a series of Java statements.
Even when using handy tools like Jakarta ECS, generating all but the simplest of HTML in Java code is a pain to maintain.

[Asking smart questions] [About Bear] [Books by Bear]
Roy Ben Ami
Ranch Hand

Joined: Jan 13, 2002
Posts: 732
Fred u r correct.
usualy u will have a JSP page call a servlet (instead of servlet calling a servlet).
but like bear said above, u dont have to.
u can put all your java code in the jsp page.
for convience sake, its is more likely to seperate real java code from the jsp page but hey thats just what i use and think
also, there is a also an added bonus of using jsp pages. u can add java beans to jsp pages with an ease. that way u can put your code (like connecting to a database) inside a bean and add that bean to the jsp page (instead of letting a servlet do that).
Fred Abbot
Ranch Hand

Joined: Jun 01, 2000
Posts: 300
Thank you all for all your help
rakhi desai

Joined: Jan 06, 2004
Posts: 1
Whats is the difference between in terms of performance or security.
Jochen Maes

Joined: Dec 03, 2002
Posts: 20

like said in the above jsp pages are generated servlets, so once generated there is no difference...
You wan't performane incof? well the first time a jsp is loaded it will be (re)compiled (if changed), that does take a sec or so, depending on the complexity. also the jsp lest you change a page without the server being reloaded immediatelly. Servlet changes need a serverreload thus as in development sometimes a burden.
But servlets can be loaded at server startup time, jsp can depending on whitch servers (precompiling) but isn't always available, or descent.
I wouldn't suggest to state that once running jsp/servlets have big differences.
I agree. Here's the link: http://aspose.com/file-tools
It's not a secret anymore!