Foros del Web » Programación para mayores de 30 ;) » Java »

Problema al hacer login con jsp y servlet

Estas en el tema de Problema al hacer login con jsp y servlet en el foro de Java en Foros del Web. Estoy haciendo un proyecto de un sistema de Almacen, he creado un jsp con un formulario para hacer login, y el servlet se encarga de ...
  #1 (permalink)  
Antiguo 04/01/2011, 09:53
 
Fecha de Ingreso: junio-2010
Mensajes: 4
Antigüedad: 13 años, 10 meses
Puntos: 0
Problema al hacer login con jsp y servlet

Estoy haciendo un proyecto de un sistema de Almacen, he creado un jsp con un formulario para hacer login, y el servlet se encarga de realizar el login y mandar a otro jsp que es el menu principal,EL PROBLEMA ESTA EN QUE AL HACER LOGIN NO ME MANDA A JSP MENU SINO QUE SALE UN PAGINA EN BLANCO, Y ESTO SALE EN LA BARRA DE DIRECCION DEL NAVEGADOR:http://localhost:8087/WebApplication1/ValidarAccesoServlet (despues de ingresar el usuario y clave)

Gracias por la ayuda

les dejo el codigo:


CODIGO DEL JSP (LOGIN.JSP)

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!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=utf-8">
<title>JSP Page</title>
<style type="text/css">
body {
background-color: #000;
}
body,td,th {
color: #FFF;
}
</style>
</head>
<body>
<h1><strong><center>
<p>SISTEMA DE ALMACEN</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
</center></strong></h1>
<form action="ValidarAccesoServlet" method="post">
<table width="328" border="1" align="center">
<tr>
<td width="97">Codigo</td>
<td width="215">
<input type="text" name="txtcodusuario" id="txtcodusuario">
</td>
</tr>
<tr>
<td>Password</td>
<td>
<input type="password" name="txtPassword" id="txtPassword">
</td>
</tr>
</table>
<p>&nbsp;</p>
<div align="center">
<input type="submit" name="btnIngresar" id="btnIngresar" value="Ingresar">

<input type="submit" name="btnSalir" id="btnSalir" value="Salir">
</div>
<p>&nbsp;</p>
</form>
</body>
</html>



Y ESTE ES EL CODIGO DEL SERVLET:

package web.almacen.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import soft.lib.conexion.SQLCommander1;

/**
*
* @author Administrador
*/
public class ValidarAccesoServlet 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");
PrintWriter out = response.getWriter();
try {
String Usuario=request.getParameter("txtcodusuario");
String clave=request.getParameter("txtPassword");

Connection cn=SQLCommander1.obtieneConexion();
String query="select * from asociado where codsocio='"+Usuario+"'and clave='"+clave+"'";
PreparedStatement pstmt = cn.prepareStatement(query);
pstmt.setString(1, query);
ResultSet rs= pstmt.executeQuery();

if(rs.next()&& clave.equals(rs.getString("claveempleado"))){
HttpSession sesion = request.getSession();
sesion.setAttribute("idusuario", getInitParameter("idusuario"));
RequestDispatcher rd = getServletContext().getRequestDispatcher("/Menu.jsp");
rd.forward(request, response);
}else{
request.setAttribute("validar", "1");
RequestDispatcher rd= getServletContext().getRequestDispatcher("/Error.jsp");
rd.forward(request, response);
}
pstmt.close();
cn.close();
System.out.println("OK");
}catch(Exception e){e.printStackTrace();}

finally {
out.close();
}
}

// <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>

}
  #2 (permalink)  
Antiguo 04/01/2011, 10:21
Avatar de Xerelo  
Fecha de Ingreso: mayo-2009
Mensajes: 2.175
Antigüedad: 15 años
Puntos: 306
Respuesta: Problema al hacer login con jsp y servlet

¿Has añadido el sevlet al web.xml?
__________________
Cada vez que solucionas los problemas de alguien que no se esfuerza, piensa en que el día de mañana puede llegar a ser tu compañero de trabajo, o peor, tu jefe.
  #3 (permalink)  
Antiguo 04/01/2011, 20:30
Avatar de hwongu  
Fecha de Ingreso: junio-2008
Ubicación: Trujillo
Mensajes: 93
Antigüedad: 15 años, 10 meses
Puntos: 5
Respuesta: Problema al hacer login con jsp y servlet

Y si el lugar de

RequestDispatcher rd = getServletContext().getRequestDispatcher("/Menu.jsp");
rd.forward(request, response);

Lo cambias por

response.sendRedirect(request.getContextPath()+"/Menu.jsp");

Etiquetas: jsp, login, servlet
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 20:56.