easy question

I am starting to learn JSP and the most simple of scripts does not seem to be working as it should.

index.jsp contains the following:

2+2 is ${2+2}

When you open index.jps with browser, it should say "2+2 is 4" but instead it says "2+2 is ${2+2}".

I have made sure that Tomcat is running.

Any ideas what I'm doing wrong?

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.


try <%=2+2%>
JSP doesn't evaluate the expressions as you have written because it's crudely java scriptlets embedded in an html page. So, it needs to know when to evaluate an expression and when to take it as a literal or a string. To do so, it evaluates anything which is between the tags <%= (your expression)%>.  It considers anything between the tags <% %> as the java code and acts accordingly. So, when you put anything outside the jsp tags, it considers it as a simple string and displays as it is.
So, to evaluate the expression 2+2, you need to have , as jv-st correctly said,

keseaAuthor Commented:
WHen I try this:

 2+2 is <%=2+2%>

the browser displays "2+2 is " with nothing filled in.
Bootstrap 4: Exploring New Features

Learn how to use and navigate the new features included in Bootstrap 4, the most popular HTML, CSS, and JavaScript framework for developing responsive, mobile-first websites.

It should. Anyway try it:
<%= (2+2) %>

Also, confirm whether your filename ends with .jsp.
secondly how you are accesing your jsp?? it should be served thru a app server like tomcat or jboss or websphere...
Ohh.. yea. I presume you are not opening the jsp page in the IE by double-clicking it or something of that sort. It has to be thru your app server.
Andrew BeersTechnology LeadCommented:
Using tomcat or Websphere it would be using localhost::8080/page.jsp

And your page has to be on your web server / simulated web server folder.


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
>it should say "2+2 is 4" but instead it says "2+2 is ${2+2}".
Yes, it should if you are using JSP 2.0.  
You need to use Tomcat 5 for JSP 2.0.
Do the jsp examples that came with your Tomcat work ?
Also the root tag in your context's web.xml  must be for Servlet 2.4 API.
Below is an empty  web.xml file.

<?xml version="1.0" encoding="ISO-8859-1"?>

<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee web-app_2_4.xsd"

keseaAuthor Commented:
I do apologize for being such a newb.  

The real answer to my problem was answered by Kupra1 and clarified by aqua9880.  rrz@871311 helped a lot too though because yes I am using JSP2 on Tomcat5 and I was confused why this book I am learning from would be wrong but I guess <%=2+2%> is for JSP1 - I think he showed the most insight to my problem by recognizing the differences between JSP2 and JSP1 and I appreciate that a lot.


keseaAuthor Commented:
So just to clarify, it worked when opened through localhost::8080/page.jsp and "2+2 is ${2+2}" printed out "2+2 is 4".

Thanks for everyone's help.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.