Java Server Pages (JSP) is a technology that helps in creating dynamically generated web pages based on HTML, XML, or other document types. JSP is an extension of Java Servlet technology.
1. Setting Up
Before we start, make sure you have:
- Java Development Kit (JDK) installed
- A web server like Apache Tomcat
- An IDE (e.g., Eclipse, IntelliJ IDEA)
2. Your First JSP Page
Create a file named hello.jsp
in your project's WebContent folder:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Hello JSP</title>
</head>
<body>
<h1>Hello, JSP World!</h1>
<% out.println("The current date is: " + new java.util.Date()); %>
</body>
</html>
This example demonstrates:
- JSP directive (
<%@ page ... %>
) - HTML mixed with Java code (
<% ... %>
)
3. JSP Scripting Elements
Expressions
Use <%= ... %>
to output the result of a Java expression:
<p>The sum of 5 and 7 is: <%= 5 + 7 %></p>
Scriptlets
Use <% ... %>
for Java code snippets:
<%
for(int i = 1; i <= 5; i++) {
out.println("<p>Line " + i + "</p>");
}
%>
Declarations
Use <%! ... %>
to declare methods or variables:
<%!
int count = 0;
void incrementCount() {
count++;
}
%>
4. JSP Implicit Objects
JSP provides several implicit objects, including:
-
request
: HttpServletRequest object -
response
: HttpServletResponse object -
out
: JspWriter object to send output to the client
Example:
<p>Your IP address is: <%= request.getRemoteAddr() %></p>
5. Working with Forms
Create a form in HTML and process it with JSP:
<form action="process.jsp" method="post">
Name: <input type="text" name="name"><br>
Email: <input type="email" name="email"><br>
<input type="submit" value="Submit">
</form>
In process.jsp
:
<%
String name = request.getParameter("name");
String email = request.getParameter("email");
%>
<p>Name: <%= name %></p>
<p>Email: <%= email %></p>
6. Including Files
Use the include
directive to include contents of another file:
<%@ include file="header.jsp" %>
<h1>Main Content</h1>
<%@ include file="footer.jsp" %>
7. Using JavaBeans
JavaBeans can be used to encapsulate many objects into a single object. Here's a simple example:
public class User {
private String name;
private String email;
// Getters and setters
public String getName() { return name; }
public void setName(String name) { this.name = name; }
public String getEmail() { return email; }
public void setEmail(String email) { this.email = email; }
}
Using the bean in JSP:
<jsp:useBean id="user" class="com.example.User" scope="session" />
<jsp:setProperty name="user" property="name" value="John Doe" />
<jsp:setProperty name="user" property="email" value="john@example.com" />
<p>Name: <jsp:getProperty name="user" property="name" /></p>
<p>Email: <jsp:getProperty name="user" property="email" /></p>
8. Error Handling
You can specify an error page to handle exceptions:
<%@ page errorPage="error.jsp" %>
In error.jsp
:
<%@ page isErrorPage="true" %>
<h1>An error occurred</h1>
<p>Error message: <%= exception.getMessage() %></p>
Conclusion
This tutorial covered the basics of JSP. To deepen your understanding, explore topics like Expression Language, and how JSP integrates with Servlets and other Java web technologies.
For more advanced Java topics, check out:
Happy coding with JSP!
Top comments (0)