我遇到了一个与bean类和session相关的问题 带有一些私有字段和一些setter和getter的简单类 方法和一个类与数据库的交互和另一个 控制器,我面临的问题是在main上将用户值设置为null 任何人都可以帮助我,我在这里发布完整的代码 throgh login.jsp我正在传递价值并通过用户在jsp中记录我试图检索请任何人帮助我在这里。
public class ConnectionManager {
public static Connection getConnection(){
Connection con=null;
try{
Class.forName("com.mysql.jdbc.Driver");
con=(Connection) DriverManager.getConnection("jdbc:mysql://localhost/demo2","root","");
}catch(Exception e){
}
return con;
}}
public class LoginServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, java.io.IOException {
try
{
UserBean user = new UserBean();
user.setUserName(request.getParameter("un"));
user.setPassword(request.getParameter("pw"));
user = UserDAO.login(user);
if (user.isValid())
{
HttpSession session = request.getSession();
session.setAttribute("currentSessionUser",user);
response.sendRedirect("userLogged.jsp");
}
else
response.sendRedirect("invalidLogin.jsp");
}
catch (Throwable theException)
{
System.out.println(theException);
}
}
}
public class UserDAO
{
static Connection currentCon = null;
static ResultSet rs = null;
public static UserBean login(UserBean bean) {
//preparing some objects for connection
Statement stmt = null;
String username = bean.getUsername();
String password = bean.getPassword();
String searchQuery =
"select * from users where username='"
+ username
+ "' AND password='"
+ password
+ "'";
try
{
currentCon = ConnectionManager.getConnection();
stmt=currentCon.createStatement();
rs = stmt.executeQuery(searchQuery);
boolean more = rs.next();
// if user does not exist set the isValid variable to false
if (!more)
{
System.out.println("Sorry, you are not a registered user! Please sign up first");
bean.setValid(false);
}
//if user exists set the isValid variable to true
else if (more)
{
String firstName = rs.getString("FirstName");
String lastName = rs.getString("LastName");
bean.setFirstName(firstName);
bean.setLastName(lastName);
bean.setValid(true);
}
}
catch (Exception ex)
{
System.out.println("Log In failed: An Exception has occurred! " + ex);
}
finally
{
if (rs != null) {
try {
rs.close();
} catch (Exception e) {}
rs = null;
}
if (stmt != null) {
try {
stmt.close();
} catch (Exception e) {}
stmt = null;
}
if (currentCon != null) {
try {
currentCon.close();
} catch (Exception e) {
}
currentCon = null;
}
}
return bean;
}
}
public class UserBean {
private String username;
private String password;
private String firstName;
private String lastName;
public boolean valid;
public String getFirstName() {
return firstName;
}
public void setFirstName(String newFirstName) {
firstName = newFirstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String newLastName) {
lastName = newLastName;
}
public String getPassword() {
return password;
}
public void setPassword(String newPassword) {
password = newPassword;
}
public String getUsername() {
return username;
}
public void setUserName(String newUsername) {
username = newUsername;
}
public boolean isValid() {
return valid;
}
public void setValid(boolean newValid) {
valid = newValid;
}
}
/*login.jsp*/
form action="LoginServlet"
Please enter your username
input type="text" name="un"
Please enter your password
input type="text" name="pw"
input type="submit" value="submit"
/form
/*userLogged.jsp*/
html
Welcome Pname:<%=request.getAttribute("user")%>
/html
答案 0 :(得分:-1)
实际上,我只是在寻找语法。我知道了;这是......
<p>first name: ${currentSessionUser.firstName}</p>