jsp vs servlet

Discussions

Web tier: servlets, JSP, Web frameworks: jsp vs servlet

  1. jsp vs servlet (2 messages)

    hi there ,
        could anyone please tell me the difference between servlets and jsp

    Threaded Messages (2)

  2. jsp vs servlet[ Go to top ]


    As you'd probably know, in the simplest terms a servlet is a java alternative to CGI. It is used for dynamic content presenation of content over the web. In this process a servlet can perhaps pick data from a database, refer other classes/beans for performing computations, some legacy app, or even some other servlets and send the results back to the user's browser in html form. The problem with servlets is that they don't enable separation of code from content.

    Within servlet code you have to embed chunks of HTML, which makes it very messy. So within code you have awkward fragments like

    out.println("<html>");
    out.println("<body>");
    out.println("<p>This is real weird</p>");
    out.println("</body>");
    out.println("</html>");


    A JSP on the other hand is a high level interface to a servlet. A JSP page is an ordinary html page (of course named as a .jsp) containing pieces of java (default language) code. So the content developer can create the html page first, and the programmer puts pieces of code directly within html. Your server automatically 'compiles' your jsp into servlet code. It also enables dynamic loading, wherein if u change your jsp, the resulting servlet also changes,is recompiled and loaded, without your having to restart your server.

     Usually in real world scenarios, both don't exist in isolation, you have jsp pages and serlvets working in tandem. The servlet acts as the gateway to an application, it performs routing of requests and some essential tasks.
    A request from an html page goes to say a master servlet which redirects it an appropriate jsp. That way all monitoring, security and other issues can be taken care of at the central location, i.e. the master servlet. And all presentation is performed by the jsp.


     To learn up JSPs you can look up the JSP specs published by Sun. As a simple tutorial you might also want to check out is:

    http://developer.java.sun.com/developer/onlineTraining/JSPIntro/


  3. jsp vs servlet[ Go to top ]

    but you don't need to use out.println(). I use a very simple templating system where the files are read into a hashtable. The htmlers can work on the include files putting in tags with variables where needed. the servlet parses the html file and replaces the variables with their appropriate values. There is no overhead worth worrying about, and you don't need jsp if you prefer servlets

    dk