• 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. Java Server Pages

 
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
     
    A feeble attempt to tell you about our stuff that makes us money
    a bit of art, as a gift, the permaculture playing cards
    https://gardener-gift.com
    reply
      Bookmark Topic Watch Topic
    • New Topic