Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Servlets or JSP?

Options
  • 26-10-2001 9:24pm
    #1
    Registered Users Posts: 2,518 ✭✭✭


    I'm going to be doing a project over the next while that will involve using Tomcat. The project will be a web-based multi-user system that logs customer calls to a tech support centre. It will use PostgreSQL as the database backend.

    I was wondering what the relative advantages/disadvantages are to using JSP or Java Servlets for this project; I'm not 100% clear on the differences between the two technologies :)


Comments

  • Registered Users Posts: 15,443 ✭✭✭✭bonkey


    Heh - is anyone really clear on the difference between these two?

    Servlets are "mini java programs". They are written completely in Java, and the HTML is generated by output statements, which can be a bit messy.

    They are useful for "controllers" and certain areas (like binary information, I think), but for your general run-of-the-mill webby stuff, its too much bloody effort IMHO.

    JSP is often classed as java's answer to ASP, which it is, but it is actually far "neater" than ASP.

    OK, with JSP you have your pages, written as HTML with "inline" java (where needed) which will be processed on the server. Better still, you can use things called custom tags, which you can write yourself in straight java and/or use any of the millions of free ones about. This means that you can more or less remove any in-line java from teh page, making it far more structured.

    JSP is actually compiled into a servlet. IIRC, on all servers, this is done at deployment time. Which means that the performance diff is mostly non-existent, and JSP is a lot cleaner to work with.

    Final recommendation :

    1) use JSPs for most of your work. Resort to using some servlets alongside it if you're doing some really complex stuff.
    2) Have a look at jakarta struts as an option to "enhance" your JSP model. Gives you a really neat framework....

    jc


  • Closed Accounts Posts: 19,777 ✭✭✭✭The Corinthian


    I've always had a preference for servlets over JSP's. I find them more stable and more powerful. The entire birth of JSP's was simply a reaction to ASP and I've never been overly happy with their implementation.

    I don't consider servlets as 'too much bloody effort' myself, but like bonkey, this is just a personal opinion. Try both, see which one you prefer working with. Servlets are as easy to register on Tomcat as JSP's in my experience.


  • Closed Accounts Posts: 1,651 ✭✭✭Enygma


    Servlets are good for initialisation, processing (image upload etc) and now Filters. You shouldn't really use Servlets for display at all. All those out.println()'s are going to wreck your head.

    A JSP page is simply a HTML page with Java 'scriptlets' in-between the code, it is translated into a Servlet as soon as it is visited the first time(*) and then compiled automatically. There's no point in saying that a Servlet is more powerful than a JSP because a JSP page becomes a servlet anyway.


    Strictly speaking you should only use your JSP page for displaying information, not processing but sometimes it's easier just to write a small JSP controller page.

    I don't think that JSP was just a reaction to ASP alone, it brings the MVC to web development in Java and the newer implementations are cleaner and more powerful than ever. Perhaps you should take a look at them Corinthian.

    Hecate, look into JSP Custom Tags, they're the ultimate in reuseability on the web.

    ========
    (*) I created an Ant buildfile that takes my JSP pages and translates them into Servlets, then compiles the generated servlets and creates a web.xml file with all the servlet mappings for the jsp pages. It then jars all the servlets and other Java classes, after which it creates a WAR file.
    The end result, after the WAR file is extracted is a website with absolutely no JSP pages. The whole thing is binary :) Cool!

    The advantages of this are, you find sytnax errors earlier and you don't have to actually wait for the application server to translate & compile the pages the first time it visits them.


  • Registered Users Posts: 15,443 ✭✭✭✭bonkey


    Originally posted by Enygma
    You shouldn't really use Servlets for display at all. All those out.println()'s are going to wreck your head.

    Not if you use ECS from jakarta, it wont :)

    A JSP page is simply a HTML page with Java 'scriptlets' in-between the code, it is translated into a Servlet as soon as it is visited the first time(*) and then compiled automatically. There's no point in saying that a Servlet is more powerful than a JSP because a JSP page becomes a servlet anyway.
    There are plenty of techniques which will let you avoid completely having "inline" code, which is almost unmaintainable, and makes files horribly ugly (IMHO)

    I have yet to see a decent editor which could let you easily seperate your code and HTML, so the best bet is to use the various techniques (taglibs etc) to avoid it.

    I don't think that JSP was just a reaction to ASP alone, it brings the MVC to web development in Java and the newer implementations are cleaner and more powerful than ever.
    Is JSP on its own considered an MVC design? I wouldnt think so...but it does allow you to use an MVC design if you wish

    My implementation of choice for non-trivial sites is JSP front-end, with jakarta struts (MVC and loads of cool taglibs), with either java or EJBs at the back end.

    I created an Ant buildfile that takes my JSP pages and translates them into Servlets, then compiles the generated servlets and creates a web.xml file with all the servlet mappings for the jsp pages. It then jars all the servlets and other Java classes, after which it creates a WAR file.
    The end result, after the WAR file is extracted is a website with absolutely no JSP pages. The whole thing is binary :) Cool!

    Nice one. I didnt have to do that cause the app server I'm on at the moment (Silverstream) compiles the JSPs at deploytime itself.

    jc


Advertisement