unexplainable Error with Session...SIMPLE, but unexplainable..!!

[nobr]I can't understand why Session maintaining between my Servlet and another Jsp page worked when I tried everything with Tomcat 5.5.

..until I decided to restart Tomcat, and it worked no more..!!

The problem, now, is that Jsp page can't recognize user's ID passed with Session Object ( It says "Utente:null" in top of the page)

SERVLET's CLASS

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();

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

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

session.putValue(ID,ID);

session.putValue(pass,pass);

rs2.close();

con.close();

res.sendRedirect("Inserimento.jsp");

}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){}

}

}

JSP PAGE

<%@ page contentType="text/html; charset=iso-8859-1" language="java" import="java.sql.*" errorPage="" %>

<!DOCTYPE html PUBLIC"-//W3C//DTD XHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<link rel="stylesheet" href="emx_nav_left.css" type="text/css">

<style type="text/css">

<!--

.Stile6{font-style: italic; color: #FF0000; font-size: 12px;}

.Stile17{

color: #0033FF;

font-weight: bold;

}

.Stile15{

color: #FF0000;

font-weight: bold;

}

.Stile18{color: #FFFFFF; font-weight: bold;}

.Stile19{color: #007FAA}

.Stile21{

color: #007FAA;

font-weight: bold;

font-style: italic;

}

.Stile22{color: #FF0000}

.Stile23{

color: #FFFFFF;

font-style: italic;

font-weight: bold;

}

.Stile25{font-size: 130%}

.Stile34{color: #FFFFFF}

.Stile35{font-size: 24px}

.Stile36{color: #000000}

-->

</style>

<head>

<title>Administrator Page</title>

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />

</head>

<body>

<%

HttpSession ses=request.getSession();

if (ses.getId()!=null){

out.print("Utente: ");

out.println(ses.getValue("ID"));

%>

<div align="right"><form action="Redirect" method="get">

<br>

<input name="submit5" type=submit class="Stile21" value="INSERIMENTO nuovi UTENTI" />

<input type=hidden name="session" value="<%= request.getParameter("session") %>" />

</form></div>

<td align="right">?lt;/td>

<tr>

<td>

<td align="right"><br>

</tr><tr>

<tr>

<form action="InsertDelOrder" method="get">

<tr>

<td width=286> <h2 align="center" class="Stile15">?</h2>

<div align="center">

<table width="769" border="1" bordercolor="#999999" bgcolor="#007FAA">

<tr>

<td width="286" height="28" bordercolor="#FFFFFF" bgcolor="#007FAA" class="Stile18 Stile34" border="1"><span class="Stile35"><font color="#FFFF00">Inserimento Ordine </font></span> </td>

</tr>

<tr> </tr>

</table>

</div>

<td align="right"></td>

</tr>

<td>

<div align="center">

<table width="769" border="1" bordercolor="#FFFFFF" bgcolor="#007FAA">

<tr>

<td width="315" class="Stile18" >ID ordine : </td>

<td width="438" align="right"><input type=text name="idordine" /></td>

</tr><tr>

<td width="315" class="Stile18" >Data ordine : </td>

<td width="438" align="right"><input type=text name="data" /></td>

</tr>

<tr>

<td class="Stile18">Data consegna pattuita : </td>

<td width="438" align="right"><input type=text name="consegna" /></td>

</tr>

</table>

<table width="769" border="1" bordercolor="#FFFFFF" bgcolor="#007FAA">

<tr>

<td width="315" height="28" class="Stile18"> Percentuale: </td>

<td width="438" align="right">

<input type=text name="percentuale" />

</td>

</tr>

<tr>

<td width="315" height="28" class="Stile18"> Sconto: </td>

<td width="438" align="right">

<input name="sconto" type=text />

</td>

</tr>

<tr> </tr>

</table>

<table width="769" height="87" border="1" bordercolor="#FFFFFF" bgcolor="#007FAA">

<tr>

<td width="315" class="Stile18">Nome dipendente (presente nel DB): </td>

<td width="438" align="right"><input type=text name="nome" /></td>

</tr>

<tr>

<td class="Stile18">Cognome dipendente (presente nel DB): </td>

<td width="438" align="right"><input type=text name="cognome" /></td>

</tr>

<tr>

<td class="Stile18">Fornitore : </td>

<td width="438" align="right"><input type=text name="fornitore" /></td>

</tr>

</table>

<table width="769" height="63" border="1" bordercolor="#2F0044" bgcolor="#007FAA">

<tr>

<td width="36" class="Stile23">Note</td>

<td width="720"><div align="center">

<input type=text size="120" name="note">

?lt;/div></td>

</tr>

</table>

</div>

<tr>

<td width="191" class="Stile19">?lt;/td>

<td width="189" align="right" class="Stile19">? </td>

<tr>

<td width="191" class="Stile19">?lt;/td>

<td width="189" align="right" class="Stile19">? </td>

</tr><tr>

<td width="191" class="Stile19">?lt;/td>

<td width="189" align="right" class="Stile19">? </td>

</tr><tr>

<td width="191" height="28" class="Stile19">?lt;/td>

<td width="189" align="right" class="Stile19">? </td>

</tr><tr>

<td width="191" height="28" class="Stile19">?lt;/td>

<td width="189" align="right" class="Stile19">? </td>

</table>

<br>

<br>

</tr><tr></tr><tr>

</tr><tr>

</tr><tr>

</tr><tr>

<td>?lt;/td>

<td class="Stile21">

<input type=hidden name="session" value="<%= request.getParameter("session") %>" />

<input name="submit1" type=submit class="Stile21" value="Inserisci Ordine"submit>

</td>

</tr>

<tr><br>

<br>

</form>

</tr>

<tr><br>

</tr>

<tr><br>

</tr>

<tr>

<td> <h2 class="Stile25"> Cancellazione ordine</h2>

<td align="right">

</tr><tr>

<tr>

<form action="InsertDelOrder" method="post">

<td><input name="submit22" type=submit class="Stile21" value="Cancella ordine" /></td>

<tr>

<td width="191" class="Stile17"> ID Ordine </td>

<td width="189" align="right"> <input type=text name="IDord"> </td>

</tr><tr>

<td></td>

<td align="right"><input type=hidden name="session" value="<%= request.getParameter("session") %>"></td>

</tr>

</form>

<br>

<hr>

</tr>

<div align="center"><br>

</div>

<h2 align="center" class="Stile25 Stile36"> Interrogazioni DB </h2>

<div align="center"><br>

</div>

<form action="Query.jsp">

<div align="center">

<input name="submit232" type=submit class="Stile17" value="Visualizza pratiche associate all operatore" />

<input type=hidden name="session" value="<%= request.getParameter("session") %>">

<input type=hidden name="my_query" value="SELECT * FROM affidamenti_procuratore WHERE anagrafe_procuratore_ID=32">

?

</div>

</form>

<form action="Query.jsp">

<div align="center">

<input name="submit23" type=submit class="Stile17" value="Visualizza ordini gi?splittati" />

<input type=hidden name="session" value="<%= request.getParameter("session") %>">

<input type=hidden name="my_query" value="SELECT * FROM ordine WHERE flag=1">

</div>

</form>

<div align="center"><br>

</div>

<form action="Query.jsp">

<div align="center">

<input name="submit232" type=submit class="Stile17" value="Visualizza fatture di merce NON ANCORA inviata" />

<input type=hidden name="session" value="<%= request.getParameter("session") %>">

<input type=hidden name="my_query" value="SELECT * FROM fattura WHERE inviato=0">

</div>

</form>

<form action="Query.jsp">

<div align="center">

<input name="submit23" type=submit class="Stile17" value="Visualizza fatture della merce inviata" />

<input type=hidden name="session" value="<%= request.getParameter("session") %>">

<input type=hidden name="my_query" value="SELECT * FROM fattura WHERE inviato=1">

</div>

</form>

<div align="center"><br>

</div>

<form action="Query.jsp">

<div align="center">

<input name="submit23" type=submit class="Stile17" value="Visualizza fatture NON ANCORA pagate" />

<input type=hidden name="session" value="<%= request.getParameter("session") %>">

<input type=hidden name="my_query" value="SELECT * FROM fattura WHERE pagato=0">

</div>

</form>

<form action="Query.jsp">

<div align="center">

<input name="submit232" type=submit class="Stile17" value="Visualizza fatture pagate" />

<input type=hidden name="session" value="<%= request.getParameter("session") %>">

<input type=hidden name="my_query" value="SELECT * FROM fattura WHERE pagato=1">

?

</div>

</form><br>

<form action="Query.jsp">

<div align="center">

<input name="submit23" type=submit class="Stile17" value="Visualizza clienti conosciuti (Nome & Cognome)" />

<input type=hidden name="session" value="<%= request.getParameter("session") %>">

<input type=hidden name="my_query" value="SELECT * FROM dipendente">

</div>

</form>

<form action="Query.jsp">

<div align="center">

<input name="submit232" type=submit class="Stile17" value="Visualizza aziende clienti conosciute" />

<input type=hidden name="session" value="<%= request.getParameter("session") %>">

<input type=hidden name="my_query" value="SELECT * FROM azienda">

?

</div>

</form>

<div align="right">

</div>

<h2 align="right" class="glink">?lt;/h2>

<div align="right">

<div align="left"> </div>

</div>

<h2 align="right" class="glink">?lt;/h2>

<form action="Login" method="post">

<h2 align="right" class="glink"><span class="Stile10 Stile22">Disconnetti?lt;/span>

<input type="hidden" name="session" value="<%= request.getParameter("session") %>" />

<input name="submit2" type="submit" class="Stile21" value="Logout" />

</h2>

</div>

</h2>

<h2>?</h2>

</form>

<%}

else

response.sendRedirect("index.html");

%>

</body>

</html>

[/nobr]

[23380 byte] By [Pentolinoa] at [2007-10-2 18:25:17]
# 1

What is your actual question? It sounds like you had an epiphany but are still somehow confused.

If you restart Tomcat, it's internal cache of session id's will be obviously cleared. I wonder if you are confusing JSP's with HTML pages. JSP's are compiled behind the scenes by Tomcat into Servlets. They all reside on the server. So, I do not understand why you would expect a JSP's session to behave differently from your Servlet.

- Saish

Saisha at 2007-7-13 19:46:18 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2

Before looking further in the code,

when using the sendRedirect(String) method, use the encodeSendRedirect(String url) method. That method will ensure that the pointed url will participate in a session, even if the cookies fail. You use this method like this

String url = resp.encodeSendRedirect("path_here/something.jsp");

and then

resp.sendRedirect(url);

Change this and tell me if it works.

Ice

Ice_Penova at 2007-7-13 19:46:18 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 3

My problem is that Servlet don't pass session to JSP, and it is noticeable beacuse it says "Utente:null" (that is, as you can see in the JSP code, a reading of the attribute ID from the Session object)

This is the real problem. All the rest I said was only because it works the first time, and no more..

Please don't mind about what I said about Tomcat, try to understand the error that is making me crazy..

really thank you :)

Pentolinoa at 2007-7-13 19:46:18 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 4

Well, you are using a fairly confusing construct here:

<%= request.getParameter("session") %>

I mean, 'session' is already a reserved word in JSP's. So, personally, that line would confuse me. (It should not cause the problem but indicates to me that you might be going about this in a strange way).

- Saish

Saisha at 2007-7-13 19:46:18 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 5
FOR ICE:I'm sorry but your solution does not works...It tells me "utente:null" like before, and I don't think it's a cookie failure but something else.Thanks :D
Pentolinoa at 2007-7-13 19:46:18 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 6
I can't find the string you're talking about...<%= request.getParameter("session") %>And I forgot to says that I'm a beginner in Java ...so try to understand me if I'm making some mistakes
Pentolinoa at 2007-7-13 19:46:18 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 7

It's OK to ask questions.

But I gotta say that this is not very-well designed code. Honestly, it hurts my eyes to look over it. It is almost impossible something not to go wrong with that code. Here's some tips :

1. Instead of using private instance fields for db connection parameters, use the ServletConfig or ServletContext parameters.

2. use get/set/removeAttribute instead of put/getValue. Those methods are deprecated.

3. Your JSP relies heavily on hidden form fields. Any client with just a right click on your jsp, and View Source, will get a look of your underlying database implementation. And that is not good :).

4. Try not to add many responsibilities to the JSP. JSP are there for presentation, servlets do all the logic. Also, avoid using scriplets in your JSP. Use the Expression Language and JSTL, instead.

Apply those tips, and you'll see how your code will be less error-prone.

Have fun,

Ice

Ice_Penova at 2007-7-13 19:46:18 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...