aspose file tools*
The moose likes Servlets and the fly likes difference between JSP and Servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "difference between JSP and Servlet" Watch "difference between JSP and Servlet" New topic
Author

difference between JSP and Servlet

emma smith
Ranch Hand

Joined: Nov 29, 2012
Posts: 48
hi friends
please guide me what is the difference between JSP and Servlets and explain shortly how do these work ?
thanks
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61424
    
  67

This is something that is easy to look up on the web. Please find appropriate tutorials.

In a nutshell:
  • A servlet is a Java class that executes in the context of a servlet container, that responds to HTTP requests.
  • A JSP is a template for a text reponse, most often an HTML page. For more information on JSP, see this article.


  • [Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
    sai rama krishna
    Ranch Hand

    Joined: May 29, 2009
    Posts: 269
    Check this link
    http://www.theserverside.com/discussions/thread.tss?thread_id=21024

    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.

    Therefore:

    * 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.
    Bear Bibeault
    Author and ninkuma
    Marshal

    Joined: Jan 10, 2002
    Posts: 61424
        
      67

    sai rama krishna wrote:servlets and JSP are functionally equivalent.

    No, they are not. To say that is to assert that they are interchangeable, and they are most certainly not.

    JSP does use servlets as an underlying technology, but they are not functionally equivalent. JSP offers many features and mechanisms that servlets do not.

    My point is that both servlets and JSP have a legitimate role to play in Java web applications.

    This is very true.
    Bear Bibeault
    Author and ninkuma
    Marshal

    Joined: Jan 10, 2002
    Posts: 61424
        
      67

    From the parallel topic:
    emma smith wrote: i know when we use HTML in java code then it is called Servlet and when we use java code in HTML then it is called JSP

    That's a very simplistic, and not particularly accurate, way of looking at these concepts.

    Servlets, as pointed out, are the Java objects that receive a request from a browser, and control how the response is generated. They could interact with model classes to read and write data to database, for example. They should not be used to emit HTML. That's a practice from about 15 years ago!

    JSP, on the other hand, is the technology for generating HTML. They should not contain Java code. They are templates that a servlet uses to generate the HTML response. Again, I urge you to read this article and ask questions about anything that you do not understand.

    This article is more advanced, but shows how JSPs and servlets work together to respond to a request.
    Vivek SharmaJi
    Ranch Hand

    Joined: Dec 09, 2012
    Posts: 38

    Servlets and Java Server Pages are complementary APIs, both providing a means for generating dynamic Web content. A servlet is a Java class implementing the javax.servlet.Servlet interface that runs within a Web or application server's servlet engine, servicing client requests forwarded to it through the server. A Java Server Page is a slightly more complicated beast. JSP pages contain a mixture of HTML, Java scripts (not to be confused with JavaScript), JSP elements, and JSP directives. The elements in a Java Server Page will generally be compiled by the JSP engine into a servlet, but the JSP specification only requires that the JSP page execution entity follow the Servlet Protocol.
    Bear Bibeault
    Author and ninkuma
    Marshal

    Joined: Jan 10, 2002
    Posts: 61424
        
      67

    Vivek SharmaJi wrote:...a mixture of HTML, Java scripts (not to be confused with JavaScript)

    The correct term is Java scriptlets (not Java scripts) and they have been obsolete for over 10 years. Modern JSPs -- any written after 2002 -- should not contain Java scriptlets. The JSTL and EL are the modern means to add dynamic data to a JSP.
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: difference between JSP and Servlet