Servlet v JSP (Java Server Page). What is the difference?
A
servlet is a
web program that generates output (normally HTML) and returns it to a browser.
A
JSP (Java Server Page) is a
page of HTML ... with some extra bits of code where programatic elements are added as it's returned to the browser
So on the surface, it looks like there's a fundamental difference. Internally, however, Tomcat converts your .jsp pages into servlets, by taking all the elements which are
outside the extra bits and putting them into out.print statements in Java source, which is then compiled. So - in reallity - a .jsp is a servlet in disguise.
You can see this in action if you wish. Tomcat's
work directory is used for the conversion to Java source, and the compiling into class files, of your servlets, and (by default) the source code is left there for you to look at if you wish. Here's an example of where to look - default configuration, application called "wetfeet", host "locathost", file called "together.jsp" ...
/usr/local/tomcat/work/Catalina/localhost/wetfeet/org/apache/jsp/together_jsp.java
Taking some examples, a line like
Here is the <%= number %> times table
converts to
out.write("Here is the ");
out.print( number );
out.write(" times table \n");
and
<% for (int k=1; k<=maxtimes; k++) {
int result = k * number; %>
<tr><td><%= k %></td><td><%= result %></td></tr>
<% } %>
converts to
for (int k=1; k<=maxtimes; k++) {
int result = k * number;
out.write("\n");
out.write("<tr><td>");
out.print( k );
out.write("</td><td>");
out.print( result );
out.write("</td></tr>\n");
}
Complete example - .jsp source
[here] and the generated servlet source
[here].
Example from today's private Apache htpd and Tomcat course, which has been tailored for this customer from our
public httpd and Tomcat class.
(written 2013-02-06, updated 2013-02-07)
Associated topics are indexed as below, or enter http://melksh.am/nnnn for individual articles
J907 - JSP - JavaServer Pages [79] Last day of Java - (2004-10-08)
[1050] The HTML++ Metalanguage - (2007-01-22)
[1550] Java (JSP and Servlet examples) live on our server - (2008-02-23)
[2147] A very easy JSP (Java Server Page) - (2009-05-01)
[2151] What is a JSP tag library? - (2009-05-01)
[2183] Servlet life cycle, and Java Servlet variable scope - (2009-05-16)
[2525] Using JSPs, Tag Libraries, Java Beans, Tomcat in one short example - (2009-12-04)
[3046] Java Beans, tag libraries and JSPs - what and why. - (2010-11-13)
J901 - Java - Servlets [1909] javax.servlet cannot be resolved - how to solve - (2008-11-26)
[2652] Reading and writing cookies in Java Servlets and JSPs - (2010-02-26)
[3044] Changing a Servlet - more that just editing and compiling - (2010-11-12)
[4432] Java web application for teaching - now with sessions and clustering / load balancing demonstrations - (2015-02-20)
A651 - Web Application Deployment - Tomcat Overview [49] Business is the predominant user of Tomcat, Perl and Tcl - (2004-09-15)
[498] Why is Tomcat called Tomcat? - (2005-11-17)
[576] Why run two different web servers - (2006-01-25)
[659] Web Application Components - (2006-03-28)
[1265] Apache, Tomcat, Jakarta, httpd, web server - what are they? - (2007-07-13)
[1771] More HowTo diagrams - MySQL, Tomcat and Java - (2008-08-24)
[1941] Server - Service - Engine - Host, Tomcat - (2008-12-14)
[2016] Apache httpd and Apache Tomcat miscellany - (2009-01-30)
[2077] Why put Apache httpd in front of Apache Tomcat - (2009-03-12)
[2186] An FAQ on the Apache httpd and Apache Tomcat web servers, and on using them together - (2009-05-17)
[2753] You do not just fly - that is just part of the journey - (2010-05-06)
[4434] Public training courses - upcoming dates - (2015-02-21)
A507 - Web Application Deployment - Java - Servlets and JSPs [479] New servlet from old - (2005-10-28)
[4431] A Java servlet that is also a stand alone program. And a server that is also a web client. - (2015-02-19)
Some other Articles
Helping search engines with appropriate 400 error codes9 years, and 4000 articles onHandling failures / absences of your backend server nicelyTravelogue - from the Netherlands to London via Hook and HarwichServlet v JSP (Java Server Page). What is the difference?Tips on Tomcat - moving applications around Onward from BrusselsOn board Eurostar - a running commentary Two trains a day ... how many fares to London? Guess!Conference and Meeting Rooms available in Melksham