1. JSP Introduction And Life-Cycle

 

What is JavaServer Pages?

- Java Server Pages (JSP) is a server-side programming technology that use to create of dynamic web application using java programming Language. 

- JSP have access to the entire family of Java APIs, including the JDBC API to access enterprise databases. 

- A JSP page consists of HTML tags and JSP tags.

- JavaServer Pages (JSP) is a technology for developing web pages that support dynamic content.

- This helps developers insert java code in HTML pages by making use of special JSP tags, most of which start with <% and end with %>.


Syntax:

JSP code is written between <% %> tags.

<%= expression %> is used for embedding expressions in HTML.


Why to Learn JSP ?

- JSP allows embedding Dynamic Elements in HTML Pages itself instead of having separate CGI files.

- JSP pages can be used in combination with servlets that handle the business logic, the model supported by Java servlet template engines.

- Using JSP, you can collect input from users through Webpage forms, present records from a database or another source, and create Webpages dynamically.


Servlet Vs JSP

JSP, Servlets are also used for generating dynamic webpages. 

The major difference between them is that servlet adds HTML code inside java file while JSP adds java code inside HTML. 

JSP

1. JSP program is a HTML code which supports java statements too.To be more precise, JSP embed java in html using JSP tags.

2. Used for developing  presentation layer of an enterprise application

3. Frequently used for designing websites and used by web developers.


Servlets:

1. Servlet is a Java program which supports HTML tags too.

2. Generally used for developing business layer(the complex computational code) of an enterprise application.

3. Servlets are created and maintained by Java developers.



JSP Lifecycle   

A JSP life cycle is defined as the process from its creation till the destruction. This is similar to a servlet life cycle with an additional step which is required to compile a JSP into servlet.





1. Translation
   - JSP pages start as simple text files with a .jsp extension.
   - During translation, the JSP container converts the JSP page into a servlet.
   - The translation process involves parsing the JSP file, creating a servlet class, and generating the necessary code.

2. Compilation:
   - The generated servlet class is compiled into bytecode by the Java compiler.
   - This bytecode is then loaded and executed by the Java Virtual Machine (JVM) when the JSP page is requested for the first time.

3. Instantiation:
   - An instance of the servlet class is created by the servlet container.
   - This instance represents the JSP page and is used to handle multiple requests concurrently.

4. Initialization:
   - During initialization, the container calls the `init()` method of the servlet.
   - This is where initialization tasks, such as setting up resources or establishing connections, can be performed.
   - This method is invoked only once during the lifecycle of the servlet. 

5. Request Processing:
  - The service() method is called to process client requests.
  - This phase involves handling HTTP requests, generating dynamic content, and interacting with any other components required to fulfill the request.

6. Destruction:
   - When the JSP page is no longer needed (e.g., when the web application is being shut down), the container calls the `destroy()` method of the servlet.
   - The `destroy()` method allows for cleanup activities, such as releasing resources or closing database connections.




Translation
Convert .jsp to .java(Servlet)

Hello.jsp
    <html>
        <head>
                < title>Hello World</title>
        </head>
        <body>
            <% out.println("Hello World"); %>
        </body>
</html>

Converted to Servlet

public class Hello_jsp extends HttpServlet{

public void _jspService(HttpServletRequest request, HttpServletResponse response) 
throws IOException,ServletException{

    PrintWriter out = response.getWriter();
    response.setContenType("text/html");

out.write("<html>\r\n");
out.write("<head>\r\n");
out.write("<title>Hello World</title>\r\n");
out.write("</head>\r\n");
out.write("<body>\r\n");
out.println("Hello World"); 
out.write("\r\n");
out.write("</body>\r\n");
out.write("</html>");
}
}




Comments

Popular posts from this blog

2. JSP Scripting elements & Directives

3. JSP Action Tag and Expression Language