session problem

[nobr]hi frnz

im getting session's value null whereas i've used setAttribute() ,i've tried very much but im unable to correct it.pls help me ,my whole project is not working bcoz of it.

code is:

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

<%

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

Connection con=DriverManager.getConnection("jdbc:odbc:al");

Statement st=con.createStatement();

String s1=request.getParameter("t1");//out.println(s1);

String s2=request.getParameter("t2");//out.println(s2);

String s3=null;

PreparedStatement ps=con.prepareStatement("select password from user_detail where username=?");

ps.setString(1,s1);

ResultSet rs=ps.executeQuery();

while( rs.next())

{

s3=rs.getString("password");

}

out.println("session");

session.setAttribute("user33","gaurav");

out.println(request.getAttribute("user33"));

%>
<%

rs.close();

ps.close();

st.close();

con.close();

out.println(s3);

if(s3.equals(s2))

{out.println("right password");}

else

{ out.println("wrong password");}

%>

output:

session null

l right password

[/nobr]

[1266 byte] By [gaurav_yadava] at [2007-11-27 3:55:26]
# 1

request != session.

Use session.setAttribute() and session.getAttribute() to set/get attributes in the session scope.

Use request.setAttribute() and request.getAttribute() to set/get attributes in the request scope.

By the way: storing unencrypted passwords in the DB is bad practice. Displaying readable passwords is also completely meaningless, there they are not intented for.

BalusCa at 2007-7-12 8:59:36 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2

[nobr]BalusC thanks for suggestion ,i corrected the code .i actually written request inplace of session there with session,getAttribute().but now when i changed the code tomcat stops & the page is not executed.

if you can find anything from the code that will be of my help then pls tell me. and i really dont know how to send data in encrypted way,if uyou can tell or any hint then pls give.

code::

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

<%

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

Connection con=DriverManager.getConnection("jdbc:odbc:al");

Statement st=con.createStatement();

String s1=request.getParameter("t1");//out.println(s1);

String s2=request.getParameter("t2");//out.println(s2);

String s3=null;

PreparedStatement ps=con.prepareStatement("select password from user_detail where username=?");

ps.setString(1,s1);

ResultSet rs=ps.executeQuery();

while( rs.next())

{

s3=rs.getString("password");

}

//out.println("session");

//session.setAttribute("user33","gaurav");

//out.println(session.getAttribute("user33"));

%><BR /><%

rs.close();

ps.close();

st.close();

con.close();

out.println(s3);

if(s3.equals(s2))

{out.println("right password");

session.setAttribute("user",s1);%><input name="user1" type="hidden" value="<%=session.getAttribute("user")%>" /><jsp:forward page="profile.jsp" /><%

}

else

{ out.println("wrong password");}

%>

output:

Unable to connect

Firefox can't establish a connection to the server at localhost:8080

[/nobr]

gaurav_yadava at 2007-7-12 8:59:36 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 3
you can follow these steps and assign the same name, it will be working, <% String s1=request.getParameter("t1"); session.setAttribute("t1",s1);%>
senthil_yogaa at 2007-7-12 8:59:37 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 4

well this about your first code

you are setting something as a attribute which should be an object but i don' think you have difined anywhere that gaurav is a String or something else......

use this and your code will run fine

String gaurav="gaurav";

session.setAttribute("user33","gaurav");

out.println(request.getAttribute("user33"));

hunterzza at 2007-7-12 8:59:37 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 5
Second one is little funny my friend because this not because of you code. This is some error in you web server.
hunterzza at 2007-7-12 8:59:37 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 6

> well this about your first code

>

> you are setting something as a attribute which should

> be an object but i don' think you have difined

> anywhere that gaurav is a String or something

> else......

> use this and your code will run fine

>

> String gaurav="gaurav";

>

> session.setAttribute("user33","gaurav");

> out.println(request.getAttribute("user33"));

my friend ,in session.setAttribute("user33","gaurav");

in just checking whether session is working or not in this page & nothing else.the problenm is in if( ) part .when i remove code under if ,the page runs fine & even when i forward page to some other page else profile then also it works .but when i use forward to profile with session code tomcat stops abruptly !! pls focus on if part & suggest something.tomcat window shuts down & so i am unable to see error causing it to shut down.

gaurav_yadava at 2007-7-12 8:59:37 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 7
Hi,Check the Profile.jsp. Chances are there the error is with profile.jsp.
Gokul_Rama at 2007-7-12 8:59:37 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 8
You are setting attribute in session but retrieving in request object.session.setAttribute("user33","gaurav");out.println(request.getAttribute("user33"));it should be :session.setAttribute("user33","gaurav");out.println(session.getAttribute("user33"));
Rajesh3677a at 2007-7-12 8:59:37 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 9

> You are setting attribute in session but retrieving

> in request object.

>

> session.setAttribute("user33","gaurav");

> out.println(request.getAttribute("user33"));

>

> it should be :

> session.setAttribute("user33","gaurav");

> out.println(session.getAttribute("user33"));

i've corrected this error !!

gaurav_yadava at 2007-7-12 8:59:37 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 10
no suggestion...strange !! guys help me ...
gaurav_yadava at 2007-7-12 8:59:37 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...