How to use expression in jsp page

The JSP expression is the block of code which has to be as part of output in jsp. I.E. the code written in JSP expression will go as parameter of the out.write() method, and it will show as output is jsp.

You cannot use the semicolon at the end of an expression, because finally it has to go inside out.write() method as parameter.

The expression element can contain any expression that is valid according to java language specification

An expression can be complex and composed of more than one part of expression. The parts of an expression are evaluated in left-to –right order. This evaluation is performed at the run time, and thus has full access to information about the request. For example the following shows the date and time that the page requested.

JSP expression syntax

The JSP expression syntax is <%= expression code %>. You can write any java code which can be used a parameter of out.write() method of servlet. Your java code will be evaluated at run time when JSP run.

Example of JSP expression

Below id the example of JSP page, which use the JSP expression to print today date, addition of 4 and 5 and hostname using request object.

<html>
<%@page import="java.util.Date"%>
<head>
<title>JSP Expression Example</title>
</head>
<body>
Today date is : <%=new Date()%><br>
Addition of 4 and 5 is : <%=4 + 5%><br>
Your hostname is : <%=request.getRemoteHost()%>
</body>
</html>

The output of above JSP code will be as:

how-to-use-expression-in-jsp-page_files