Entra

View Full Version : [jsp & servlet] chiamare pagina da servlet.


heijonny
07-10-2004, 21:23
Ciao a tutti,
vi pongo un quesito da perfetto ignorante o quasi in materia:

Sto cercando di sviluppare un' applicazione web con jsp e servlet.
Ho creato una pagina di login con id e password; questi dati vengono inviati ad un servlet che li recupera, poi si collega ad un database, li confronta e decide che se l'utente è admin, deve essere reindirizzato alla sua pagina jsp, mentre se è un qualsiasi altro utente deve essere reindirizzato ad un'altra pagina che sarà poi modellata da un'altro servlet, a seconda dell'utente.
La mia domanda è:
come faccio a dire a questo servlet di indirizzarmi ad una data pagina jsp?

Grazie

Francesco.
:muro: :confused: :muro:

ally
07-10-2004, 21:56
...response.sendRedirect("paginaservlet");
...response.sendRedirect("pagina.jsp");

...attento che il primo funge se hai specificato l'esistenza della servlet nel web.inf ;)

heijonny
07-10-2004, 22:10
Ci ho provato ma non funziona...
Non capisco dove sbaglio, puoi darmi una mano?
Ecco il codice:

/*
* UserServlet.java
*
* Created on 5 ottobre 2004, 23.26
*/

package servlet;

import java.io.*;
import java.net.*;
import java.io.PrintWriter;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.Enumeration;
import java.io.IOException;
import java.sql.*;

/**
*
* @author francesco
* @version 1.0
*/
public class UserServlet extends HttpServlet {

protected Connection con;
protected String userName;
protected String password;
String pass;
String user;
/** Initializes the servlet.
*/
public void init(ServletConfig config) throws ServletException {
super.init(config);



}

/** Destroys the servlet.
*/
public void destroy() {

}

/** Recupera id e password dalla pagina di login
*/
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/plain");
PrintWriter out = response.getWriter();

Enumeration e=request.getParameterNames();
String pwd=(String)e.nextElement();
password=request.getParameter(pwd);
String id=(String)e.nextElement();
userName=request.getParameter(id);

}

/** Trova nel DB id e password corrispondenti
*/
protected void verify(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
try{
String driver="driver.implementation.class";
Class.forName(driver).newInstance();
String url="jdbc:mysql://localhost:3306/dbprova";
con=DriverManager.getConnection(url,"user", "user");
Statement stmt=con.createStatement();
String sql="select username,password from anagrafica where username="+
userName+" and password="+password;
ResultSet rs=stmt.executeQuery(sql);
while (rs.next()){
pass=rs.getString("password");
user=rs.getString("username");
}
}//fine try
catch (Exception e){
throw new UnavailableException(e.getMessage());
}//fine catch

}
protected void redirect(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
if (pass=="admin" && user=="admin"){
System.out.println(password);
response.sendRedirect("protected/homeAdmin.jsp");

}
}//fine redirect


protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);

}

/** Handles the HTTP <code>POST</code> method.
* @param request servlet request
* @param response servlet response
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}

/** Returns a short description of the servlet.
*/
public String getServletInfo() {
return "ITARSKILL - Autenticazione utenti";
}

}


Grazie

Francesco

ally
07-10-2004, 22:39
...bel malloppetto...che errore ti da il web serer?...

...4xx...5xx...:)

heijonny
08-10-2004, 07:48
Il web server non mi da errori, però, il metodo redirect dovrebbe verificare l'utente, e se è admin rimandarlo alla pagina di amministrazione, e invece non è così.
Il metodo redirect non funziona!

Ho alcuni libri a casa sull'argomento, ma non trovo degli esempi chiari.
Magari hai da consigliarmi anche qualche testo dove trovare degli esempi chiari?

Grazie

Ciao

ally
08-10-2004, 10:10
...guarda il metodo funziona senza ombra di dubbio...

...ti allego un file "dispensatore" che ho usato per lagestione di un elenco film...

...prova il response redirect in un programma piu' semplice...

...sei sicuro che l'algoitmo di autenticazione funzioni correttamente? :)

heijonny
09-10-2004, 15:47
Grazie per l'aiuto.
Seguirò i tui consigli, e poi ti faccio sapere.

Francesco

heijonny
10-10-2004, 09:55
Ciao,
ho spezzato il mio servlet, e ne ho creati due in questo modo:
Il primo servlet è richiamato dalla index.jsp che gli passa id e password, memorizza le informazioni in 2 variabili e le passa al secondo servlet con due metodi get. Questo servlet richiama il secondo servlet con response.sendRedirect, ma quando eseguo il tutto con tomcat, ottengo questo errore : HTTP Status 404 - Servlet authenticateServlet is not available.
Ho controllato il file web.xml, e tutti i servlet mi sembrano mappati correttamente.
Ho provato a richiamare il secondo servlet direttamente da index.jsp, e in questo modo funziona, anche se non correttamente.
Non riesco a spiegarmi perchè.

Allego i file java + il file web.xml

Grazie

Francesco