This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes JSP and the fly likes Session variable returns null value Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "Session variable returns null value" Watch "Session variable returns null value" New topic
Author

Session variable returns null value

Lakshi Meghoo
Greenhorn

Joined: Feb 03, 2009
Posts: 5
Hi all,

I am developing a WAP application using jsp. I'm saving a variable in session in 1 page and retrieving the value in another page.


My codes are as follows:

chooseques.jsp

<%@ page contentType="text/vnd.wap.wml" %>

<%@page language="java" import="java.sql.*" %>


<%

Connection con = null;
String url = "jdbc:mysql://localhost:3306/";
String dbName = "asssys";
String driver = "com.mysql.jdbc.Driver";
String userName = "root";
String password = "";
Statement st;
String mod1 = request.getParameter("mod");
int i=1;
int j=0;

session.setAttribute( "module1", mod1 );

System.out.println("module1="+session.getAttribute("module1"));



try{
Class.forName("org.gjt.mm.mysql.Driver").newInstance();

con = DriverManager.getConnection(url+dbName,userName,password);
}
catch(Exception e){
System.out.println(e.getMessage());
}


String s="select * from questions where questions.mid="+mod1;

System.out.println(mod1);

%>

<card id="card2" title="Choose questions">
<form method="POST" action="choosestud.jsp">

<%
try{
st = con.createStatement();
ResultSet rs=st.executeQuery(s);


while (rs.next()){%>

<p align="left">

<b><%=i%>. <%=rs.getString(3)%></b><br/>
<b>A.</b> <%=rs.getString(4)%><br/>
<b>B.</b> <%=rs.getString(5)%><br/>
<b>C.</b> <%=rs.getString(6)%><br/>
<b>D.</b> <%=rs.getString(7)%><br/>
<input type="checkbox" name="chk<%=+i%>" checked="checked" value="<%=rs.getString(1)%>" />
<br/>
<br/>
<hr/><hr/><br/>

<% i++; j++;
}//while
%>

//I want to save an integer value in session. I'm casting it to string first.
<% String count = Integer.toString(j);



session.setAttribute( "sess_count", count );


System.out.println("sess_count="+session.getAttribute("sess_count"));// This displays the sess_count correctly




%>

<p><input type="submit" value="Submit" ></p>
</form>
</card>

<%


rs.close();
con.close();

}
catch(Exception e){
System.out.println(e.getMessage());
}


%>




displayques.jsp


<%@ page contentType="text/vnd.wap.wml" %>

<%@page language="java" import="java.sql.*" %>
<%


String count =(String)session.getAttribute("sess_count");//I'm retriving the session variable but it returns null!!


int counter = Integer.parseInt(count1);

System.out.println("count ="+count);



Connection con = null;
String url = "jdbc:mysql://localhost:3306/";
String dbName = "asssys";
String driver = "com.mysql.jdbc.Driver";
String userName = "root";
String password = "";
Statement st;

try{
Class.forName("org.gjt.mm.mysql.Driver").newInstance();

con = DriverManager.getConnection(url+dbName,userName,password);
}
catch(Exception e){
System.out.println(e.getMessage());
}


int i=0;
int j=1;
String[] arr = new String[counter];
%>

<card id="card3" title="Display selected questions">

<%

while (i<counter){
arr[i] = request.getParameter("chk"+j);

String s="select * from questions where questions.qid="+request.getParameter("chk"+j);



try{
st = con.createStatement();
ResultSet rs=st.executeQuery(s);
rs.next();

%>

<p align="left">

<b><%=j%>. <%=rs.getString(3)%></b><br/>
<b>A.</b> <%=rs.getString(4)%><br/>
<b>B.</b> <%=rs.getString(5)%><br/>
<b>C.</b> <%=rs.getString(6)%><br/>
<b>D.</b> <%=rs.getString(7)%><br/>
<input type="hidden" name="chk<%=rs.getString(1)%>" value="<%=rs.getString(1)%>" />
<br/>
Display qid: <%=rs.getString(1)%>
<hr/><hr/><br/></p>


<%

rs.close();

}//try

catch(Exception e){
System.out.println(e.getMessage());
}//catch

i++; j++;

}// while


con.close();
%>

------------------------------------------------------------------------------

Can anyone tell me why the session variable is returning null value? What is the problem with the code?
Your reply will be of great help.


Balagopal Kannampallil
Ranch Hand

Joined: Oct 18, 2004
Posts: 136

Try using request.getSession().setAttribute("sess_count", count ); while setting the object into session and request.getSession().getAttribute("sess_count"); while retrieving it back.


SCJP 5.0 and now fighting with ExtJS
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61043
    
  66

Please be sure to use code tags when posting code to the forums. Unformatted code is extremely hard to read and many people that might be able to help you will just move along to posts that are easier to read. Please read this for more information.

You can go back and change your post to add code tags by clicking the button on your post.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Lakshi Meghoo
Greenhorn

Joined: Feb 03, 2009
Posts: 5
Hi,

Balagopal

I tried the codes you gave me but still the session returns null.

Do you have other suggestions?

I really need to solve this problem.

Balagopal Kannampallil
Ranch Hand

Joined: Oct 18, 2004
Posts: 136

If we put it in session, you should be able to retrieve it as long as you are sharing the same session. That is the story with a usual web application. Sorry Lakshi, I have not worked in WAP. I have no idea how session is handled in your WAP server.
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Lakshi Meghoo wrote:
Do you have other suggestions?


Check whether you are accessing displaygues.jsp before you setting the value into the session ?
Lakshi Meghoo
Greenhorn

Joined: Feb 03, 2009
Posts: 5
Thanks for your willingness to help.
I understand my application is not the simple html and jsp. Does anyone familiar with WAP can help me to sort this problem?
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12769
    
    5
It sounds like every request is getting a new session. You could check this by getting the id with the getID() method and logging or printing it.

Failure to recover the same ID might be happening if the client side does not allow cookies. In that case you will have to encode the session ID into all link URLs sent to the client.

Bill
Lakshi Meghoo
Greenhorn

Joined: Feb 03, 2009
Posts: 5
I tried my code in jsp and html format. It works well. I think the problem is about the wap pages. Perhaps some header tags are missing.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Session variable returns null value