Ver Mensaje Individual
  #1 (permalink)  
Antiguo 14/07/2011, 10:13
kyubi22
 
Fecha de Ingreso: mayo-2010
Mensajes: 33
Antigüedad: 14 años
Puntos: 3
Exclamación Como obtener lo datos de una sesion hecha con servlets a traves de un jsp

AYUDA POR FAVOR TENGO UN PROBLEMA CON LAS SESIONES AL MOMENTO DE QUERER RECUPERAR EL NOMBRE DE USUARIO DE LA SESION, LA SESION LA GENERO EN UN SERVLET Y EN DONDE TENGO EL PROBLEMA ES EN EL JSP, AL MOMENTO DE QUERER RECUPERARLA EN UN JSP ME MARCA NULL EN EL CAMPO DONDE DEBE DE IR EL NOMBRE DEL USUARIO, POR FAVOR ME PRODRIAN DECIR COMO PUEDO RECUPERAR EN UN JSP EL NOMBRE DE USUARIO

Aqui esta el codigo del servlet:


package CentroQuirurgicoController;

import CentroQuirurgicoDATA.Conexion;
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;

/**
*
* @author Administrador
*/
public class LoginServlet 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("txtUsuario");
String clave = request.getParameter("txtClave");

Connection cn = new Conexion().getConnection();
String query = "select * from usuario where nombre_usuario=?";
PreparedStatement pstmt = cn.prepareStatement(query);
pstmt.setString(1, usuario);
ResultSet rs = pstmt.executeQuery();

if ( rs.next() && clave.equals(rs.getString("Clave"))) {

HttpSession sesion = request.getSession();
sesion.setAttribute("NombreUsuario", getInitParameter("NombreUsuario"));
RequestDispatcher rd = getServletContext().getRequestDispatcher("/index.jsp");
rd.forward(request, response);
} else {
request.setAttribute("validar", "1");
RequestDispatcher rd = getServletContext().getRequestDispatcher("/Login.jsp");
rd.forward(request, response);
}

pstmt.close();
cn.close();
System.out.println("Ok");

}
catch(Exception e){e.printStackTrace();}

finally {
out.close();
}
}}



Este es el jsp donde debe aparecer el nombre del usuario que h iniciado sesion:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>MVC con Java - ::: Menu ::: -</title>
<style type="text/css">
<!--
.Estilo2 {font-size: 18px; }
-->
</style>
</head>

<body>
<div align="center">
<p>::: MENU :::</p>
<table width="240" border="0">
<tr>
<td><a href="vistas/web_nuevo_empleado.jsp"><img src="vistas/iconos/icono-formulario.png"/></a></td>
<td><div class="Estilo2">
<a href="vistas/web_agragar_empleado.html">SOLICITUD SALA DE OPERACIONES</a>
</div></td>
</tr>
<tr>
<td><a href="vistas/web_modificar_empleado.jsp"><img src="vistas/iconos/reportes.png"/></a></td>
<td><div class="Estilo2">
<a href="vistas/web_modificar_empleado.jsp">REPORTES</a>
</div></td>
</tr>
<tr>
<td><div class="Estilo2"><a href="Mantenimiento.jsp"><img src="vistas/iconos/database.png"/></a></div></td>
<td><div class="Estilo2"><a href="Mantenimiento.jsp">MANTENIMIENTO DE LA BASE DE DATOS</a></div></td>
</tr>
<tr>
<td><div class="Estilo2"><a href="vistas/web_consultar_empleado.jsp"><img src="vistas/iconos/salir.jpg"/></a></div></td>
<td><div class="Estilo2"><a href="vistas/web_consultar_empleado.jsp">SALIR</a></div></td>
</tr>

</table>



<p>&nbsp;</p>
</div>
</body>

</html>