Sunday 8 September 2013

What is JSP ?

Introduction



  • jsp stands for java server pages.

before learning JSP , you should have understanding of HTML, JAVA AND SERVLETS

  • Because a lot of things we are going to talk in this session make sense, if you do not know any of these technologies.


Intoduction
JSP Elements
Implicit Objects
Tag Libraries
JSP Actions
Error Handling
Demo


  • In the servlet tutorials we have talked about lot of topics and all those concepts to apply in JSP as well , but not all most of the concepts do apply in jsp
  • JSP is used to presents dynamic content to users, so here again we are trying to say the same thing. Even in servlet tutorial , I told you that servlet is used to present dynamic content to the users.
  • And I told you that If want to present static content to the user, we use HTML
  • And when you want to dynamic content, you go for servlets, here again in JSP telling the same thing which is used to dynamic content to the users

-------------------------------------------------------------------------------------------------------------
So what is the difference between the servlets and jsps

Difference between Servlets and Jsp?


  • Basically both are used to present the dynamic content to the users. But their job is different.
  • Servlets are used for business processing logic where as jsps are used for presentation logic processing logic.


MVC Architecture overview:


And it is context am I talking all these about, Iam talking about context of these MVC Architecture
MVC is a very famous architecture, which stands for Model View Controller where each componant has its own job.

What is Model?

Here the model component is responsible to communicate with the data base. That is fetch the data or get the information from the database and make it available to the application to use

What is View componet?
The view component is responsible for presenting the page to the useror presenting the response to the end user and very beautiful and neat format. So we visit a lot of websites everyday, you see that the UI looks so good and all those credit goes to the view componant, so makes it so beautiful. And Jsp is used for view componet here and it is very strongly used view component.

What is Controller?
Next part is Controller, basically the controller controls the whole application flow, whenever a request comes into the application, the request is crapped by Controller, and the Controller is going to decide based on the request to which helper object should it make use of, which view object is going to use of ? and sends the response back to the user.
Basically the controller intercepts all the requests and it controls the whole flow of the application

Lets take a real time example of MVC Architecture

How a flow takes place in web application with servlets and jsps in place
When the user sends the request, the Web Container or Servlet Container is the interface between your, end user and the application
So the request goes to the Container, again Container will send to the request to the Servlet, so basically servlet is the place where you write the business logic.
And infact the Servlet will make use of certain other helper objects to completed business logic process
Lets say that this Helper Obect is talking to the Business Tier who executes certain statements (business Logic).
Gets back to the Servlet, the data is populated into some bean or some intermediate object within the application and the Controll is passed to the jsp
So the jsp is here the representing the presentation layer, and we put all the presentation logic within the jsp. So JSP is nothing but a html with some amount of java code in it. JSP acts as a presentation layer, we write html with dynamic code where add some java that's the reason we used dynamic content.
Once JSP does all its processing, and its sends back its response to the Web Container which Web Container will sends back to the end user
This is how Control flow in a web Application with both servlets and jsps.
Basically servlet role is taking the request and pop of the business logic processing and send the request to the jsp. Jsp role is take care of presentation logic and send back response to the user.

Servlets jsp
1. handles dynamic data 1. handles dynamic data
2. Handles business Logic 2. Handles presentation Logic
3. life cycles methods are 3. life cycles methods are
    init()    : can be overridden jspInit() : can be                           overridden
    service():  can be overridden _jspService(): can not                       be overridden
    destroy():  can be overridden jspDestroy(): can be overridden
4. html within java 4. java within html
   out.println("<html> <body=">);   <html><body>
   out.println("Time is="+new Date());   Time is<%=new Date()%>
   out.println("</body></html>");   </body></html>
5. Runs within a Web container 5. Runs within a web container


  1. Here both the servlets and jsp handles dynamic content. It has ability to taking the request, produce dynamic data based on request. 
  2. We saw that servlet handles business logic where as jsp handles the presentation logic part in an application. 
  3. We know that servlet life cycle methods are init(), service(), destroy() methods and we can override all these methods. Where as Life cycle methods of servlets are: jspInit(), _jspService(), and jspDestroy() methods , basically we can override jspInit(), jspDestroy() methods but we can not override _jspService() method. 
  4. Basically whatever methods starts with (_) underscore , that will not have ability to override and whatever the methods which we do not have underscores that can be overridden

So if we want to present the data to the user within servlets what can you do? we put html within java.

out.println("<html> <body=">);  
   out.println("Time is="+new Date());
   out.println("</body></html>");  
here we just write print and Time is  the current tim on to the page
simlarly in the jsp also we do the same thing, but the jsp is opposite i.e java within html

<html><body>
 Time is<%=new Date()%>
</body></html>


  • basically we are writing html code and whatever java code you need to write , in certain special tags example: Time is<%=new Date()%>. Where as in servlet we are writing html code within the java code
  • If we compare the both the code which do you think looks more pretty and easy to understand which is in jsp part rather than servlet.
  • In jsp , simply we have html code and certain special parameters where you put in your java code so for that reasons jsp is very popular for that reason we need a separate presentation layer to present the information to the end user. And we can not user in servlets to perform the presentation and as well as business logic
  • And both servlets and jsps are run within the web container.
---------------------------------------------------------------------------------------------------------

JSP is nothing but a Servlet


We already talked that in the end every jsp is nothing but a servlet so since we have same life cycle methods as well. Because whatever you write in the jsp in the end translated to the servlet
So whatever code you write in jsp either goes to the init() method , service ()method or destroy() method
So the programmer writes the jsp and it is translated to java file i.e MyJsp.jsp-->MyJsp_jsp.java and the java is ultimately translated to class file i.e MyJsp_jsp.class.
When you execute the jsp, when you call the jsp then it is loaded and initialized as a Servlet
So ultimately in the end jsp is nothing but a Servlet so all the features we have in servlet basically you will ultimately have jsp as well. You should have all the ability to use all the features in of servlet in jsp because a jsp is nothing but a servlet

No comments:

Post a Comment