1. Simple JSP Program
-
JSP is a technology used to create dynamic web pages by embedding Java code in HTML.
-
A basic JSP page is a
.jspfile that contains a mix of HTML and Java code (embedded using JSP tags). When a request for a JSP page is made, the server compiles the JSP into a servlet and processes it. -
Example of a simple JSP program:
<%-- This is a comment in JSP --%> <html> <body> <h2>Hello, Welcome to JSP!</h2> <% String name = "John"; out.println("Hello, " + name); %> </body> </html>
### 2. Life Cycle of a JSP
The life cycle of a JSP page is similar to that of a servlet, involving the following stages:
1. Translation: When the JSP page is requested for the first time, the servlet container translates the JSP file into a servlet (i.e., Java code). This servlet is then compiled.
2. Initialization: The servlet container creates an instance of the servlet (the translated JSP page) and initializes it (using the `init()` method of the servlet).
3. Request Handling: For each request, the servlet's `service()` method is called. This method generates dynamic content by executing the embedded Java code in the JSP page.
4. Destruction: When the JSP page is no longer needed, the servlet container calls the `destroy()` method to clean up any resources.
### 3. Implicit Objects
JSP provides several implicit objects that you can use without explicitly declaring them. These objects are automatically available within the JSP page:
- request: The `HttpServletRequest` object that contains the request information.
- response: The `HttpServletResponse` object that allows you to send the response back to the client.
- out: A `PrintWriter` object that is used to send output to the client.
- session: The `HttpSession` object used to store session-related data.
- application: The `ServletContext` object used to store application-level data.
- config: The `ServletConfig` object used to get initialization parameters for the servlet.
- pageContext: Provides access to various objects related to the page (e.g., session, request).
- exception: An object that provides access to any uncaught exceptions that occurred during the request processing.
### 4. Scripting Elements
JSP allows you to embed Java code directly into the HTML using three main types of scripting elements:
1. Declarations:
- Declaring variables and methods inside a JSP page.
- Syntax:
```html
<%! int counter = 0; %>
- Declarations are placed in the declaration section and are available throughout the JSP page.
- Expressions:
- Used to output values directly to the response. The result of the expression is automatically converted to a string and displayed.
- Syntax:
<%= "Current Time: " + new java.util.Date() %>- The output will be displayed on the webpage wherever the expression is used.
-
Scriplets:
-
These allow you to write Java code inside a JSP page.
-
Syntax:
jsp
CopyEdit
<% int sum = 10 + 20; out.println("The sum is: " + sum); %> -
Scriplets can be used for more complex logic or operations within the JSP page.
-
-
Comments:
-
JSP allows two types of comments:
-
HTML Comments: These comments are visible in the source code.
html
CopyEdit
<!-- This is an HTML comment --> -
JSP Comments: These comments are not visible in the output (client-side).
jsp
CopyEdit
<%-- This is a JSP comment --%>
-
-
5. JSP Directives
JSP directives provide instructions to the container about how to process the JSP page. There are several types of directives:
-
Page Directive:
-
It provides page-level settings such as content type, language, etc.
-
Syntax:
jsp
CopyEdit
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" %> -
This directive can also be used to include tag libraries, specify buffering, etc.
-
-
Include Directive:
-
It allows you to include content from another file at the time the JSP page is compiled.
-
Syntax:
jsp
CopyEdit
<%@ include file="header.jsp" %> -
The content of
header.jspwill be inserted at the place where the include directive is placed.
-
6. Mixing Scriplets and HTML
- JSP allows you to combine Java code (scriplets) with HTML. You can write Java code to dynamically generate HTML content. For example:
<html>
<body>
<h2>Welcome to the User Dashboard</h2>
<% String userName = "John";
out.println("<p>Welcome, " + userName + "!</p>"); %> </body>
</html>- Here, the HTML part is static, and the JSP scriplet dynamically generates the content (
Welcome, John!).
Summary
- JSP pages allow for dynamic content generation by embedding Java code in HTML.
- The JSP lifecycle includes translation, initialization, request handling, and destruction.
- Implicit objects such as
request,response,out, etc., simplify handling data in the JSP. - Scripting elements include declarations (for variables/methods), expressions (for output), and scriplets (for Java logic).
- JSP directives control the page-level configuration, like setting the language or including other files.