Tutorial :Servlets, JSP, Java?


I'm quite confused about some terms, I know Java for writting some "usual executable" programs. But I found that you can do servlets with java, and then heard of JSP.

I'm not sure about the diference of servlets and JSP.. I dont get the idea, I think it's like PHP or something like that.

Do you have any organized reference (or mind to explain the transition of "usual" Java to this technologies) about this?



Servlets are HTTP listeners that run in a servlet/JSP application engine like Tomcat, Jetty, etc.

JSPs are templates that are compiled into servlets. They allow you to write HTML-generating servlets in a tag-like template notation in such a way that dynamic behavior is possible.


Servlets are Java, umm, programs that run in a special environment for Web applications. They receive and handle Web requests, as you'd expect. Pretty much straight Java code plus a few conventions. Servlets have methods for digging out request parameters, and can connect a printer output stream to the output stream that will eventually be returned to the user's browser.

JSP are Java Server Pages. That's HTML pages and a kind of preprocessor. Among the usual HTML tags, the preprocessor evaluates a kind of template/tag language. Essentially, you can write an HTML page but include Java code. The "preprocessor" translates your JSP into a servlet; the HTML tags and their contents become strings in the servlet's code. Since you mentioned PHP, yes, JSP looks roughly like (some) PHP code that has HTML mashed in.

JSP is a very simple way to create Web pages with active Java content, roughly comparable to Microsoft's ASP. However, most of the Java community agree that the way JSP mixes HTML markup and code is an abomination that results in very unclean applications and also doesn't allow for specialization between Web designers and programmers. It's the same inelegance that "real" programmers like to bash PHP for.

Thus, JSP have mostly fallen from grace, and have been supplanted by other solutions for mixing Web content and code. Most notable/successful in industry were the meanwhile "industry standard" Struts and Java Server Faces. However, lots of people feel that those, too, are horribly complicated, and so there's a plethora of different Web application frameworks for Java.

Any of the keywords I've mentioned will lead you to other links on information. Exhaustingly complete information can be found in Oracle's documentation on J2EE but I can't recommend reading that as an introduction. It's frighteningly heavy, detailed and far-reaching. Do look instead at some of the modern 3rd party, preferrably open source frameworks, which I consider far less painful. I personally enoy Wicket but it's one of many and I know far too little of the alternatives to issue a valid recommendation.


Servlets and JSPs, just like PHP, are used to create dynamic HTML pages, but using the Java language. In the Java world, first we had Servlets. A Servlet is a Java class that implements certain interfaces. Then, for example, let's pretend that you want your servlet to output an HTML page like this:

<html>  <head></head>  <body>current time</body>  </html>  

Where current time will be dynamically generated and show the current time. In order to do this, you have to write multiple out.println statements:

out.println("<html>");  out.println("<head></head>");  out.println("<body>" + new Date() + "</body>");  out.println("</html>");  

As you can see, compared to PHP, this is very verbose and not maintainable. That's why JSP was born. You can achieve the same thing in JSP using the following code:

<html>  <head></head>  <body><%= new Date() %></body>  </html>  

This is much less verbose, much more readable, and much more familiar to HTML authors.

Now in order to server JSPs and Servlets, in the same way that you run PHP inside Apache + Mod PHP, you need a special kind of server called a Servlet container. A popular and open source Servlet container is Tomcat: tomcat.apache.org

Let me know if you have any other questions.


In simple terms, JSP is a way to build a servlet by embedding Java in a template.


Back in the old days there were applets, which were applications that ran inside a client's browser, and the inverse of that was servlets, which are applications that run within a java-based web server (container).

The servlet has access to information to pass back information to the browser, and to get information from resources such as the cookie and headers.

But, many people are familiar with ASP and PHP and writing servlets can be harder, so to enable frameworks to abstract out and make web development easier, we have JSP (Java Server Pages). These will compile to servlets, it is just an easier way to create the web pages.

This is a simplified explanation, so there are some parts I skipped, such as talking about how templating would be used, but you can get a better idea by looking at the JSP lifecyle here: http://java.sun.com/j2ee/tutorial/1_3-fcs/doc/JSPIntro4.html.

There are limitations to JSP pages that force you to use servlets at times, such as JSP pages can't return binary data, so if you want to send back an image or PDF then a servlet is your only option.


The "servlet API" is a set of Java interfaces ("contracts") which allows you to write small snippets of code dealing with a single web request from a browser and generating a response to that. That is usually a web page, but might also be a generated image or sound file.

Basically, you as a servlet programmer do not have to care for much more than just "what should I generate as a response to this request". The surrounding environment handles all the rest, like listening on ports for requests, logging request information, saving active sessions so users do not notice that the server was rebooted, etc.

In such a servlet, the response is generated in code, and

out.println("<li>" + name + ": " + address +"</li>");  

quickly gets tedious. Therefore JSP was invented allowing you to say

<li><%= name %>: <%= address%></li>  

instead in pages mostly HTML but with a bit of code in it.

Experience has since shown that the HTML-skeleton and the Java-data-presentation should be separated, as the mixing of HTML and Java in the same file makes it hard to maintain.

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Next Post »