problem in session.setAttribute(like the other user!)

Hello,

when JSP is invoked by the servlet it found ID = null

My Servlet has this code ( I tried to let connection and statement open like you said in another post, but nothing)

The worse thing is that, the first time I tried this code, it worked!

publicclass Loginextends HttpServlet{

private Connection con;

private Statement st;

private ResultSet rs,rs2;

publicvoid doGet(HttpServletRequest req,HttpServletResponse res){

try{

Class.forName("com.mysql.jdbc.Driver");

con=DriverManager.getConnection("jdbc:mysql://localhost:3306/gecoprova","root","argoilcane");

st=con.createStatement();

rs=st.executeQuery("SELECT * FROM anagrafe_procuratori WHERE ragsoc='"+req.getParameter("ID")+"'");

if(rs!=null){

if(rs.next())try{

st=con.createStatement();

rs2=st.executeQuery("SELECT * FROM privilegi_procuratore WHERE (userid='"+req.getParameter("ID")+"' AND pass='"+req.getParameter("pass")+"')");

if(rs2!=null){

if(rs2.next())try{

HttpSession session=req.getSession(true);

String ID=req.getParameter("ID");

String pass=req.getParameter("pass");

session.setAttribute(pass,pass);

session.setAttribute(ID,ID);

// rs2.close();

// con.close();

String url = res.encodeRedirectURL("Inserimento.jsp");

res.sendRedirect(url);

}catch (Exception e){e.printStackTrace();}

res.sendRedirect("errorlogin.html");

}

else{

res.sendRedirect("errorlogin.html");

}

}catch (Exception e){e.printStackTrace();}

res.sendRedirect("errorlogin.html");

}

else{

res.sendRedirect("errorlogin.html");

}

res.sendRedirect("errorlogin.html");

}

catch(Exception e){

try{

e.printStackTrace();

res.sendRedirect("errorlogin.html");

}

catch(Exception e1){}

}

}

[4057 byte] By [Pentolinoa] at [2007-10-2 18:27:27]
# 1

> Hello,

>

> when JSP is invoked by the servlet it found ID =

> null

>

> My Servlet has this code ( I tried to let connection

> and statement open like you said in another post, but

> nothing)

> The worse thing is that, the first time I tried this

> code, it worked!

>

> public class Login extends HttpServlet{

> private Connection con;

> private Statement st;

> private ResultSet rs,rs2;

> public void doGet(HttpServletRequest

> t req,HttpServletResponse res) {

> try{

> Class.forName("com.mysql.jdbc.Driver");

> con=DriverManager.getConnection("jdbc:mysql://localh

> ost:3306/gecoprova","root","argoilcane");

> st=con.createStatement();

> rs=st.executeQuery("SELECT * FROM

> OM anagrafe_procuratori WHERE

> ragsoc='"+req.getParameter("ID")+"'");

> if(rs!=null){

> if(rs.next())try {

> st=con.createStatement();

> rs2=st.executeQuery("SELECT * FROM

> FROM privilegi_procuratore WHERE

> (userid='"+req.getParameter("ID")+"' AND

> pass='"+req.getParameter("pass")+"')");

> if(rs2!=null){

> if(rs2.next())try {

>

> HttpSession session=req.getSession(true);

> String ID=req.getParameter("ID");

> String pass=req.getParameter("pass");

>session.setAttribute(pass,pass);

> session.setAttribute(ID,ID);

>

> // rs2.close();

> // con.close();

>

>

> String url =

> ng url = res.encodeRedirectURL("Inserimento.jsp");

> res.sendRedirect(url);

>

>

>

>

>

>

>

> }catch (Exception e){e.printStackTrace();}

>

> res.sendRedirect("errorlogin.html");

> }

> else{

> res.sendRedirect("errorlogin.html");

> }

> }catch (Exception e){e.printStackTrace();}

>

> res.sendRedirect("errorlogin.html");

> }

> else{

> res.sendRedirect("errorlogin.html");

> }

> res.sendRedirect("errorlogin.html");

> }

> catch(Exception e){

> try {

> e.printStackTrace();

> res.sendRedirect("errorlogin.html");

> }

> catch(Exception e1){}

> }

> }

hi ...

i think u retrive the session information in jsp like this ....

session.getAttribute("ID");

session.getAttribute("pass");

if this so .. then u made a mistake in ur servlet code

check wat u write in ur servlet

String ID=req.getParameter("ID");

String pass=req.getParameter("pass");

session.setAttribute(pass,pass);

session.setAttribute(ID,ID);

the first argument of setAttribute method is used as key ... which u set as ID and pass .. but b4 that u set ID and pass to string values that r coming from request...

change the code in servlet to this

String ID=req.getParameter("ID");

String pass=req.getParameter("pass");

session.setAttribute("pass",pass);

session.setAttribute("ID",ID);

i think it will work now

session.getAttribute("ID");

session.getAttribute("ID");

sushruta at 2007-7-13 19:48:38 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2
You're right!!!Thanks, You gave me a big help with this little trouble
Pentolinoa at 2007-7-13 19:48:38 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...