[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 520
  • Last Modified:

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

0
gopikrish
Asked:
gopikrish
1 Solution
 
basicinstinctCommented:
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

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now