Getting JSP Compile error for session.putAttribute ?

In my book.jsp I have,

<%
  String str[]=request.getParameterValues("s1");
  session.putAttribute("bookpur",str);
  %>
<%= session.isNew() %>
<a href="sport.html">sport</a>

Getting "s1" from book.html. But when I try to come to book.jsp from book.html its giving the following error,

C:\DOCUME~1\Gopi\LOCALS~1\Temp\jsp_servlet\__book.java:123: cannot resolve symbol
symbol  : method putAttribute (java.lang.String,java.lang.String[])
location: interface javax.servlet.http.HttpSession
      session.putAttribute("bookpur",str); //[ /book.jsp; Line: 3]
             ^
1 error

gopikrishAsked:
Who is Participating?
 
basicinstinctConnect With a Mentor Commented:
What about if you change this:

session.putAttribute("bookpur",str);

To this:

session.setAttribute("bookpur",str);
0
 
Siva Prasanna KumarPrincipal Solutions ArchitectCommented:
generally

session.getAttribute(); is used to retrive the stored session attributes.

To set the attributes(Objects) in session you need to use session.setAttribute(); as per basicinstants above suggestion.

in your case to put the str[] in the session you need to use

session.setAttribute("bookpur",str);

and to retrieve back the stuff

String arr[] = (String[]) session.getAttribute("bookpur");

now your stored  session str[] will be got to arr[].

One more ting make sure you do a null Check when ever you try to retrieve a session Attribute. like

if(session.getAttribute("bookpur")!=null)
{
String arr[] = (String[]) session.getAttribute("bookpur");
}
else
{
//do some thing.
}

Thank You.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.