0

I am trying to use below code to instantiate a Integer variable but I keep getting error like java.lang.InstantiationException: Not defined beanName or class!

<jsp:useBean id="selectedID" scope="session" type="java.lang.Integer" />

I am not sure but this piece of code used to work in JDK 1.4 but in JDK 1.6 it's throwing exception. Is there any other way to instantiate a Integer variable in JSP?

I tried using class="java.lang.Integer" but it didn't work because Integer wrapper doesn't have a no-arg constructor. I also tried below code but it reset the value during the runtime.

<% java.lang.Integer selectedID; %>

selectedID is used in JSP within Java blocks: switch (selectedID.intValue())

selectedID is set somewhere in a java class in a session. The set value is then used in JSP to render specific interface. All I need is to instantiate (not initialized) the variable in JSP.

Any inputs will be appreciated.

johnk1
  • 247
  • 8
  • 19

1 Answers1

-1
<% session.setAttribute("selectedID", new Integer(0)); %>

?

if you need to fetch it from request, you can use request.getParameter(), and to fetch it from session, session.getAttribute().

eis
  • 51,991
  • 13
  • 150
  • 199
  • Using *scriptlets* is [discouraged](http://stackoverflow.com/questions/3177733/how-to-avoid-java-code-in-jsp-files/3180202#3180202) since JSP 2.0 in 2003, already over a decade ago. – BalusC Apr 16 '13 at 15:42
  • @BalusC indeed. But without JSTL or any other help beyond just plain JSP, I don't see other ways to set variables to session scope as requested by OP. – eis Apr 16 '13 at 15:44
  • tried this but get "cannot find symbol symbol : variable selectedID" error while executing JSP. There is a switch statement in java block in JSP which is using selectedID value – johnk1 Apr 16 '13 at 15:46
  • @johnk1 it is setting the attribute directly to session - you have to read it back from session, too, for it to be available for scriplets. – eis Apr 16 '13 at 16:29