William Pfaffe William Pfaffe - 1 month ago 11x
Java Question

Getting session sub-attributes from object

I currently have a User object:

public class User {

public final String username, password, email;
public final double balance;

public User(String username, String password, String email, double balance) {
this.username = username;
this.password = password;
this.email = email;
this.balance = balance;

And a login function, that returns a User object:

public User checkLogin(String usrname, String pssword) {
Front fr = new Front();
User newUser = null;
try {
int count = 0;
Connection myConn = DriverManager.getConnection("jdbc:mysql://localhost:3306/projects", "william", "william");
java.sql.Statement mySt = myConn.createStatement();
String rs = "SELECT username, password, email, balance FROM user WHERE username='" + usrname + "'AND password='" + pssword + "'";

ResultSet myRS = mySt.executeQuery(rs);
while (myRS.next()) {
newUser = new User(myRS.getString("username"),myRS.getString("password"),myRS.getString("email"),myRS.getDouble("balance"));


if (count == 0) {
return newUser;

if (count == 1) {
return newUser;

} catch (SQLException | HeadlessException ex) {
JOptionPane.showMessageDialog(null, ex);
} catch (ClassNotFoundException ex) {
Logger.getLogger(DBHandler.class.getName()).log(Level.SEVERE, null, ex);
return newUser;


And last, a servlet, which handles forms, and sets different attributes:

protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
PrintWriter out = response.getWriter();
Double balance = 0.00;
String origin = request.getParameter("origin");
String username = request.getParameter("username");
String password = request.getParameter("password");
String email = request.getParameter("email");
case "login":
db.checkLogin(username, password);
User tempUser;
if((tempUser = db.checkLogin(username, password)) != null){
out.print("You will be redirected");
request.getSession().setAttribute("user", tempUser);
}else if(db.checkLogin(username,password) == null){
out.print("Wrong login info, please try again!");

case "register":
db.registerUser(username, password, balance, email);


What I am trying to do, is for example, display on a webpage, one of the sub-values that User has (username, email and so on). However, I am unsure howto use session.getAttribute, and how I can get tempUser.username out.

Thanks in advance.


You can add the attribute to the request and if you are returning it to a view page then you can access it from the response object which gets returned to the view. I will provide an example shortly.

Here is an example:


and in the view page that is on index.jsp as in your example you can achieve it by using these formats

Use of EL (expression language) and JSTL tags is highly recommended. For example, here you could use EL as

<td>Username: </td>
<td><input type="text" value="${user.username}" /></td>

There are other formats too and you can explore them

have a look on this post

How to use session in jsp pages to get information?

You can also use scriptlets but they are way too obesolete.

just try using EL or switch to using spring rest where you can handle everything as efficiently as you want.