How to show the data of a user logged on java web

0

Hello, I need to show the data of the logged in user. I have the following in the servlet:

protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {

    String user = request.getParameter("username");
    String pass = request.getParameter("password");

    Session Ss = HibernateUtil.getSessionFactory().openSession();

    Query rs = Ss.createQuery("From Trabajador where correo ='" + user + "'and contrasena='" + pass + "'");
    List<Trabajador> list = rs.list();

    for (Trabajador t :list) {

        if (t==t) {

            HttpSession newSession = request.getSession();
            newSession.setAttribute("usersession", user);
            response.sendRedirect("index.jsp");

        }else{

        response.getWriter().write("<script>alert('Usuario o contraseña incorrectos !');window.history.back();</script>");


        }   

    }

It is normal but I need to show the data in a form to be modified, but in doing so I can only show the email and password with which it starts this is the class:

public class Trabajador  implements java.io.Serializable {

 private Integer idTrabajador;
 private Parqueadero parqueadero;
 private String nombres;
 private String apellidos;
 private String tipoDocumento;
 private String doumento;
 private String contrasena;
 private String correo;
 private String estado;

public Trabajador() {
}

public Trabajador(Parqueadero parqueadero, String nombres, String apellidos, String tipoDocumento, String doumento, String contrasena, String correo, String estado) {
   this.parqueadero = parqueadero;
   this.nombres = nombres;
   this.apellidos = apellidos;
   this.tipoDocumento = tipoDocumento;
   this.doumento = doumento;
   this.contrasena = contrasena;
   this.correo = correo;
   this.estado = estado;
}

public Integer getIdTrabajador() {
    return this.idTrabajador;
}

public void setIdTrabajador(Integer idTrabajador) {
    this.idTrabajador = idTrabajador;
}
public Parqueadero getParqueadero() {
    return this.parqueadero;
}

public void setParqueadero(Parqueadero parqueadero) {
    this.parqueadero = parqueadero;
}
public String getNombres() {
    return this.nombres;
}

public void setNombres(String nombres) {
    this.nombres = nombres;
}
public String getApellidos() {
    return this.apellidos;
}

public void setApellidos(String apellidos) {
    this.apellidos = apellidos;
}
public String getTipoDocumento() {
    return this.tipoDocumento;
}

public void setTipoDocumento(String tipoDocumento) {
    this.tipoDocumento = tipoDocumento;
}
public String getDoumento() {
    return this.doumento;
}

public void setDoumento(String doumento) {
    this.doumento = doumento;
}
public String getContrasena() {
    return this.contrasena;
}

public void setContrasena(String contrasena) {
    this.contrasena = contrasena;
}
public String getCorreo() {
    return this.correo;
}

public void setCorreo(String correo) {
    this.correo = correo;
}
public String getEstado() {
    return this.estado;
}

public void setEstado(String estado) {
    this.estado = estado;
}
}
    
asked by Marcela CB 15.07.2017 в 05:51
source

2 answers

1

What I see is that you're missing annotations in your Entity class.

You must have something like this:

@Entity
@Table(name = "Trabajador")
public class Trabajador {
    private Integer idTrabajador;
    private Parqueadero parqueadero;
    private String nombres;
    private String apellidos;
    private String tipoDocumento;
    private String doumento;
    private String contrasena;
    private String correo;
    private String estado;
}

Annotations are the way Hibernate can work and map your bean with the resultSet. Another important thing to consider is that the name of your properties matches the name of your properties in the table otherwise you have to add an annotation that helps you do the mapping with the correct name of your column in the table. Example:

 @Column(name = "address_line1")
 private String addressLine1;

Where the column in your table is "address_line1" but in your bean it is called addressLine1

Another thing like your user variable that saves it in session only brings the mail parameter to login. you should save the object that returned your query to you in session.

newSession.setAttribute("usersession", t);

And when you are in your JSP (assuming you are occupying JSP to render in the browser) you do a casting of the object of the Worker type

Trabajador userData = (Trabajador) session.getAttribute("usersession");
    
answered by 15.07.2017 в 15:14
0

As I understand your query, worker would be the user, therefore in that object 't' you must have the user's data.

In the same way you send the error message you can send the html to generate the form with the data of the worker.

    
answered by 15.07.2017 в 09:28