Skip to main content

Using JSP Expressions with Example

The following example illustrates using JSP expressions.
An expression is a combination of symbols that represents a value. Examples of expression include a+b, a*b, new Date() etc.
In JSP you can get an expression value and print it without the need of writing in scriptlets. The syntax is as follows

<%=some-expression%>

Here the result of this expression is returned as String object. If the tag alone is written in a JSP page, then the expression value is printed. In simple words, you can get the result of a function call, an arithmetic operation or an object creation using the expression.

The expression shouldn't return a void value.

index.jsp


<html>
    <body>
    <%="This is sample text"%><br/>
    <%=10+20%><br/>
    <%=new Integer(20)%><br/>
    <%="The difference is"+(20-10)%>
   
    <%--
    This is an error, void type not allowed
    <%=out.println("hai")%>
    --%>
    </body>
</html>

Output of the program


This is sample text
30
20
The difference is10

Previous: <jsp:forward>

References

http://docs.oracle.com/javaee/5/tutorial/doc/bnaov.html

Comments

Popular posts from this blog

Setting Background Image in JFrame - Swing

Here is sample tutorial, a simple trick that enables you to set background image for JFrame.



import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class BackgroundImageJFrame extends JFrame
{
JButton b1;
JLabel l1;
public BackgroundImageJFrame()
{
setTitle("Background Color for JFrame");
setSize(400,400);
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
/*
One way
-----------------
setLayout(new BorderLayout());
JLabel background=new JLabel(new ImageIcon("C:\\Users\\Computer\\Downloads\\colorful design.png"));
add(background);
background.setLayout(new FlowLayout());
l1=new JLabel("Here is a button");
b1=new JButton("I am a button");
background.add(l1);
background.add(b1);
*/
// Another way
setLayout(new BorderLayout());
setContentPane(new JLabel(new ImageIcon("C:\\Users\\Computer\\Downloads\\colorful design.png")));
setLayout(new FlowLayout());
l1=new JLabel("Here is a button");
b1=new JButton("I am …

Add two numbers in JSP easily

Here is an example on adding up two numbers in JSP. This is very simple as you see below. This requires very small piece of code. All i have done here is created a HTML file containing the input fields which contain the numbers to add up.Now the values in the input field are of type String, so i have done type casting and converted them into int using Integer.parseInt(). See how easy it looks.



Catch The Rat: Simple Game in Java

A simple, shortest and easiest game that could be written in Java using Swing components and event handling.