0

after a hole bunch of suffering with mySQL and tomcat i managed to make them all work at the same time, now its time for me to implement servlets to connect everything with java and do marvelous things with my database and queries except that after another hole bunch i havent manage to get a good outcome with it.

The interface is a bit crude and the variable names got messy in the process, everything seems fine but when i try to actually use this i only get a blank page and i cannot make any sense of it, here is my code:

the index in html:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>
<head>
<title>Escuela</title>
</head>

<body>
<div id="section">
<h1>Busqueda de clientes</h1>
<p><form action="./ConexionAlumnos" method="post">
        nombre: <input type="text" id="nombre" name="nombre" />
        <input type="submit" />
</form></p>
</div>
</body>
</html>

here is the web.xml:

<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
                  http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" 
version="3.0"
metadata-complete="true">

<display-name>
  Aplicacion  para escuela
</display-name>

<description>
Trae los alugnos de la base
</description>

<session-config>
<session-timeout>
    30
</session-timeout>
</session-config>

<servlet>

    <servlet-name>
         ConexionAlumnos
    </servlet-name>

    <servlet-class>
         ConexionAlumnos
    </servlet-class>

</servlet>

<servlet-mapping>

    <servlet-name>
            ConexionAlumnos
    </servlet-name>

    <url-pattern>
        /ConexionAlumnos
    </url-pattern>

</servlet-mapping>
</web-app>

my .JSP:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>

<html>
<head>
<title>Escuela</title>
</head>
<body>
<h1>Busqueda de clientes</h1>
<table border="1">
<tr>
        <th>ID Cliente</th>
        <th>Nombre</th>
        <th>Direccion</th>
        <th>tamanio</th>
        <th>Id Ruta de Reparto</th>
        <th>Descuento</th>
</tr>
 <c:forEach items="${requestScope.clientes}" var="al">
  <tr>
        <td>
    <c:out value="${al.idCliente}" />
    <br />
        </td>
        <td>
    <c:out value="${al.nombre}" />
    <br />
        </td>
        <td>
    <c:out value="${al.direccion}" />
    <br />
        </td>
        <td>
    <c:out value="${al.tamanio}" />
    <br />
        </td>
        <td>
    <c:out value="${al.id_reparto}" />
    <br />
        </td>
        <td>
    <c:out value="${al.descuento}" />
    <br />
        </td>           
    </tr>
 </c:forEach>
</table>
<br />
<a href="./index.html">Regresar</a>
</body>
</html>

and lastly my serlvet .java:

import java.io.*;
import java.sql.*;
import java.util.Vector;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.swing.JOptionPane;

public class ConexionAlumnos  extends HttpServlet{
@Override
public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
    doPost(request, response);

}

@Override
public void doPost(HttpServletRequest request, HttpServletResponse response){
    // TODO Auto-generated method stub
    String base = "polanco";
    String usuario = "usuario";
    String password = "admin";
    Connection con = null;
    Statement stat = null;
    ResultSet res = null;

    try {
        Class.forName("com.mysql.jdbc.Driver");

    String url = "jdbc:mysql://localhost:8080/"+base;

    System.out.println(url);

        con = DriverManager.getConnection(url,usuario,password);




        stat = con.createStatement();

    String sql = "SELECT * FROM clientes;";

    String nombre = JOptionPane.showInputDialog("dame un nombre para buscar");
    if(!nombre.equals("")){
        sql = "SELECT * FROM clientes WHERE nombre=\"" + nombre + "\";";
    }



        res = stat.executeQuery(sql);

    Vector<Clientes> clientes = new Vector<Clientes>();


        while(res.next()){

            Clientes aux = new Clientes();

            aux.setIdClientes(res.getInt("idClientes"));
            aux.setNombre(res.getString("nombre"));
            aux.setDireccion(res.getString("direccion"));
            aux.setTamanio(res.getString("tamanio"));
            aux.setDireccion(res.getString("direccion"));
            aux.setId_repartos(res.getInt("id_repartos"));
            aux.setDescuento(res.getDouble("descuento"));

            clientes.add(aux);

        }



        stat.close();


        con.close();
        request.setAttribute("clientes",clientes);
        RequestDispatcher disp =  getServletContext().getRequestDispatcher("/procesa.jsp");


        if(disp!=null){
            disp.forward(request,response);
        }

    } catch (Exception e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }

}
public void Get(HttpServletRequest request, HttpServletResponse response){
    // TODO Auto-generated method stub
    String base = "polanco";
    String usuario = "usuario";
    String password = "admin";
    Connection con = null;
    Statement stat = null;
    ResultSet res = null;

    try {
        Class.forName("com.mysql.jdbc.Driver");

    String url = "jdbc:mysql://localhost:8080/"+base;

    System.out.println(url);

        con = DriverManager.getConnection(url,usuario,password);




        stat = con.createStatement();

    String sql = "SELECT * FROM clientes;";

    String nombre = JOptionPane.showInputDialog("dame un nombre para buscar");
    if(!nombre.equals("")){
        sql = "SELECT * FROM clientes WHERE nombre=\"" + nombre + "\";";
    }



        res = stat.executeQuery(sql);

    Vector<Clientes> clientes = new Vector<Clientes>();


        while(res.next()){

            Clientes aux = new Clientes();

            aux.setIdClientes(res.getInt("idClientes"));
            aux.setNombre(res.getString("nombre"));
            aux.setDireccion(res.getString("direccion"));
            aux.setTamanio(res.getString("tamanio"));
            aux.setDireccion(res.getString("direccion"));
            aux.setId_repartos(res.getInt("id_repartos"));
            aux.setDescuento(res.getDouble("descuento"));

            clientes.add(aux);

        }



        stat.close();


        con.close();
        request.setAttribute("clientes",clientes);
        RequestDispatcher disp =  getServletContext().getRequestDispatcher("/procesa.jsp");


        if(disp!=null){
            disp.forward(request,response);
        }

    } catch (Exception e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }

}

}

the needed classes are made, the connection with the database has the right credentials and when i run it all seems to work just fine until i send the "form" from the webpage and all i get in return is a blank page with no html or error messages whatsoever.

Api.Jc
  • 1

0 Answers0