我在我的网络应用程序中遇到了困难,我发布了代码并定义了我在代码部分中所做的事情......
这是我的过滤器,它检查用户类型(管理员、经理、用户)。我在标记为...的行出现错误。
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
HttpServletRequest req = (HttpServletRequest) request;
HttpSession session = req.getSession();
RequestDispatcher rd=null;
Person user = (Person) session.getAttribute("usertype"); <------ **IM GETTING EXCEPTION HERE!**
if (user != null && user.getType().equals(UserType.MANAGER.toString())) {
String nextJSP = "/ManagerHome.jsp";
rd = request.getRequestDispatcher(nextJSP);
rd.forward(request, response);
}
else if (user != null && user.getType().equals(UserType.ADMIN.toString())) {
String nextJSP = "/AdminHome.jsp";
rd = request.getRequestDispatcher(nextJSP);
rd.forward(request, response);
}
else if (user != null && user.getType().equals(UserType.USER.toString())) {
String nextJSP = "/UserHome.jsp";
rd = request.getRequestDispatcher(nextJSP);
rd.forward(request, response);
}
else {
String nextJSP = "/Login.jsp";
rd = request.getRequestDispatcher(nextJSP);
rd.forward(request, response);
}
chain.doFilter(request, response);
}
这是我的 Person 类,其中包含人员记录
@Table(name="\"Person\"")
public class Person implements Serializable {
/**
*
*/
private static final long serialVersionUID = 2532993385565282772L;
@Id
@Column(name="id",nullable=false,updatable=false)
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String username;
private String name;
private String surname;
private String sskno;
private String address;
private String telno;
private String type;
@OneToMany
private List<Leave> leaves;
public Person() {
}
getters & setters....
这是我的 LoginServlet... 这里暂时 Pusername、Pname、Pusertype 和 Pusername 用于设置会话属性。根据此页面,JSP 的指向取决于用户类型...(如果用户转到 userhome,如果经理经理回家并继续)...我知道为什么我会收到此错误,但我不知道要避免它。我做了研究,但对我没有任何帮助......请帮助我这里是我的 Loginservlet
public class LoginServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public LoginServlet() {
super();
}
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
try {
String pName;
String pSurname;
String pUserName;
String pUserType;
String query;
String home="/Login.jsp";
String username = request.getParameter("username");
String password = request.getParameter("password");
RequestDispatcher rd = request.getRequestDispatcher(home);
mysqlCon con = new mysqlCon();
//HttpSession session = request.getSession();
LoginService ls = new LoginService();
Statement stmt = con.getConnection().createStatement();
query = "SELECT name, surname, usertype, username FROM employee WHERE username='"
+ username + "' AND password='" + password + "';";
stmt.executeQuery(query);
ResultSet rs = stmt.getResultSet();
if(rs.next()){
pName = rs.getString(1);
pSurname = rs.getString(2);
pUserType = rs.getString(3);
pUserName = rs.getString(4);
if (ls.loginCheck(username, password) != false) {
Person tmp = new Person();
tmp.setName(pName);
tmp.setSurname(pSurname);
tmp.setType(pUserType);
tmp.setUsername(pUserName);
HttpSession session = request.getSession();
session.setAttribute("name", tmp.getName());
session.setAttribute("surname", tmp.getSurname());
session.setAttribute("usertype", tmp.getType());
session.setAttribute("username", tmp.getUsername());
if (pUserType.equals(UserType.MANAGER.toString())) {
String nextJSP = "home/ManagerHome.jsp";
rd = request.getRequestDispatcher(nextJSP);
rd.forward(request, response);
}
else if (pUserType.equals(UserType.ADMIN.toString())) {
String nextJSP = "home/AdminHome.jsp";
rd = request.getRequestDispatcher(nextJSP);
rd.forward(request, response);
}
else if (pUserType.equals(UserType.USER.toString())) {
String nextJSP = "home/UserHome.jsp";
rd = request.getRequestDispatcher(nextJSP);
rd.forward(request, response);
}
else {
String nextJSP = "/Login.jsp";
rd = request.getRequestDispatcher(nextJSP);
rd.forward(request, response);
}
}
}
else {
rd.forward(request, response);
}
如果您希望我添加更多信息,我可以做到。我的问题是,我怎样才能避免这种情况并使其发挥作用。