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.