How to use JSP for presenting data

You can display data in JSP using Java code and JSP tags. Here is a simple example demonstrating how to display data on a JSP page.

Create a JSP file named `index.jsp` and write the following code in it:

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>

<!DOCTYPE html>

<html>

<head>

    <meta charset="UTF-8">

    <title>Data Display Example</title>

</head>

<body>

    <%-- 定义一个 Java 变量 --%>

    <% String message = "Hello, JSP!"; %>

    

    <h1>Message: <%= message %></h1>

</body>

</html>

Save the above code as ‘index.jsp’ file and place it in the WebContent directory or subdirectory of your web application.

3. Access `http://localhost:8080/your-application-name/index.jsp` in a web browser (assuming your application is running on port 8080 on a local server). You will see a page displaying “Message: Hello, JSP!”.

In the above example, we defined a Java variable `message` and inserted its value into an HTML page using JSP expression `<%= message %>`. You can pass data from the backend to the JSP page according to your needs, and then display it using JSP tags and expressions.

In addition to using Java variables, you can also utilize JSP tag libraries (taglib) and JSTL (JSP Standard Tag Library) to easily display data. These libraries offer various functions, such as loops, conditional statements, making data display more flexible and easy to manage.

bannerAds