DEV Community

joash
joash

Posted on

# JSP Tutorial for Beginners

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>
Enter fullscreen mode Exit fullscreen mode

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>
Enter fullscreen mode Exit fullscreen mode

Scriptlets

Use <% ... %> for Java code snippets:

<% 
    for(int i = 1; i <= 5; i++) {
        out.println("<p>Line " + i + "</p>");
    }
%>
Enter fullscreen mode Exit fullscreen mode

Declarations

Use <%! ... %> to declare methods or variables:

<%! 
    int count = 0;
    void incrementCount() {
        count++;
    }
%>
Enter fullscreen mode Exit fullscreen mode

4. JSP Implicit Objects

JSP provides several implicit objects, including:

Example:

<p>Your IP address is: <%= request.getRemoteAddr() %></p>
Enter fullscreen mode Exit fullscreen mode

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>
Enter fullscreen mode Exit fullscreen mode

In process.jsp:

<%
    String name = request.getParameter("name");
    String email = request.getParameter("email");
%>
<p>Name: <%= name %></p>
<p>Email: <%= email %></p>
Enter fullscreen mode Exit fullscreen mode

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" %>
Enter fullscreen mode Exit fullscreen mode

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; }
}
Enter fullscreen mode Exit fullscreen mode

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>
Enter fullscreen mode Exit fullscreen mode

8. Error Handling

You can specify an error page to handle exceptions:

<%@ page errorPage="error.jsp" %>
Enter fullscreen mode Exit fullscreen mode

In error.jsp:

<%@ page isErrorPage="true" %>
<h1>An error occurred</h1>
<p>Error message: <%= exception.getMessage() %></p>
Enter fullscreen mode Exit fullscreen mode

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)