General J2EE: Difference between Servlet and JSP

  1. Difference between Servlet and JSP (1 messages)

    Dear All,
        Please tell me the difference that lies between Servlet and a JSP and when to use each of these.Kindly clarify my doubt on this.

  2. Difference between Servlet and JSP[ Go to top ]

    Servlets and JSP are functionally equivalent. In fact, a JSP is (eventually) compiled into a servlet.

    Having said that, the two technologies do best for different things. JSP are mostly HTML, possibly with a bit of Java mixed in. Servlets are mostly Java; to mix in HTML, you need ugly-looking out.println() statements.


    * JSP are good for operations that are HTML-intensive.
    * Servlets are good for operations that are Java-intensive.

    A common usage pattern for web development is Model-View-Controller (MVC), which separates application components into three groups:

    Model: Components holding application data. These are typically JavaBeans.

    Views: Components that display information to the user. On the web, this means dynamically-generated HTML, so JSP work well here.

    Controllers: Components that mediate between the model and the view, by (a) putting information entered by the user in the view into the model and (b) forwarding to the next view. Servlets do well for this, since these updates involve no HTML.

    A typical web-interaction following this pattern would be:

    1. User enters information into an HTML form and the form is submitted to a servlet.

    2. This servlet retrieves the form information, and stores the changes in model JavaBeans (which typically save the data someplace permanent like a database).

    3. The controller servlet automatically redirects to a view JSP.

    4. The view JSP retrieves data from the model JavaBean and creates and HTML page to display the results.

    Of course, this is only a sketch, and things can get much, much more complicated than this. My point is that both servlets and JSP have a legitimate role to play in Java web applications.