Ciao a tutti guys!Sto provando a creare un'applicazione che autentica un utente estraendo dal db user e pwd. Come framework utilizzo Struts 1.3,come DBMS uso MySql e come application Server uso Tomcat Per la connessione sto utilizzando un DataSource. Il problema è che quando inserisco i dati della form e li invio non succede niente di niente e la pagina che visualizzo non è la view configurata sullo struts-config, preposta alla visualizzazione dei dati estratti dal db.Qualcuno può aiutarmi?Vi posto del codice per capire se ho fatto tutto bene:
Prima di tutto Ho scompattato il driver connector-java sotto la cartella lib di tomcat e l'ho importato come libreria aggiuntiva in Eclipse.
Configurazione del file context di Tomcat:
Configurazione del web.xml della web applicationcodice:<Resource name="jdbc/DataBase" auth="Container" type="javax.sql.DataSource" username="root" password="artemska" driverClassName="com.mysql.jdbc.Driver" url="jdbc:mysql://localhost:3306/azienda" maxActive="8" maxIdle="4" maxWait="10000"/>
Actioncodice:<resource-ref> <description>DB Connection</description> <res-ref-name>jdbc/DataBase</res-ref-name> <res-type>javax.sql.DataSource</res-type> <res-auth>Container</res-auth> </resource-ref>
JSPcodice:package org.conn; import javax.servlet.http.*; import javax.sql.DataSource; import java.sql.*; import org.apache.struts.action.*; import javax.naming.*; public class connAction extends Action { public ActionForward execute(ActionMapping mapping, HttpServletRequest req,HttpServletResponse res, ActionForm form) throws Exception{ connForm formBean=(connForm) form; String usr=formBean.getUser(); String pwd=formBean.getPassword(); DataSource ds=null; Connection conn=null; try { Context context = new InitialContext(); ds = (DataSource) context.lookup("java:comp/env/jdbc/DataBase"); conn=ds.getConnection(); Statement stm=conn.createStatement(); ResultSet rs=stm.executeQuery("SELECT * FROM utenti"); while (rs.next()){ String usr1=rs.getString("user"); String pwd1=rs.getString("password"); if ((usr1.equals("usr")) && (pwd1.equals("pwd"))){ req.setAttribute("usr1", usr1); req.setAttribute("pwd1", pwd1); } } } catch(SQLException e){ } return mapping.findForward("success"); } }
Struts-Configcodice:<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>ciao</title> </head> <body> <% String usr1= (String) request.getAttribute("usr1"); %> <%= usr1 %> <% String pwd1= (String) request.getAttribute("pwd1"); %> <%= pwd1 %> </body> </html>
P.S. ho avviato un'istanza di mysql autenticandomi sul db....c'è altro che debba fare?in cosa sbaglio?grazie a tutticodice:<form-beans> <form-bean name="connForm" type="org.conn.connForm"/> </form-beans> <action-mappings> <action path="/login" type="org.conn.connAction" name="connForm" scope="request" validate="false" input="/Home.jsp"/> <forward name="success" path="/Hola.jsp"/> <forward name="failed" path="/none.jsp"/> </action-mappings> <message-resources parameter="MessageResources" /> <message-resources parameter="org.conn.Validazione"/>

Rispondi quotando

