How to include page dynamically in JSP page?

In this example we are going to learn how to use <jsp:include> action. This action can be used to include resource dynamically to our JSP pages. For example the resource can be another JSP page, a servlet or event a static html pages. But to make it enable to be processed as a JSP page, such as accepting parameters, we must use the .jsp as the file extension. If we use other extension such as .jspf, it will be processed as a static page.

The other things to note is that using the <jsp:include> action will process the page inclusion at the request time. This is why we can pass parameters to the included page using the <jsp:param>. The value can be read by obtaining the parameter from the request object or using expression language variable param.

But if we use the <%@ include %> directive the inclusion of the page happen when it translated into a Servlet. See the following example about the <%@ include %> directive: How do I include a page fragment into JSP?

<%@ page contentType="text/html;charset=UTF-8" %>
<!DOCTYPE html>
<html lang="en">
<head>
    <title>JSP - Include Demo</title>
</head>
<body>

Lorem Ipsum

<jsp:include page="jspf/footer.jsp">
    <jsp:param name="year" value="2021"/>
</jsp:include>

</body>
</html>

Below is the content of our footer.jsp page. In this page we display the footer information with a parameter read from the request object.

<%@ page contentType="text/html;charset=UTF-8" %>
<hr/>
Copyright © ${param["year"]} Kodejava.org. All rights reserved.

This example will give you the following result in the browser:

JSP Include Action Demo

JSP Include Action Demo

How do I forward request to another page in JSP?

To forward a request from one page to another JSP page we can use the <jsp:forward> action. This action has a page attribute where we can specify the target page of the forward action. If we want to pass parameter to another page we can include a <jsp:param> in the forward action. But make sure that the forward page is a JSP page to enable the parameters to be processed.

<%@ page contentType="text/html;charset=UTF-8" %>
<!DOCTYPE html>
<html lang="en">
<head>
    <title>JSP - Forward Demo</title>
</head>
<body>

<jsp:forward page="message.jsp">
    <jsp:param name="message" value="Welcome!"/>
</jsp:forward>

</body>
</html>

And here is how to read the parameters in the message.jsp.

<%@ page contentType="text/html;charset=UTF-8" %>
<!DOCTYPE html>
<html lang="en">
<head>
    <title>Message: ${param["message"]}</title>
</head>
<body>
Message: ${param["message"]}
</body>
</html>
jsp:forward Demo

jsp:forward Demo

When you access the jspForward.jsp page in the web browser what you will see is the content of the message.jsp page. This is because the <jsp:forward> action forward your request to the message.jsp before returning the response back to the browser for display.

How do I use jsp:useBean action in JSP pages?

In this example you will learn how to use the <jsp:useBean>. We can use the useBean action to create a scripting variables. To define the variable name we use the id attribute of this action. The class attribute define the type of this variable. We can also define the scope of the variable using the scope attribute.

As an example in the code snippet below we create a variable name now and declare it to hold a java.util.Date instance. And then we use a JSP expression to print out the value.

<%@ page contentType="text/html;charset=UTF-8" %>
<!DOCTYPE html>
<html lang="en">
<head>
    <title>JSP - useBean Demo</title>
</head>
<body>

<jsp:useBean id="now" class="java.util.Date"/>

Now is: <%= now %>

</body>
</html>
jsp:useBean Demo

jsp:useBean Demo

How do I create JRadioButton and define some action?

Below we create a JRadioButton and pass an action handle to the component so that we know when the component is clicked.

package org.kodejava.swing;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;

public class RadioButtonCreateAction extends JFrame {
    public RadioButtonCreateAction() {
        initializeUI();
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(
                () -> new RadioButtonCreateAction().setVisible(true));
    }

    private void initializeUI() {
        setSize(300, 300);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        setLayout(new FlowLayout());

        Action action = new AbstractAction() {
            public void actionPerformed(ActionEvent e) {
                JRadioButton button = (JRadioButton) e.getSource();
                System.out.println("actionPerformed " + button.getText());
            }
        };

        // Create a radio button and pass an AbstractAction as its constructor.
        // The action will be call everytime the radio button is clicked or
        // pressed. But when it selected programmatically the action will not 
        // be called.
        JRadioButton button = new JRadioButton(action);
        button.setText("Click Me!");
        button.setSelected(true);

        getContentPane().add(button);
    }
}