Está en la página 1de 9

Instituto Politcnico Nacional

Escuela Superior de Cmputo

Asignatura: Tecnologas para la web Profesor: Zagal Flores Roberto Eswart Alumno: Reyes Torres Jaziel Boleta: 2011630542 Grupo: 2CV9 Practica N 1 : Navegacin entre documentos HTML

I. Investigacin para aprender HTML 4.0:


1. Cul es la etiqueta que necesitamos para generar hipervnculos entre documento HTML? Con la etiqueta <a href="URL"></a> creamos un hipervnculo entre documentos HTML. 2. Cul es la etiqueta que permite colocar ttulos dentro de la etiqueta <body>? Con las etiquetas: <h1></h1> Ttulo muy grande <h2></h2> Ttutlo grande <h3></h3> Ttulo algo as grande de lo normal <h4></h4> Ttulo normal <h5></h5> Ttulo pequeo <h6></h6> Ttulo muy pequeo

II. Reporte
1. En la pgina de login, genere un hipervnculo a una nueva pgina HTML. Esta pgina se llamar registro y contendr un formulario (usuario, email, nombre, apellido y contrasea). 2. Si el registro es exitoso, mandar un mensaje de Registro exitoso, de lo contrario Registro fallido. 3. Modifique el cdigo para que el registro y la autenticacin de usuario utilicen un archivo de texto. Iniciamos con la creacion de un de un archivo HTML llamado registroUsuario.html el cual debe contener un formulario con 5 cajas de texto (uno para cada campo: usuario, email, nombre, apellido y ocntrasea) y un boton para enviar la informacion al servlet ServletRegistro por medio del metodo doPost. RegistroUsuario.html
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> Transitional//EN"

<title>Formulario de registro</title> </head> <body> <form action='registroController' method='post'> <table border="3"> <tr> <td>Usuario</td> <td><input type='text' name='user' /></td> </tr> <tr> <td>Correo</td> <td><input type="text" name='email'></td> </tr> <tr> <td>Nombre</td> <td><input type="text" name='name'></td> </tr> <tr> <td>Apellido</td> <td><input type="text" name='lastName'></td> </tr> <tr> <td>Password</td> <td><input type="password" name='pwd'></td> </tr> <tr> <td></td> <td><input type="submit" value='Enviar'></td> </tr> </table> </form> </body> </html>

Ya creada la pgina registroUsuario.html, tenemos que acceder a ella por medio de un hipervnculo desde la pgina login.html. Esto es posible con la etiqueta <a href="URL"></a> como se aprecia en la imagen y el cdigo.

login.html
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>PRUEBA</title> <body> <h1>Login</h1> <form action='loginController' method='post'> <table border="3"> <tr> <td><input type='text' name='username' /></td> </tr> <tr> <td><input type="password" name='pwd'></td> </tr> <tr> <td><input type="submit" value='Entrar a test'></td> </tr> </table> <a href="registro.html"> Registro </a> <section> <h1>WWF</h1> <p>The World Wide Fund for Nature (WWF) is an international

organization working on issues regarding the conservation, research and restoration of the environment, formerly named the World Wildlife Fund. WWF was founded in 1961.</p> </section> </form> </body> </html>

Ahora creamos el servlet ServletRegistro y declaramos el mtodo autenticarRegisto el cual ser llamado desde el metodo doPost (para tener mayor orden en nuestro codigo). El metodo autenticarRegistro ser el encargado de verificar que los campos de texto del formulario de registro estn llenos; de ser asi mandara el mensaje Registro exitoso y guardara en un archivo los datos que se ingresaron en el formulario, en caso contrario solo mandara el mensaje Error en el registro, intente de nuevo.

ServletRegistro.java
package logicaNegocio; import java.io.File; import java.io.IOException;

import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * Servlet implementation class ServletRegistro */ @WebServlet("/registroController") public class ServletRegistro extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public ServletRegistro() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void autenticarRegistro(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String user= request.getParameter("user"); String email= request.getParameter("email"); String name= request.getParameter("name"); String lastName= request.getParameter("lastName"); String pdw= request.getParameter("pwd"); if( user.length() == 0 || email.length() == 0 || name.length() == 0 || lastName.length() == 0 || pdw.length() == 0 ){ System.out.println("error en registro"); response.getWriter().println("Error en el registro, intente de nuevo."); } else{ File base = new File ("/home/jaziel/Documentos/registro.txt"); File logUsuario = new File ("/home/jaziel/Documentos/autenticar.txt"); PrintWriter archivar = new PrintWriter(base); PrintWriter login = new PrintWriter(logUsuario); archivar.println("Usuario:"+user); login.println(user); archivar.println("e-mail:"+email); archivar.println("Nombre:"+name); archivar.println("Apellido:"+lastName); archivar.println("Apellido:"+pdw); login.println(pdw); archivar.close(); login.close(); System.out.println("Registro exitoso");

response.getWriter().println("Registro exitoso"); } } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub autenticarRegistro(request, response); } }

El servlet ServletLogin al igual que ServletRegistro tiene un mtodo autenticar el cual verifica que el usuario que quiere acceder, se halla registrado previamente (revista el usuario y contrasea de un archivo). Si la autenticacin es correcta aparecer el mensaje Desde post: Hola, 'usuario', de lo contrario se direccionara a la pgina error.html.

ServletLogin.java
package logicaNegocio; import java.io.BufferedReader; import java.io.File; import java.io.FileReader; import java.io.IOException;

import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * Servlet implementation class servletController */ @WebServlet("/loginController") public class ServletLogin extends HttpServlet { private static final long serialVersionUID = 1L; /** * Default constructor. */ public ServletLogin() { // TODO Auto-generated constructor stub System.out.println("Estado---> iniciado"); } protected void autenticar(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { File base = null; FileReader fr = null; BufferedReader br = null; int contador=0; String array[] = new String[10]; String cadena; base = new File ("/home/jaziel/Documentos/autenticar.txt"); fr = new FileReader(base); br = new BufferedReader(fr); String user= request.getParameter("username"); String pwd= request.getParameter("pwd"); System.out.println("Codificacin de los carcteres del cliente: "+request.getCharacterEncoding()); System.out.println("Context path"+request.getContextPath()); while ( ( cadena = br.readLine() ) != null ){ array[contador] = cadena; contador = contador + 1; } if(user.equals(array[0])==true && pwd.equals(array[1])==true){ System.out.println("Es correcto"); response.getWriter().println("Desde post: Hola, "+user); } else{ System.out.println("Saldr error 404"); response.sendRedirect("error.html"); }

} /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { autenticar(request, response); } }

También podría gustarte