我有一个people类。它的类里面有一个people job。
我有一个过滤器类。在这个类中,我创建了一个对象。People people = new People();
并设置他们的工作。people.setjob="doctor";
现在,我如何在另一个类中使用这个对象?例如,在checkjob类中,我写道 people.getJob
却什么都没有发生。
我把我的问题画出来,让你容易理解。
顺便说一下,我这样做是为了获得用户角色。我把问题最小化了。
下面是代码。
Login.Java
if(dao.check(uname, pass)) {
UserAccount user = new UserAccount();
user.setUsername(uname);
Filter2 loginbean = new Filter2();
String userValidate = loginbean.authenticateUser(user);
if(userValidate.equals("Admin_Role")) {
HttpSession session = request.getSession();
session.setAttribute("username", uname);
user.setRole("Admin");
response.sendRedirect("welcome.jsp");
}
else if (userValidate.equals("Editor_Role")) {
HttpSession session = request.getSession();
session.setAttribute("username", uname);
user.setRole("Editör");
response.sendRedirect("gorevler.jsp");
}
用户账户.Java
public class UserAccount {
private String username;
private String role;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getRole() {
return role;
}
public void setRole(String role) {
this.role = role;
}
Filter2.Java
public String authenticateUser(UserAccount loginBean)
{
String userName = loginBean.getUsername();
Connection con = null;
Statement statement = null;
ResultSet resultSet = null;
String userNameDB = "";
String passwordDB = "";
String roleDB = "";
try
{
con= (Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/covid","root","fbfbfb333*");
statement = con.createStatement();
resultSet = statement.executeQuery("select username,role from users");
while(resultSet.next())
{
userNameDB = resultSet.getString("username");
roleDB = resultSet.getString("role");
if(userName.equals("emre") && roleDB.equals("admin")) {
return "Admin_Role";}
else if(userName.equals(userNameDB) && roleDB.equals("editor"))
return "Editor_Role";
else if(userName.equals(userNameDB) && roleDB.equals("user"))
return "User_Role";
}
}
欢迎.Java
<body>
<jsp:include page="menu.jsp"></jsp:include>
<%
if(session.getAttribute("username")==null){
response.sendRedirect("login.jsp");
}
UserAccount user = new UserAccount();
String rol = user.getRole();
pageContext.setAttribute("rol", rol);
%>
WELCOME ${username}
${rol }
你需要在其他类中重新初始化它。 并给它同样的属性。