• 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

difference between JSP and Servlet

 
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi friends
please guide me what is the difference between JSP and Servlets and explain shortly how do these work ?
thanks
 
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 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.
  •  
    Ranch Hand
    Posts: 930
    2
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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
    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

    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
    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
    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.
     
    Ranch Hand
    Posts: 38
    Eclipse IDE Spring Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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
    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

    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.
     
    With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
    reply
      Bookmark Topic Watch Topic
    • New Topic