Filter中的ClassCastException?

时间:2013-08-19 08:11:14

标签: java web-applications filter classcastexception

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

这是我的过滤器,用于检查用户类型(管理员,经理,用户)..我在此处收到错误标记为...

    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的定向取决于usertype ...(如果用户去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);

        }

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

3 个答案:

答案 0 :(得分:2)

tmpPerson类型的对象。据推测,.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();
......................

答案 1 :(得分:0)

嗯,您将usertype设置为tmp.getType(),所以这并不奇怪。您应该将tmp存储在usertype下,或者将usertype存储到tmp.getType()的任何类型。

答案 2 :(得分:-1)

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

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

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

你可以自己弄清楚看看logcat输出。 同时发布您的LogCat输出,以便我们可以告诉您确切的问题所在。