Thursday, 31 August 2017

Java Server Pages

Page No. : JSP lifecycle 
                  Insertion of a record in a table

JSP is a java based Web Application development technology, which is an advancement of the Java Servlets. Before JSP, java servlets was used for the server side programming  in which both the processing of the requests and the generation of the response were handled by a single servlet class. Java servlets uses HTML codes embedded in Java Codes , so it is necessary for a programmer to have a thorough knowledge of  java as well as HTML programming. Java Server Pages(JSP)  is a technology for developing web pages that include dynamic content. As a HTML page can contains only static content which always remains the same but a JSP page can change its content based on any number of variable items, including the identity of the user, the user's browser type, information provided by the user, and selections made by the user.

A JSP page contains standard markup language elements, such as HTML tags, just like a regular web page as well as it also contains special JSP elements that allow the server to insert dynamic content in the page. JSP elements can be used for a variety of purposes, such as retrieving information from a database or registering user preferences. When a user asks for a JSP page, the server executes the JSP elements, merges the results with the static parts of the page, and sends the dynamically composed page back to the browser.





Advantages of JSP over Servlets :- As we found servlets more advantageous over CGI scripts because servlet do not create process for all request, they create a thread which is lighter than a process and in all thread the resources are shared so there is not separate allocation of resources which in CGI all are treated as a separate process but there is a problem of using servlet. Servlet needs to recompile if any designing code is modified and doesn't provide separation Presentation and Business logic, they are mixed up.

JSP offers various advantages over Java Servlet. It provides a better server- side scripting support as compared to Java Servlet. Java programmer can easily create servlet but it is a tough job for a web page designer. A JSP page consists of HTML and some java code embedded in it. There are some reasons for using  JSP over servlets.

·        It facilitates automatic recompilation of JSP page by a web container for any update in the code
·        JSP allows to access a page easily as HTML
·        Allows to create a JSP page by using a simple HTML template and the JSP page is automatically handled by JSP container.


Architecture of JSP Page:- there are 2 approaches to build a web applications which are known as JSP MODEL I and JSP MODEL II.

The JSP MODEL I Architecture:- in this architecture, a web browser directly accesses  JSP pages of a web container which interact with JavaBean. The JSP or Servlet handles all the responsibility of request processing such as validating the data, business logic validation and processing, show error messages, finally generating the view for the application.
JSP Model 1 architecture is good for very small application, but it's not a good solution for big enterprise application. The JSP Model 1 architecture do not separate the view, business processing logic and the view is tightly integrated with the business logics.








Processing of Model I

1.     When a client browser sends request from a the JSP page
2.     JSP accesses Java Bean and invokes business logic and check which request parameters are invoked by the client request. 
3.     If generated response requires accessing of a database, the JSP page uses a JavaBean for retrieving the data from the database and get/save data
4.     Response is sent to the browser which is generated by JSP

The JSP MODEL II(MVC) Architecture Model 2 is based on the MVC (Model View Controller) design pattern.
The MVC design consists of three modules model, view and controller.
Model(Java Bean) The model represents the state (data) and business logic of the application.
View(JSP) :- The view module is responsible to display data i.e. it represents the presentation.
Controller (Servlet) :-  The controller module acts as an interface between view and model. It intercepts all the requests i.e. receives input and commands to Model / View to change accordingly.
Processing of Model II

1.     When a client browser sends request from a JSP page. JSP create the view for the application and a centralized Servlet is used to handle the entire request for the application which works as the controller for the application.
2.     This servlet instantiate Java Bean for processing the business logic and getting the data (Model) from the database if required.
3.     The controller servlet decides which JSP page to forward to as a response.
4.     A java bean instance invokes the database server if data is needed from the server
5.     Finally the selected JSP page render the view to the user.





Page No. : JSP lifecycle 1

No comments:

Post a Comment