To inject values into a JSP (Java Server Page) in Java, you can use the jsp:useBean
action tag and the getJspContext()
method.
The jsp:useBean
action tag allows you to create or locate a JavaBean object and store it in a variable. The getJspContext()
method returns the JspContext
object, which represents the runtime state of the JSP.
Here is an example of how to use the jsp:useBean
action tag and the getJspContext()
method to inject a value into a JSP:
<%@ page import="java.util.Date" %> <jsp:useBean id="currentTime" class="java.util.Date" /> <% currentTime.setTime(System.currentTimeMillis()); getJspContext().setAttribute("currentTime", currentTime); %> Current time: <%= currentTime %>
In this example, the jsp:useBean
action tag creates a new Date
object and stores it in the currentTime
variable. The JSP scriptlet then sets the time of the currentTime
object to the current system time, and uses the setAttribute()
method of the JspContext
object to set the value of the "currentTime" attribute to the currentTime
object. The value of the currentTime
object is then displayed on the JSP using the expression language (EL) <%= currentTime %>
.
For more information on JSP and how to use the jsp:useBean
action tag and the getJspContext()
method in Java, you can refer to the Java documentation and the Java EE tutorial.