JSP variables

How do I get a variable from the JSP c:set tag into the pure Java tag?

This is what I have got so far:

<c:set var="message" value="Hello World" />

String printMessage= page.getParameter( message );

This should just output Hello Word, but I get the error message:

An error occurred at line: 3 in the jsp file: /test.jsp
Generated servlet error:
message cannot be resolved

Any ideas what I am doing wrong? :sick:

“message” should probably be in quotes

I have tried it with quotes around message, but now I get the error:

An error occurred at line: 3 in the jsp file: /test.jsp
Generated servlet error:
The method getParameter(String) is undefined for the type Object

what type object is this:


that you are trying to call the method on … I bet ya its NOT a request object! :wink: … cause thats what that method is used on!

I think the <c:set /> tag has a scope attribute, though I could be wrong, but if I’m right then this should work

   <c:set var="message" value="Hello World" scope="page" />
   // always cast, page/request both return Object
   String printMessage= (String) pageContext.getParameter("message");

I have tried rozner’s code, but am still getting errors :'(, I have gone though loads of different tags to try and output it, but the closest I think I have got so far is this one:

   <c:set var="message" value="Hello World" scope="page" />

   String printMessage = pageContext.getAttribute("message");

With the error:

An error occurred at line: 3 in the jsp file: /test.jsp
Generated servlet error:
Type mismatch: cannot convert from Object to String

Any ideas how I get the JSP variable into a Java variable?

Ah!! Got it working, about 2 secs after post my last message!! Its:

   <c:set var="message" value="Hello World" scope="page" />

   String printMessage = (String) pageContext.getAttribute("message");

Ok so now one problem leads to another!! :frowning:

Its now printing Hello World, but now I can’t get it to print an int, float or double number :confused:
Here my code:

   <c:set var="message" value="123.456" scope="page" />

   int printMessage = (int) pageContext.getAttribute("message");

And this is the error:

An error occurred at line: 3 in the jsp file: /test.jsp
Generated servlet error:
Cannot cast from Object to int

The same happens if I change int to String, float or double, any ideas how to output numbers?

You’re trying to downcast objects into primitives. The only way to do that is with one of the number Objects like such

String num = “12345”;

int num = Integer.parseInt(num);

But you have to be careful with that cause you can blow some NASTY exceptions!

Every primitive type (int, double, float) has a corresponding Object to do this exact thing.

BUT (final one) … if you arent doing math with them WHY BOTHER!

To sum up what dc dalton said, you can’t cast from String to int like you can from Object to String.
If you want to convert a String to an int you use the code dc dalton posted:

int num = Integer.parseInt(otherVar);

This works:

   <c:set var="message" value="14.989999771118164" scope="page" />

   String printMessage = (String) pageContext.getAttribute("message");
   double num = Double.parseDouble(printMessage);

While for some reason this doesn’t:

<c:set var="total" value="${ row.price * dvd.quantity }" scope="page" />

String printMessage = (String) pageContext.getAttribute("total");
double num = Double.parseDouble(printMessage);

Here total is equal to 14.989999771118164, which is output in the first code sample, but in the second code sample I get the error:

org.apache.jasper.JasperException: java.lang.Double
java.lang.ClassCastException: java.lang.Double

Any ideas why I can’t get the value of an expression to output? As 14.989999771118164 and ${ row.price * dvd.quantity } both equal the same thing :frowning:

what type variables are these two row.price * dvd.quantity (coming in that is)

First thing I noticed was that in that expression you have NOT converted them into the proper data types (at least not in the code we can see) where is that happening?

Also have you tested them FIRST (before the math) to make sure you are getting the type data you are expecting?

Always test the incoming data when you are having problems like this … one little miss-spelling of a variable’s name OR something like a $ in a variable can blow you up. (not that that is the problem here but again without seeing the rest of the code we are just guessing)

Reason I ask is because it looks like whatever’s coming out of that parse double isnt right!

If you look at the API:

static double parseDouble(String s) expects a String to be passed to it NOT a double!

If youre already converting those two variables into numbers you already have a double NOT a String!

row.price is a float value (eg 14.99) coming from a MySQL database and dvd.quantity is just an number (eg 1) in the session stored using Java Bean. (Its a DVD online store for my uni java assignment) :rolleyes:

I have just displayed total, by just typing ${ total } and its output is 14.989999771118164

It outputs fine in JSP, but I cannot get the vaule into pure java code part eg <% %>. Any ideas how to get it working?

YES BUT AGAIN ::::::::::::::::::::::::::::

Double.parseDouble DOES NOT ACCEPT a number … it accepts a String!

HERE … right out of the API:


public static double parseDouble(String s) throws NumberFormatException

Returns a new double initialized to the value represented by the specified [B]String[/B], as performed by the valueOf method of class Double.

    s - the [B]string[/B] to be parsed.
    the double value represented by the [B]string argument.[/B]
    NumberFormatException - if the string does not contain a parsable double.
See Also:

This is ONE REASON why I HATE JSPS … if it was a servlet the compiler would have SCREAMED bloody murder about it!

Here is an example I just whipped up to show you:

public class NumTest {

public void testNum() {
double num = 12.34;
int quantity = 2;

/* THIS WORKS results are a double */
System.out.println(num * quantity);
/* This will NOT work because the parse double ONLY accepts a String */
System.out.println(Double.parseDouble(num * quantity));

/* IF you really have to parse a double BACK into a double (god knows why) */
System.out.println(Double.parseDouble(""+num * quantity));


Here is the exact error message from the compiler:

NumTest.java:11: parseDouble(java.lang.String) in java.lang.Double cannot be applied to (double)

System.out.println(Double.parseDouble(num * quantity));
1 error

This is ONE REASON why I HATE JSPS … if it was a servlet the compiler would have SCREAMED bloody murder about it!

This is probably the most important concept to grasp when working with JSP & Servlets. I went through the same thing trying to do calculations and processing, like connecting directly to JDBC, right in JSPs, very bad idea. This should be done in a servlet.

Of course this was all learning on my own so I was playing around with JSP & Servlets and it seemed natural coming from a PHP background, but as dc_dalton said, this type of work should be done in a Servlet. It makes life much easier. The compiler will generally tell you exactly what’s wrong where as JSP will give you a JasperException usually followed by a NullPointerException. Good luck figuring out where your problem is.

Humm I give up I used every combination I can think of and nothing works. :cry:

I will just skip this part of the assignment and move on.