Posted on 2005-05-17
Last Modified: 2010-04-01
what is the difference between jsp and servlet, or is there a comparision at all?

Question by:Moizsaif123
    LVL 35

    Accepted Solution

    A jsp is compiled into a servlet that (by default) returns HTML

    You can change this of course...
    LVL 35

    Expert Comment

    So basically, a JSP is the same as a servlet with a doGet method which has text/html as it's responseType

    With a servlet, you can do doPost too :-) (as well as others)
    LVL 35

    Expert Comment


    Added to that list is the fact that there are now hundreds of tag-libraries you can plug in to JSPs (or write your own)
    LVL 4

    Author Comment

    when it says...

    Building Web pages on the fly is useful ....for e-commerce sites do this as well.

    building webpages on the fly means..they refresh on a timer basis and update records?

    what do e-commerce sites usually do?


    LVL 35

    Expert Comment

    No, the page has current (up to date) data on it is what they mean...  they are talking about dynamic pages as a whole (both jsp and servlet allow you to do this, as does ASP, CGI, etc)

    That link is a bit rubbish...sorry :-(
    LVL 2

    Expert Comment

    a JSP is a Servlet that thinks it's a Web page.

     Both use server-side Java to dynamically generate web pages. The source code to a JSP looks like HTML, with Java embedded inside funny tags (*); the source code to a servlet looks like Java, with HTML embedded in out.print(...) statements. Both use the Servlet API to communicate with the web server and the client. In fact, a JSP gets compiled into a servlet, so they're almost identical in terms of expressive power. The choice is, whether you're more comfortable coding your pages in Java or in JSP-style HTML; and since you can call a JSP from a Servlet and vice versa, you don't have to make an either-or decision.
    LVL 16

    Assisted Solution

    As other experts said ...

    JSP is high-level language of Servlet.

    If you're writing web by JSP, you'll get *.jsp files, but you'll get *.java files if you're writing by Servlet.
    However, the final of both .jsp and .java is .class file by transform process

       file.jsp ----> ----> file.class

    This process is done by web container, e.g. Tomcat, JBoss, Websphere etc.

    The .class file will provide code (called "Business Logic") for generating a web page (HTML, XML, JavaScript, VbScript etc) in various format depends on some parameters. Whenever a client make a request to a .class (by refer to a servlet or a jsp file), the web container will executes the code in the file .class to generate a web page for the request. This process is called "building webpage on the fly". In another word, you're not writing web pages directly, but you're writing a tool to generate web pages using java language.

    This useful for e-commerce sites because most of e-commerce sites are saling products and services on sites.
    You can remember which products are selected by user and show only those selected instead of all products to user.
    Not only that, you can compute total price and show pictures of those products by get images from your database.
    Of course, I would use all of my life to explain all benefits that JSP and Servlet can be applied to :)

    Finally, I would like to say that

    By JSP, you're writing java language inside HTML file.
    By Servlet, you're writing  HTML language inside JAVA file.
    LVL 35

    Expert Comment

    >> By JSP, you're writing java language inside HTML file.
    >> By Servlet, you're writing  HTML language inside JAVA file.

    Unless of course, you're handling binary data, like generating an image on the fly...

    Featured Post

    6 Surprising Benefits of Threat Intelligence

    All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

    Join & Write a Comment

    Suggested Solutions

    Title # Comments Views Activity
    JTable - RowFilter & Columns Header. 3 129
    Images won't display in JSP pages 30 165
    gZip compression filter 2 75
    spring example errors 33 153
    OfficeMate Freezes on login or does not load after login credentials are input.
    David Varnum recently wrote up his impressions of PRTG, based on a presentation by my colleague Christian at Tech Field Day at VMworld in Barcelona. Thanks David, for your detailed and honest evaluation!
    This video is in connection to the article "The case of a missing mobile phone (". It will help one to understand clearly the steps to track a lost android phone.
    In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor ( If you're interested in additional methods for monitoring bandwidt…

    755 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    21 Experts available now in Live!

    Get 1:1 Help Now