E JSP ( Java Servet Pages ) naci� como respuesta al ASP de Microsoft y no nos vamos a detener demasiado en �l porque es pr�cticamente lo mismo s�lo con la diferencia de que lo que hay entre <% %> es c�digo Java.
Debes instalar el JDK1.2 y el JSWDK como ya hemos visto en el Cap�tulo 1.
�Ejemplo
Adem�s de haber instalado JDK y JSP tambi�n necesitamos el JDBC que contiene los m�todos y clases para acceder a las bases de datos.
Primero creamos las clases para Acceder a la base de datos y para insertar registros en la misma. Para nuestro ejemplo son estas:
DataBaseSelect Sirve parea ejecutar un select * from Clientes
import java.sql.*;
import java.util.Vector;
public class DataBaseSelect {
private Vector result;
public DataBaseSelect() {
result = new Vector();
} // constructor DataBaseSelect
public String connect() {
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver").newInstance();
return "Driver Loaded!";
} catch (Exception E) {
return "Unable to load driver.";
}
}
public String select() {
try {
Connection C = DriverManager.getConnection("jdbc:odbc:Ejemplo1");
Statement Stmt = C.createStatement();
ResultSet myResult = Stmt.executeQuery("SELECT * From Clientes");
while (myResult.next()) {
result.addElement(myResult.getString(1));
}
// Clean up
myResult.close();
Stmt.close();
C.close();
return "Connection Success!";
} catch (SQLException E) {
return "SQLException: " + E.getMessage();
}
}
/**
* Accessor for result
**/
public Vector getResult() {
return result;
}
/**
* Mutator for result
**/
public void setResult(Vector avector) {
result = avector;
}
} // class DataBaseSelect
DataBase Insert para insertar un registo
import java.sql.*;
public class DataBaseInsert {
String something;
public DataBaseInsert() {
something = null;
} // constructor DataBaseInsert
public String getSomething() {
return something;
}
public void setSomething(String asomething) {
something = asomething;
}
public String connect() {
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver").newInstance();
return "Driver Loaded!";
} catch (Exception E) {
return "Unable to load driver.";
}
}
public String insertSomething() {
try {
Connection C = DriverManager.getConnection("jdbc:odbc:Ejemplo1");
Statement Stmt = C.createStatement();
String insert = "INSERT INTO Clientes (Dni,Nombre,Apellido1,Apellido2)
VALUES ('"44279736","Alejandro","Garcia","Machado"')";
int stmtInt = Stmt.executeUpdate(insert);
Stmt.close();
C.close();
return "Inserted row " + stmtInt;
} catch (SQLException E) {
return "SQLException: " + E.getMessage();
} catch (Exception E) {
return "Error " + E.toString();
}
}
} // class DataBaseInsert
Se guardan en .java y se compilan.
Ahora creamos el fichero .jsp que usar� esas clases y nos devolver� el resultado como un script ASP
Aqu� tienes un ejemplo:
insert.jsp
<html>
<head>
<title>Insertando registros en la base de datos </title>
</head>
<body>
<bean name="insert" type="DataBaseInsert" scope="request">
<param something="something already in the bean"></bean>
<% out.print(insert.connect()); %><br>
<% insert.setSomething("a new something to insert"); %><br>
<% out.print(insert.insertSomething()); %>
</body>
</html>
Select.jps
<html>
<head>
<title>Select everything from a database</title>
</head>
<body>
<bean name="select" type="DataBaseSelect" scope="request">
</bean>
<% out.print(select.connect()); %>
<br>
<% out.print(select.select()); %>
<p>Format results
<br>
<%@ import="java.util.Vector" %>
<% Vector aResult = select.getResult(); %>
<table>
<% for (int i=0; i < aResult.size(); i++) { %>
<tr>
<td>
<% out.print(aResult.elementAt(i)); %>
</td>
</tr>
<% } %>
</table>
</body>
</html>
Cuando tengas los .class y los .jsp los debes guardar en un directorio del JSWDK y acceder a ellos con la llamada:
http://127.1.1.1:8080/select.jsp
La manera de hacer formularios y dem�s es bastante f�cil. Adem�s dentro del JSWDK vienen ejemplos muy buenos que te ense�an como hacerlos.
Como ves es bastante parecido al ASP.