Thursday, 19 January 2017

JSP Life Cycle

JSP provide excellent server side scripting support for creating database driven web applications. JSP enable the developers to directly insert java code into jsp file, this makes the development process very simple and its maintenance also becomes very easy.  JSP pages are efficient, it loads into the web server’s memory on receiving the request very first time and the subsequent calls are served within a very short period of time.

 JSP over HTML

·DB driven applications can be written in JSP. With JDBC all user data can be stored in DB and connectivity can be done in JSP

·Dynamic content is possible in JSP

·Server side scripting language. No need to change data on every page

JSP over Servlets
·We go for JSP to separate the business logic from the presentation logic. But in servlets, we will write both business logic and presentation logic together.

·There problems come into the picture. If we do any modification to the presentation logic, we have to re-deploy the servlet in order to see the result of modified code of the servlet. So for every time we modifying the servlet, we should re-deploy the servlet. But where as in the case of JSP, we will write presentation logic in jsp and business logic inside the java bean and we use that java bean object in our jsp page using <jsp:useBean> tag. So, even if we do any modification to the presentation logic, no need to re-deploy the jsp.

JSP Working
·To process a JSP file, we need a JSP engine that can be connected with a web server or can be accommodated inside a web server.

·Firstly when a web browser seeks a JSP file through an URL from the web server, the web server recognizes the .jsp file extension in the URL requested by the browser and understands that the requested resource is a Java Server Page. Then the web server passes the request to the JSP engine. The JSP page is then translated into a Java class, which is then compiled into a servlet.

·This translation and compilation phase occurs only when the JSP file is requested for the first time, or if it undergoes any changes to the extent of getting retranslated and recompiled. For each additional request of the JSP page thereafter, the request directly goes to the servlet byte code, which is already in memory.

·Thus when a request comes for a servlet, an init () method is called when the Servlet is first loaded into the virtual machine, to perform any global initialization that every request of the servlet will need.

·Then the individual requests are sent to a service() method, where the response is put together. The servlet creates a new thread to run service() method for each request.

·The request from the browser is converted into a Java object of type HttpServletRequest, which is passed to the Servlet along with an HttpServletResponse object that is used to send the response back to the browser. The servlet code performs the operations specified by the JSP elements in the .jsp file.

JSP Life Cycle

· Translation
· Compilation
·  Loading
·  Instantiation
·  Initialization
·  Request Processing
·  Destruction



JSP Tags

Element data or that part of the JSP which is processed on the server, can be classified into the following categories:
1. Directives
2. Scripting elements
3. Standard actions

Scripting elements are used to include scripting code (Java code) within the JSP.
·They allow declaring variables and methods
·They include arbitrary scripting code and evaluate an expression.
·The three types of scripting element are: Declaration, Scriptlets and Expressions.

declaration is a block of Java code in a JSP that is used to define class-wide variables and methods in the generated class file. Declarations are initialized when the JSP page is initialized and have class scope. Anything defined in a declaration is available throughout the JSP, to other declarations, expressions or code.

scriptlet consists of one or more valid Java statements.
·A scriptlet is a block of Java code that is executed at request-processing time.
.A scriptlet is enclosed between "<%" and "%>".
·Scriptlets like any other Java code block or method, can modify objects inside them as a result of method invocations.

An expression is a shorthand notation for a scriptlet that outputs a value in the response stream back to the client. When the expression is evaluated, the result is converted to a string and displayed. An expression is enclosed within <%= and %> "<%=" and "%>". If any part of expression is an object, the conversion is done using the toString() method of the object.

Standard actions are specific tags that affect the runtime behaviour of the JSP and affect the response sent back to the client. The JSP specification lists some standard action types to be provided by all containers, irrespective of the implementation. Standard actions provide page authors with some basic functionality to exploit

1 comment:

  1. As we know there are many companies which are converting into Hadoop consulting services. with the right direction we can definitely predict the future.

    ReplyDelete