views:

60

answers:

1

Hi, I've written a html form to retrieve password and user name from users:

<form action="prueba-registro" method=get enctype=multipart/form-data>
<h3>
User information
</h3>
User name: <INPUT TYPE=TEXT NAME="realname"><BR>
Password: <INPUT TYPE=PASSWORD NAME="mypassword">
<P><INPUT TYPE=SUBMIT VALUE="Register">
</form>

This information is received in a servlet (checkRegistration) that checks if this information is valid. If everything is ok the servlet "checkRegistration" will call another servlet: uploadFile

In the servlet uploadFile the user is allowed to upload files to the server. In order to store the information in a database I need to know the name of the user.

How could I pass the information about the user name (whichi is available in the servler checkRegistration) to the servlet uploadFile?

Thanks

A: 

The most common way to do this is to save the user information to their session as an attribute. You can access the session from the request:

HttpSession session = request.getSession();

The same session is associated with every request that the user makes. The session has an attribute map that allows you to get and set values. For example, if CheckRegistrationServlet finds that the login is successful, it can do something like this:

request.getSession().setAttribute("LoggedIn", "true");

Then in the UploadFileServlet, check for this attribute before allowing the upload to take place:

if ("true".equals(request.getSession().getAttribute("LoggedIn")) {
    // perform file upload
}
else {
    // deny file upload
}

Of course, instead of saving a "true" value for the "LoggedIn" attribute, it's common to save an object that represents the user's session information. You could build a UserSession object that holds the username, and save that instead of the "true" String.

Kaleb Brasee
It was similar to what I wanted to do, worked perfectly. Thanks
dedalo