Shaun Abram
Technology and Leadership Blog
JSP EL statements not being evaluated
On several occasions I have had problems with EL (Expression Language) statements not being evaluated.
As an example, you add a statement to a JSP like
$(2+2), expecting the JSP to simply display 4, when it in fact displays the raw statement, i.e. $(2+2).
After digging around the web, I found a number of points to check…
HelloWorld: Html -> Servlet -> JSP
A couple of times recently I have had to setup up some very simple code to display on the client side (browser) some values created on the server side based on basic user input.
This has been part of some prototype/proof-of-concept work. I am posting my basic setup as an example in the form of the ubiquitous HelloWorld app. It uses a Html page to receive user input, which is submitted to a Servlet, processed, and the response displayed in a JSP using EL (Expression Language).
Obviously this could be done in various ways, including just JSPs, but this example acts as a template of Html/Servlet/JSP interaction.
1) Create a Html page called HelloWorld.html
Greeting Setup - Hello Who?
2) Create a servlet to receive the user input
HelloWorldServlet.java
package com.helloworld;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class HelloWorldServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public void doGet(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
//get inputs
String msg = req.getParameter("msg");
//any additional processing can be done here
//set outputs
req.setAttribute("msg", msg);
//forward to jsp
req.getRequestDispatcher("/HelloWorld.jsp").forward(req, res);
}
}
3) Create a JSP to display the response
HelloWorld.jsp:
Hello ${msg}
4) Create the web application’s deployment descriptor file:
web.xml
HelloWorld.html
HelloWorldServlet
com.helloworld.HelloWorldServlet
HelloWorldServlet
/HelloWorldServlet
That’s it.
The easiest way I have found for this kind of development is to create in Eclipse as a Dynamic Web Project (available via the Web Tools Platform that comes with the in the “Eclipse IDE for Java EE Developers” package). When you have carried out the above steps, just right click the project -> Run As -> Run on Server (assuming you have a server like Tomcat or GlassFish setup in Eclipse…).
I have attached the source in a war file.