[ Foro de Java ]

Enlazar Jsp con Java

16-May-2018 13:42
Alvaro S. Moreno
0 Respuestas

Hola buenas, ando un poco perdido en esto, ya que soy novato.
Me gustaría poder acceder a una clase java desde un formulario Jsp.
Y no veo manera de hacerlo. Solo veo ejemplos con Servlet, pero creo que no es necesario.
el codigo del jsp, seria el siguiente:
<%@page import="java.sql.*, javax.swing.*" language="java"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<link type="text/css" rel="stylesheet" href="style.css">

<title>index.html</title>
</head>
<body>
<p style="text-align: center;">
<label for="ltickets" class="first-name"><b><font
size="16">Tickets de trabajo</font></b></label>
</p>
<br>
<form name="form" action="ConexionBBDD" method="get">
<table class="table1" width="100%" border="1">
<tbody>
<tr>
<td style="text-align: right;" class="table1"><label
for="lusuario">Usuario:</label></td>
<td class="table1"><input name="usuario" class="cajatexto"
type="text"><br></td>
</tr>
<tr>
<td style="width: 545.67px; text-align: right;" class="table1"><label
for="lpass">Contrase&ntildea:</label></td>
<td style="width: 536.333px;" class="table1"><input
name="password" maxlength="10" class="cajatexto" type="password"><br>
</td>
</tr>
<tr>
<td style="text-align: right;" class="table1"><br>
<button name="aceptar" type="submit">Aceptar</button>&nbsp;&nbsp;
<br></td>
<td class="table1"><br>
<button name="cancelar" type="reset">Cancelar</button> <br></td>
</tr>
</tbody>
</table>
</form>
<%String usuario = request.getParameter( "usuario" );
String pass=request.getParameter("password");
%>
<p style="text-align: center;">
<br>
</p>
</body>
</html>
y la clase sería la siguiente:
package partes;
import java.sql.*;

public class GestionBBDD {
public GestionBBDD(String usuario, String password) {
String dbUsuario, dbPass;
boolean login= false;
// String pass=request.getParameter("password");
String sql="select * from Usuarios where NombreUsuarios=? and Contrasena=?";


try {
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException ex) {
System.out.println("Error al registrar el driver de MySQL: " + ex);
}
Connection conexion=null;
PreparedStatement pst = null;
ResultSet rs = null;
conexion = DriverManager.getConnection("jdbc:mysql://localhost:3306/ticketsdetrabajo", "root", "Contrasena");
//creamos objeto Statement

pst=conexion.prepareStatement(sql);
pst.setString(1, usuario);

rs=pst.executeQuery();
if(rs.next())
{
dbUsuario=rs.getString("NombreUsuarios");
dbPass=rs.getString("Contrasena");
if(dbUsuario.equals(usuario)&& dbPass.equals(password)) {
System.out.print("Ok");
login=true;
}else {
System.out.print("El usuario o Contraseña introducidos son erroneos");
}


System.out.println(rs.getObject("NombreUsuarios")+" "+ rs.getObject("Contrasena")+ " "+rs.getObject("Nempleado")+" "+rs.getObject("Jefe"));
}


}catch (java.sql.SQLException sqle){
System.out.println("Error: " + sqle);
}
}
}




(No se puede continuar esta discusión porque tiene más de dos meses de antigüedad. Si tienes dudas parecidas, abre un nuevo hilo.)