Hi
I want to send session variable in an array from one JSP page to another using code
given below
String[] textfield_a =(String[])session.getAttribute("textfield_a");
But i m getting error .
plz help

Dani AI

Generated

A short, practical diagnosis and fix.

The likely cause of the error in 's original line is a type mismatch: casting to an array is valid only when the object actually stored in session is an array. was right to suggest using a List when that is what was stored, but the statement that "String[] does not exist" is incorrect — Java supports arrays. The key is to verify what was put into session and handle each case safely.

Quick checks (log the runtime type):

Object attr = session.getAttribute("textfield_a");
if (attr == null) {
    // attribute missing
} else {
    System.out.println("runtime class = " + attr.getClass().getName());
}

Safe retrieval patterns (handle array or list without blind casts):

Object attr = session.getAttribute("textfield_a");
if (attr instanceof String[]) {
    String[] vals = (String[]) attr;
    // use vals
} else if (attr instanceof java.util.List) {
    java.util.List<?> list = (java.util.List<?>) attr;
    String[] vals = list.toArray(new String[list.size()]);
    // use vals
} else {
    // handle single String or unexpected type
}

Notes for and servlet use: session attributes set in a JSP are visible in a servlet; obtain the HttpSession inside doGet/doPost via request.getSession(false) (false prevents creating a new one) and then call getAttribute with the same name. For and "normal" functions in JSP, avoid embedding business logic in scriptlets. Prefer a small utility method (in a helper class or bean) that accepts HttpSession or explicit values; in JSPs use JSTL/EL to access sessionScope when possible.

Troubleshooting checklist: verify attribute name and lifecycle, log the runtime class, use instanceof before casting, convert List->String[] when necessary, and avoid storing different shapes under the same attribute name.

Recommended Answers

All 4 Replies

Can you plese be clear in ur requirement.
Atleast can u send me the code snippet,

Hi
I want to send session variable in an array from one JSP page to another using code
given below
String[] textfield_a =(String[])session.getAttribute("textfield_a");
But i m getting error .
plz help

Member Avatar for Member #268089

You can't cast to String[]. It does not exist in Java.

Try to use an ArrayList instead.

All JSP pages share the same session. You will always have the variable in session until the session is terminated or until you remove the attribute yourself.

When you want the values back into another page you could use some code like this. ArrayList list = (ArrayList) session.getAttribute("callername"); Now you have the values in the list. on the second page

Now you can get the value with following code.

<% 
     Iterator iter = list.iterator();
     while(iter.hasNext()){
%>
     <input type="text" name="textfield1" value="<%= String.valueOf(iter.next()) %>">
<%
     }
%>

I created session in jsp like session.setAttribute("arr",xxx);
I want to get that sessionin servlet.plz help me

How to pass a session to a normal function in a jsp?? so that the function can access all session variables..

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.