ciao a tutti,![]()
allora ho un problema tra servlet e jsp, quando inserisco un oggetto nella sessione attraverso il servlet non e'poi disponibile via jsp, almeno da quello che ho capito.
Qualcuno sa come mai? o quale potrebbe essere la causa?
vi posto il codice:
index.html
SimpleClient.javacodice:<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Frameset//IT" "http://www.w3.org/TR/html4/frameset.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> <title>VmWare Esxi web client</title> <link rel="stylesheet" type="text/css" href="./stile/defaultStile.css"/> </head> <body lang="it"> <center> <h2>Inserire username e password</h2> </center> <div style="text-align:center"> <form name="Login" action="SimpleClient" method="POST"> Username: <input name="user" type="text" /> Password: <input name="pass" type="password" /> <input id="Submit" type="submit" value="Login" /> </form> </div> </body> </html>
mainpage.jspcodice:import javax.servlet.http.*; import javax.servlet.*; import java.io.IOException; public class SimpleClient extends HttpServlet { /* metodo per rispondere alla post*/ public void doPost (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String user = request.getParameter("user");//prelevo user String pass = request.getParameter("pass");//prelevo pass String jspPage = "";//inizializzo vuota che poi riempiro a seconda della pagina che carico //inizializzo la sessione HttpSession session = request.getSession(); session.setAttribute("user", user); //a seconda dei risultati di login indirizzo alla pagina giusta if(user.equals("pippo")) { jspPage = "mainpage.jsp"; } else { jspPage = "index.jsp"; } request.getRequestDispatcher(jspPage).forward(request,response); } }
codice:<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> <title>VmWare Esxi web client</title> <link rel="stylesheet" type="text/css" href="./stile/defaultStile.css"/> </head> <body> <div style="text-align:center"> Benvenuto <%=session.getAttribute("user")%> </div> </body> </html>

Rispondi quotando

