2017-11-05 5 views
0

ich bin ziemlich neu in der Programmierung, so ist es ein bisschen schwer für mich xx, aber ich versuche, eine Anmeldung (JSP) mit einer gespeicherten Prozedur auf Java (Servlets) auszuführen, leider das Servlet Seite gibt mir nur eine leere Seite und leitet mich nicht auf die Startseite homeAlumno.jsp (JSP mit nur einer Hallo Welt im Moment), ich benutze Netbeans IDE 8.2 (mit Tomcat Server) und die DB ist Oracle 11g Express-Edition mit ein paar Tabellen, jede Hilfe oder Anregungen werden geschätzt.Blank Servlets mit Stored Procedures

pd: erster Beitrag, sorry, wenn ich etwas falsch schreiben: 'D

und heere ist der Code

login.jsp

<%@page contentType="text/html" pageEncoding="UTF-8"%> 
<!DOCTYPE html> 
<html> 
    <head> 
     <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
     <title>Login CEM</title> 
    </head> 
    <body style="text-align: center"> 
     <h1>Menú CEM</h1> 
     <form method="POST" action="serv_login"> 
      <input type="text" name="usuario" placeholder="Nombre"/> 
      <input type="password" name="pass" placeholder="Password" /> 
      <input type="submit" value="Login"/> 
     </form> 
    </body> 
</html> 

Klasse Conexion (Verbindung)

package modelo; 
import java.sql.Connection; 
import java.sql.DriverManager; 
import java.sql.SQLException; 

    public class Conexion { 
     private static Connection cnx = null; 
     public static Connection obtener() throws SQLException, ClassNotFoundException { 
     if (cnx == null) { 
     try { 
     Class.forName("oracle.jdbc.OracleDriver"); 
     cnx = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe", "system", "123"); 
    } catch (SQLException ex) { 
     throw new SQLException(ex); 
    } catch (ClassNotFoundException ex) { 
     throw new ClassCastException(ex.getMessage()); 
    } 
    } 
    return cnx; 
} 
    public static void cerrar() throws SQLException { 
      if (cnx != null) { 
      cnx.close(); 
    } 
    } 
} 

serv_login.java (Servlet)

package controlador; 

import java.io.IOException; 
import java.sql.CallableStatement; 
import java.sql.Connection; 
import java.sql.ResultSet; 
import java.sql.SQLException; 
import java.util.logging.Level; 
import java.util.logging.Logger; 
import javax.servlet.RequestDispatcher; 
import javax.servlet.ServletException; 
import javax.servlet.annotation.WebServlet; 
import javax.servlet.http.HttpServlet; 
import javax.servlet.http.HttpServletRequest; 
import javax.servlet.http.HttpServletResponse; 
import modelo.Conexion; 

/** 
* 
* @author asd 
*/ 
@WebServlet(name = "serv_login", urlPatterns = {"/serv_login"}) 
public class serv_login extends HttpServlet { 

/** 
* Processes requests for both HTTP <code>GET</code> and <code>POST</code> 
* methods. 
* 
* @param request servlet request 
* @param response servlet response 
* @throws ServletException if a servlet-specific error occurs 
* @throws IOException if an I/O error occurs 
*/ 
    protected void processRequest(HttpServletRequest request, HttpServletResponse response) 
     throws ServletException, IOException { 
    response.setContentType("text/html;charset=UTF-8"); 

    try { 

     String usuario = request.getParameter("usuario"); 
     String pass = request.getParameter("pass"); 



     CallableStatement stmt = Conexion.obtener().prepareCall("{call LoginJava(?,?)}"); 

     stmt.setString(1, usuario); 
     stmt.setString(2, pass); 


     ResultSet rs = stmt.executeQuery(); 
     RequestDispatcher rd; 

     rd = request.getRequestDispatcher("login.jsp"); 

     while(rs.next()) { 

      if(rs.getString("NOMBRE_ALUMN").equals(request.getParameter("usuario")) && rs.getString("PASS_ALUMN").equals(request.getParameter("pass"))){            
        request.getRequestDispatcher("homeAlumno.jsp"); 
        rd.forward(request, response); 
       }else{ 
        request.getRequestDispatcher("login.jsp"); 
        rd.forward(request, response); 
       } 

      } 

    } catch (SQLException ex) { 
      ex.getMessage(); 

    } catch (ClassNotFoundException ex) { 
     Logger.getLogger(serv_login.class.getName()).log(Level.SEVERE, null, ex); 
     ex.getMessage(); 
    } 

} 

// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code."> 
/** 
* Handles the HTTP <code>GET</code> method. 
* 
* @param request servlet request 
* @param response servlet response 
* @throws ServletException if a servlet-specific error occurs 
* @throws IOException if an I/O error occurs 
*/ 
@Override 
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 
* @throws ServletException if a servlet-specific error occurs 
* @throws IOException if an I/O error occurs 
*/ 
@Override 
protected void doPost(HttpServletRequest request, HttpServletResponse response) 
     throws ServletException, IOException { 
    processRequest(request, response); 
} 

/** 
* Returns a short description of the servlet. 
* 
* @return a String containing servlet description 
*/ 
@Override 
public String getServletInfo() { 
    return "Short description"; 
}// </editor-fold> 

} 

Stored Procedure (Oracle DB)

create or replace procedure LoginJava(usuario ALUMNO.USU_ALUMN%type, 
contraseña ALUMNO.PASS_ALUMN%type)        
as 
    v_usuario ALUMNO.USU_ALUMN%type; 
    v_contraseña ALUMNO.PASS_ALUMN%type; 
begin 
    select USU_ALUMN, PASS_ALUMN into v_usuario, v_contraseña from ALUMNO 
    where USU_ALUMN = usuario and PASS_ALUMN = contraseña;   
    return; 
end; 

Dank!

Antwort

0

Hier ist was Sie tun:

rd = request.getRequestDispatcher("login.jsp"); 

... some code ... 

request.getRequestDispatcher("homeAlumno.jsp"); 
rd.forward(request, response); 

Die Linie mit request.getRequestDispatcher("homeAlumno.jsp") schafft nur ein brandneues RequestDispatcher, die dann weggeworfen wird (wie es nicht auf eine Variable gespeichert wird). Und in der nächsten Zeile gehst du einfach auf login.jsp.

Versuchen Sie Folgendes zu tun:

request.getRequestDispatcher("homeAlumno.jsp").forward(request, response); 

Eine andere Sache: in der Schleife theoretisch mehrere nach vorne machen können (wenn Ihr Verfahren mehr als einen Datensatz zurückgibt). Ich würde eine Pause hinzufügen (oder ersetzen while durch if) nur für den Fall.

Und noch etwas: Ich bin kein großer Spezialist in Oracle gespeicherten Prozeduren Syntax, aber es scheint, dass Sie keine Ergebnismenge von Ihrer Prozedur zurückgeben, so dass Ihre Schleife Körper überhaupt nicht ausgeführt wird Einmal).

Probieren Sie etwas wie dies zu tun:

select count(*) CNT from ALUMNO 
where USU_ALUMN = usuario and PASS_ALUMN = contraseña; 

Und dann (anstelle Ihrer while Schleife):

rs.next(); 
if (rs.getInt("CNT") > 0) { 
    // .. there is such a user, do the forward 
} else { 
    // no such user, or password mismatch 
} 

Ein paar Empfehlungen, wenn Sie nichts dagegen haben.

Zuerst sieht es aus wie Sie planen, Ihre Passwörter im Klartext in der Datenbank zu speichern. Das ist eine schlechte Idee: Verwenden Sie einen Hash mit einem Salz, zum Beispiel so etwas wie BCrypt. Hier gibt es mehr Informationen: https://nakedsecurity.sophos.com/2013/11/20/serious-security-how-to-store-your-users-passwords-safely/

Auch Sie werden hier das Rad mit Passwort-Authentifizierung neu zu erfinden. Wenn dies nicht nur ein Studentenprojekt ist, das in ein paar Tagen weggeworfen wird, würde ich Ihnen empfehlen, Spring Security (Standalone oder Spring Boot) oder ein ähnliches Sicherheitsframework zu betrachten. Das erleichtert die Authentifizierung erheblich.