0

我在我的网络应用程序中遇到了困难,我发布了代码并定义了我在代码部分中所做的事情......

这是我的过滤器,它检查用户类型(管理员、经理、用户)。我在标记为...的行出现错误。

    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);

        }

如果您希望我添加更多信息,我可以做到。我的问题是,我怎样才能避免这种情况并使其发挥作用。

4

3 回答 3

2

tmp是一个类型的对象Person。想必,.getType()给你类的String type;属性Person。所以,你实际上是String在这里设置一个对象:

session.setAttribute("usertype", tmp.getType());

因此,下面的行导致ClassCastException

Person user = (Person) session.getAttribute("usertype"); 

您需要将返回值转换为String.

String userType = (String) session.getAttribute("usertype");

更好的是,您可以将整个Person对象设置在session.

HttpSession session = request.getSession();
session.setAttribute("person", tmp);

然后您可以将其属性检索为:

Person user = (Person) session.getAttribute("person");
String personType = user.getType();
......................
于 2013-08-19T08:15:22.120 回答
0

好吧,你设置usertypetmp.getType(),所以这并不奇怪。您应该存储或转换tmp为任何类型。usertypeusertypetmp.getType()

于 2013-08-19T08:14:09.697 回答
-1

你可以在 Java 的文档中看到这个错误。这一行将帮助您理解和修复它。

抛出以指示代码已尝试将对象强制转换为它不是实例的子类。例如,以下代码生成 ClassCastException:

 Object x = new Integer(0);
 System.out.println((String)x);

您可以自己查看 logcat 输出来弄清楚。还要发布您的 LogCat 输出,以便我们可以告诉您确切的问题出在哪里。

于 2013-08-19T08:13:31.693 回答