I have this Servlet in which I will receive the name and password of the object request
to later validate if it exists in the database and check its full name. But once the type object is created UsuarioDTO
I don't know how to save it in the session to show the property nombreCompleto
in various JSP :
@WebServlet("/One")
public class One extends HttpServlet {
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
/* Se crea el usuario */
UsuarioDTO usuario = new UsuarioDTO();
usuario.setIdUsuario(1);
usuario.setNombre("jperez");
usuario.setNombreCompleto("Juan Pérez López");
//Aquí necesito meter el usuario a la sesión antes de que se
//vaya a la página principal
response.sendRedirect(request.getContextPath() + "/principal.jsp");
}
}
This is the DTO :
public class UsuarioDTO {
private long idUsuario;
private String nombre;
private String nombreCompleto;
public long getIdUsuario() {
return idUsuario;
}
public void setIdUsuario(long idUsuario) {
this.idUsuario = idUsuario;
}
public String getNombre() {
return nombre;
}
public void setNombre(String nombre) {
this.nombre = nombre;
}
public String getNombreCompleto() {
return nombreCompleto;
}
public void setNombreCompleto(String nombreCompleto) {
this.nombreCompleto = nombreCompleto;
}
}
➊ First you retrieve the object
HttpSession
:On interface
HttpSession
, Arun Gupta 1 says:➋ Then assign your object with a name as an attribute in the session. That is, for example:
In your code:
➌ And in your JSP (
principal.jsp
) you can use Expression Language (EL) to display the data. This is, for example:✱ Addendum
Because you use the method
response.sendRedirect
, the browser has to make another request to the server to display the corresponding JSP (the URL in the browser changes), so you need to have the object in the session, i.e. in the objectsession
.However, if you were to use the method
request.getRequestDispatcher
, you would only need to have the object in the current request, that is, in the objectrequest
. And the browser would not need to make another request (in this case, the browser URL remains the same).Grades
A session variable must be created as follows:
You can set the attributes you need and to get them you use the get attribute:
In your JSP it would look like this:
For further reference you can use the following tutorial:
servlet-session
You must create an object of type session inside your java class:
Then you must add the attribute you want to store, assigning it an identifier and the value, in your case:
This way you have already stored a value in a session.
In order to retrieve the stored value you must use the Get method, example:
In this way you can now transfer or retrieve it directly from your JSP.
Cheers
I have to discourage the answers out there that are based on scriptlets (the
<% %>
and the<%= %>
), which are considered deprecated. If you're working on a project with scriptlets keep it that way, but new projects should use Expression Language (EL) or at least JSTL.To put an object in a
session
, I agree with the other questions, although I recommend putting the entire DTO.To use the bean via EL, simply do
in the JSP. This will look for the bean
usuario
in the various scopes (page, request, session, and application) and when it finds it, it will call thegetNombreCompleto()
.It can also be used inside JSTL tags:
If your platform does not yet support EL, you can use the JSP tags
useBean
andgetAttribute
By the way, if the bean's data is related to the request and not to the session, the most normal thing is to save the bean as an attribute of
request
(request.setAttribute()
) and do a forward instead of a redirect 1 ; That means that data is not shared with other simultaneous requests. In this case, the JSP is not altered.1 Redirect indicates to the client (browser) that it has to be redirected to the new page, creating a new request. Forward continues processing the same request at the URL passed to it, the browser does nothing.