Java Server Pages (JSP) is a technology used for developing web pages that support dynamic content. It is part of the Java EE (Enterprise Edition) platform and allows developers to embed Java code in HTML pages. JSP is commonly used to create dynamic web applications, where the content of web pages can change based on user interactions or data from a database.
Key Features of JSP:
- Separation of Concerns: JSP separates the presentation layer from the business logic, making it easier to manage and develop web applications.
- Reusable Components: JSP supports the use of JavaBeans and custom tags, which promote code reuse and modularity.
- Built-in Objects: JSP provides several implicit objects (e.g.,
request
,response
,session
,application
) that simplify the development process.
JSP Syntax:
JSP files are HTML files with embedded Java code. The embedded Java code is enclosed within special tags.
- Scriptlet: A scriptlet contains Java code that is executed when the JSP is requested. It is enclosed within
<% %>
tags.
<%
// Java code here
int count = 5;
out.println("Count: " + count);
%>
2. Expression: An expression evaluates a Java expression and converts it to a string, which is then inserted into the output. It is enclosed within <%= %>
tags.
<%= "Hello, World!" %>
3. Declaration: A declaration declares one or more variables or methods that can be used in the JSP file. It is enclosed within <%! %>
tags.
<%!
int counter = 0;
public int getCounter() {
return counter++;
}
%>
Example JSP Page:
Here’s a simple example of a JSP page that displays a welcome message and the current date and time.
welcome.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<title>Welcome Page</title>
</head>
<body>
<h1>Welcome to My Website</h1>
<p>The current date and time is: <%= new java.util.Date() %></p>
<%
// Java scriptlet to perform some processing
String userName = request.getParameter("name");
if (userName == null || userName.isEmpty()) {
userName = "Guest";
}
%>
<p>Hello, <%= userName %>!</p>
</body>
</html>
Explanation:
- Directive: The
<%@ page %>
directive defines page-dependent attributes, such as the language used, the content type, and the page encoding. - Expression:
<%= new java.util.Date() %>
outputs the current date and time. - Scriptlet: The scriptlet checks if a request parameter
name
is provided. If not, it defaults to “Guest”. - Expression:
<%= userName %>
outputs the user’s name.
Using JavaBeans in JSP:
JavaBeans are reusable software components that follow certain conventions. They can be used in JSP to encapsulate business logic.
UserBean.java
public class UserBean {
private String firstName;
private String lastName;
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getFullName() {
return firstName + " " + lastName;
}
}
user.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<jsp:useBean id="user" class="com.example.UserBean" scope="request"/>
<jsp:setProperty name="user" property="firstName" value="John"/>
<jsp:setProperty name="user" property="lastName" value="Doe"/>
<!DOCTYPE html>
<html>
<head>
<title>User Page</title>
</head>
<body>
<h1>User Information</h1>
<p>Full Name: <jsp:getProperty name="user" property="fullName"/></p>
</body>
</html>
Explanation:
- jsp: Declares a JavaBean for use in the JSP page. The
id
attribute is the reference name,class
specifies the fully qualified class name, andscope
defines the bean’s lifecycle. - jsp: Sets properties of the JavaBean.
- jsp: Retrieves properties from the JavaBean.
By using JSP, developers can create dynamic, interactive web applications that separate business logic from the presentation layer, resulting in more maintainable and scalable code.