Monday, November 21, 2011

JSP

For our HelloWorld servlet, we could get away with a single call to the response writer:
 response.getWriter().println("Hello World!");  


However, this doesn't work so well if you wanted to output an entire HTML.
For example, to generate 'Hello, World!' message as an HTML page, you will have to do:
           response.getWriter().println("<HTML>");  
response.getWriter().println("<HEAD>");
response.getWriter().println("</HEAD>");
response.getWriter().println("<BODY>");
response.getWriter().println("Hello World!");
response.getWriter().println("</BODY>");
response.getWriter().println("</HTML>");



Clearly, as your HTML gets more complex, the Servlet will become unreadable.

JSP (Java Server Pages) was introduced to solve this problem.

JSP is a templating technology - JSP pages (*.jsp) look exactly like HTML pages, plus they can make calls to Java classes.

1 comment:

Anonymous said...

A point thats confusing for folks... a JSP is ultimately a servlet.

When Tomcat explodes the war, it compiles the JSP files to Java servlets.

To have a look at the compiled JAVA code, navigate to the Tomcat installation directory and then go to: work/Catalina/localhost/HelloWorld/org/apache/jsp/

Here you should see the java code and the compiled class files.