Thursday 5 September 2013

Overview of JSP

servlets: HTML within java business logic

public void doGet(request, response)
{
 PrintWriter out=response.getWriter();
 String name=request.getParameter(name);

 out.println("<html><body>");
 out.println("Hello"+name);
 out.println("</body></html>");
}

JSPs: java within HTML Presentation Logic
<html>
 <body>
  <% String name=request.getParameter(name);%>
  Hello<%=name%>
 </body>
</html>

Initially web server or HTTP server can handle and service only static http pages but where as if you want to go to any dynamic content you have to go with servlets
So how do you basically print out a dynamically content on a html? This is the answer to that, you write out.println() statement on your servlet (i.e out.println("<html><body>")
In client URL the following code will be visible:

out.println("<html><body>");
 out.println("Hello"+name);
 out.println("</body></html>");
So out.println() goes to the client screen, so whatever it says, actually it is a output. Which is sent it as a response to the client
This is nothing but here what I'm doing Just writing simple do..get method Im getting response.Writer() and simply out.println("<html><body>");
And println("hello"); i.e  out.println("Hello"+name);
That means this is the html i.e <html><body> file and it only one word that is "hello" and "name" of the person in the body of that file
So this is how servlets is able to putting the UI code in its class
But you have a look at this is a mixture of business logic with html code
Basically servlets use only to perform some business logic for creating dynamic content.
But here what you are doing is you are perfoming business logic in the dynamic content but at the same time you are even doing html code in servlet itself so this is very bad practice
You have actually your apps in such a way that your business logic entity is seperate and your presentation logic entity is separate .
You have to seperate these things . Business Logic and Presentation Logic are two separat things but it should not be makes in only one thing which creates more confusion and its not very clear
So your servlet only take care of only business logic part and the presentation logic part should be done somewhere else.
What is that somewhere else? Where is that some where else?
Here somewhere else is nothing but JSP's. This is where JSPs had come into the whole servlet picture

JSPs: java within HTML Presentation Logic
<html>
 <body>
  <% String name=request.getParameter(name);%>
  Hello<%=name%>
 </body>
</html>
JSPs are used to provide the presentation logic to the client so this is nothing but JSP s are similar to HTMLs
But within this some special kind of HTML, you can write java code within this HTML
So HTML+JAVA =JSPs
So JSPs take care of all of your presentation logic for beautiful UI look and your servlet actually should take care of your business logic. So separation of concerns is what we are doing here
JSPs are only concerns only presentation logic and servlets should only concens only Business logic




Now but in the end JSP is nothing but a servlet everything you write a jsp ultimately goes into a servlet so who creates this servlet, it is the Web Container whose responsible  to create a servlet out of this JSPs.
So as we discussed JSP is nothing but a HTML + Java, ultimately this whole thing is converted into a servlet so previosly we have seen that when you write the out.println()

public void doGet(request, response)
{
 PrintWriter out=response.getWriter();
 String name=request.getParameter(name);

 out.println("<html><body>");
 out.println("Hello"+name);
 out.println("</body></html>");
}
ultimately when you write a jsp program nice UI look and html tags and all those stuff like below:

JSPs: java within HTML Presentation Logic
<html>
 <body>
  <% String name=request.getParameter(name);%>
  Hello<%=name%>
 </body>
</html>
And the Web Container converts the jsp program into a servlet (like below)

servlets: HTML within java business logic

public void doGet(request, response)
{
 PrintWriter out=response.getWriter();
 String name=request.getParameter(name);

 out.println("<html><body>");
 out.println("Hello"+name);
 out.println("</body></html>");
}

And your servlet looks the same as out.println() statements. But this is not your responsiblity. Conversion of jsp to servlet is the responsible of web container.
So you basically write as a programmer in jsp page where you will put your html tags all the java content in it and that servlet is translated into a java file (Ex: MyJsp_jsp.java) and then java file is compiled and it is loaded and initialized as a Servlet
So ultimately in the end a jsp is nothing but a servlet.
So when the first request for jsp comes in what container does is it translates a jsp into a java, compiles it and loades that compiled java class as a servlet from there on it is a servlet so basically a jsp is also a servlet but when you code in as a jsp then it ultimately becomes a servlet.

No comments:

Post a Comment