Renny
Renny

Reputation: 114

Using session with Struts2 in JSP

I have a login (username and password stored in MySQL database). After insert username and password in textfield, the class Login controls if the username and password are stored in database or not, then in my 'LoginRiuscito.jsp' i want to write Welcome username.

This is my index.jsp:

<s:form action="acces" id="login" style="display:none">
<h1>Login</h1>
<s:label value="Username"/><s:textfield id="username" name="username" required="true"/>
<s:label value="Password"/><s:password name="password" required="true"/>
<s:submit value="Accedi"/>
</s:form>

This is my Class named 'Login':

public class Login extends ActionSupport implements SessionAware {

public String accesso() throws ClassNotFoundException, SQLException{
                   Connessione();  // DB connection method
                   QueryL();   // Query for login
                   session.put("username",username);
                   return QueryL();
               }
    public void setUsername(String username) {this.username = username;}
        public String getUsername() {return username;}
        public String getPassword() {return password;}
        public void setPassword(String password) {this.password = password;}
        public Map<String, Object> getSession(){return session;}
        @Override
        public void setSession(Map<String, Object> session) {
            this.session=session;
        }

I have this in 'LoginRiuscito.jsp' :

Welcome : <s:property value="%{#session.Login.username}" />

But it doesn't work, when i go in 'LoginRiuscito.jsp' i see only "Welcome: "

Upvotes: 0

Views: 124

Answers (1)

Andrea Ligios
Andrea Ligios

Reputation: 50203

You didn't use Login.username, but only username as key, then change your tag accordingly:

from

<s:property value="%{#session.Login.username}" />

to

<s:property value="%{#session.username}" />

Upvotes: 1

Related Questions