wood burning stoves 2.0*
The moose likes JSF and the fly likes Sessions in JSF Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSF
Bookmark "Sessions in JSF" Watch "Sessions in JSF" New topic
Author

Sessions in JSF

Ravi Thumma
Greenhorn

Joined: Jul 04, 2009
Posts: 13


hi all
I am new to JSF. My doubt is I have created session in a class and I found no clue of getting the session variable from another method in the same class.
Below is the example:

In this example I have created session in UserLogin() method. I can invalidate the session but I am unable to modify userdata from SaveUser() method using sessions. I have also used session in managed bean scope. Please help me in this regard.

Thanks in advance.


import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.faces.application.Application;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

import javax.servlet.http.HttpSession;

import javax.sql.DataSource;
//kage usrreg;


//import javax.servlet.*;

//import javax.faces.application.*;


public class UserLoginBean {

public UserLoginBean() throws NamingException, SQLException {
try {
loadDriver();
} catch (NamingException n) {
//System.out.println("")
n.printStackTrace();
} catch (SQLException s) {
s.printStackTrace();
}
}

public void loadDriver() throws SQLException, NamingException {
try {
ctx = new InitialContext();
if (ctx == null) {
throw new NamingException("Context not found");
}
ds = (DataSource)ctx.lookup("jdbc/jndi");
if (ds == null) {
throw new SQLException("Datasource not found");
}
} catch (NamingException n) {
System.out.println("Cannot obtain Initial Context in class UserLoginBean....");
n.printStackTrace();
} catch (SQLException s) {
System.out.println("cannot obtain DataSource in class UserLoginBean....");
s.printStackTrace();
}
}
private String userName;
private String password;

private String firstName;
private String lastName;
private String email;
private String phone;
private String address;
private String country;
private String state;


Context ctx;
DataSource ds;
public Connection con;
public Statement st;
public ResultSet rs;


public void setUserName(String userName) {
this.userName = userName;
}

public String getUserName() {
return "userName";
}

public void setPassword(String password) {
this.password = password;
}

public String getPassword() {
return "password";
}

public void setFirstName(String firstName) {
this.firstName = firstName;
}

public String getFirstName() {
return "firstName";
}

public void setLastName(String lastName) {
this.lastName = lastName;
}

public String getLastName() {
return "lastName";
}

public void setEmail(String email) {
this.email = email;
}

public String getEmail() {
return "email";
}

public void setPhone(String phone) {
this.phone = phone;
}

public String getPhone() {
return "phone";
}

public void setAddress(String address) {
this.address = address;
}

public String getAddress() {
return "address";
}

public void setCountry(String country) {
this.country = country;
}

public String getCountry() {
return "country";
}

public void setState(String state) {
this.state = state;
}

public String getState() {
return "state";
}

public String userLogin() throws SQLException {

String returnString = null;
con = ds.getConnection();

if (con == null) {
throw new SQLException("Connection not created");
}
try {
st = con.createStatement();
String sql =
"SELECT USERNAME, PASSWORD FROM user_registration WHERE USERNAME='" +
userName + "'" + "AND password='" + password + "'";
/*" SELECT USERNAME, PASSWORD FROM USER_REGISTRATION " + " WHERE USERNAME =' " +
userName + " ' " + "AND password =' " + password + " ' ";*/

rs = st.executeQuery(sql);

while (rs.next()) {
String user = rs.getString("USERNAME");
String pass;
pass = rs.getString("PASSWORD");

System.out.println("User name is: " + user);
System.out.println("Password is: " + pass);

if (((user!=userName || user == null || user.length() == 0)) &&
((pass!=password || pass==null || pass.length()==0))) {


FacesMessage fm = new FacesMessage("Incorrect User Name or Password.");
FacesContext.getCurrentInstance().addMessage("Incorrect User Name or Password.", fm);
System.out.println("username & password does not exist");
returnString="login";
} else {
FacesContext context = FacesContext.getCurrentInstance();
HttpSession session =
(HttpSession)context.getExternalContext().getSession(true);

session.setAttribute("username", user);
session.setAttribute("password", pass);

System.out.println("username & password exists");
returnString = "success";

}
}
} catch (SQLException s) {
s.printStackTrace();
} finally {
try {
rs.close();
st.close();
//con.close();
//System.out.println("All Opened Resources Closed");
} catch (SQLException e) {
e.printStackTrace();
}
}
return returnString;
}

public String userLogOut() throws SQLException{
String returnstring=null;
try{
FacesContext context = FacesContext.getCurrentInstance();
HttpSession session =
(HttpSession)context.getExternalContext().getSession(true);


if (session == null) {
System.out.println("Session not valid");
returnstring= "failure";
} else {
System.out.println("Session Invalidated");
session.invalidate();
returnstring= "login";
}
} /*catch (SQLException s) {
s.printStackTrace();
System.out.println("Cannot Invalidate session");
}*/
finally {
try {
// rs.close();
//st.close();
con.close();
System.out.println("All Opened Resources Closed");
} catch (SQLException e) {
e.printStackTrace();
}
}
return returnstring;
}

public String saveUser() throws SQLException {
try {
FacesContext context = FacesContext.getCurrentInstance();
Application application = context.getApplication();
HttpSession session =
(HttpSession)context.getExternalContext().getSession(true);
String username = (String)session.getAttribute("username");

if (username != null) {
String save =
"UPDATE user_registration SET firstname='" + firstName +
"', lastname='" + lastName + "', email='" + email +
"', phone='" + phone + "', address='" + address +
"', country='" + country + "', state='" + state + "'
"WHERE username='" + username + "'";

}
} catch (SQLException sq) {
sq.printStackTrace();
} finally {
try {
// rs.close();
//st.close();
con.close();
System.out.println("All Opened Resources Closed");
} catch (SQLException e) {
e.printStackTrace();
}
}
}

}
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Sessions in JSF