• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Session variable returns null value

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.


 
Ranch Hand
Posts: 136
Firefox Browser Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try using request.getSession().setAttribute("sess_count", count ); while setting the object into session and request.getSession().getAttribute("sess_count"); while retrieving it back.
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

 
Lakshi Meghoo
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 136
Firefox Browser Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic