• Post Reply Bookmark Topic Watch Topic
  • New Topic

Servlets vs. Java Server Pages  RSS feed

 
Elton Hughes
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello All,
Ok, I think this is a beginner question so here goes. I finished Head First Java recently, but I want a little clarification concerning servlets and Java Server Pages (JSP).
My understanding is that servlets are Java code with HTML inside, while JSP is HTML pages with Java code inside. Is that correct? Is it a little too simplistic?
Thanks,
Elton
p.s. If I have follow up questions, should I post them here or in the other forums?
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Elton,
That's a good "toy" answer; the truth, as always, is a little more complex.
A servlet is a apecial piece of Java code that runs inside a web server. It (usually) extends an HttpServlet base class. In the "bad old days" it was quite common for servlets to contain many statements like
out.println("<HEAD>");
etc -- hence the "HTML inside Java" reference.
A JSP is indeed HTML containing special tags. A JSP container takes it and translates it all into Java code, which is compiled to create a servlet.
Most web applications use both. Servlets are used to do the real computational work (or even just to delegate this work to other classes) and the user-interface part is implemented in JSPs. You could do everything in servlets alone, or JSPs alone, but it's better to use the right tool for the right job and split things up this way. It's much easier to write complicated code as plain old Java code, and it's much much easier to write HTML as HTML, rather than as a bunch of Java print statements.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66207
151
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This question gets asked a lot, and Ernest's answer is one of the best I've seen.
Could we get this topic moved to the JSP forum where many people might look for it?
thanks,
bear
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sure, the JSP forum is just as good as the Servlet forum.
Moving this to the JSP forum...
 
Elton Hughes
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ernest,
Thank you for your reply. Your example of servlets is exactly what I have seen. Lots of "out.println("Hellol World");". Now you called this style of programming the bad old days. Well, I don't want to learn bad habits, so, what books on JSP and/or servlets would you recommend to a novice Java programmer.
Thanks,
Elton
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are many good ones, some of them authored by your friendly JavaRanch bartenders! My two personal favorites:
  • As a technical reference, I liked Wrox's Professional Java Servlets 2.3, although because of Wrox's bankruptcy, I'm not sure about this book's availability. Simon Brown of JavaRanch is one of the authors. I liked this book because it covers a little of everything: servlets and JSPs, of course, but also application architectures, the details of app deployment, etc.
  • For "best practices" I think Bruce Tate's Bitter Java is a very helpful book, and fun to read, too!

  • As I said, there are many others, and I'm no authority on the servlet-book market, so hopefully you'll get some other responses.
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!