0

I'm having difficulties in my web app, I post codes and define what im doing in code parts...

This is my Filter which checks user type(admin, manager, user).. And im getting error here at line which marked...

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

And This is my Person class which has records of 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....

And This is my LoginServlet... Here at temporarily Pusername,Pname,Pusertype and Pusername are for setting attribute on session. And according to this pages which are JSP's are directed depends to usertype...(if user go userhome, if manager manager home and go on)... I know why im getting this error but I don't know hot avoid it. I did research and nothing worked for me... Please help me here is my 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);

        }

IF you want me to add more information i can do it. My question is that how can i avoid this and make this work.

Pitch
  • 87
  • 1
  • 4
  • 13

3 Answers3

2

tmp is an object of type Person . Presumably , .getType() gets you the String type; attribute of Person class. So , you are actually setting a String object here :

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

Hence the below line results in ClassCastException :

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

You need to cast the return value to String .

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

Better, you can set the entire Person object in the session .

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

Then you can retrieve its properties as :

Person user = (Person) session.getAttribute("person");
String personType = user.getType();
......................
AllTooSir
  • 48,828
  • 16
  • 130
  • 164
  • Your saying i should do like this? Person user = (Person) session.getAttribute("usertype"); String usertype=user.getType(); – Pitch Aug 19 '13 at 08:24
  • @Pitch Absolutely not , go through my answer once more . – AllTooSir Aug 19 '13 at 08:25
  • By the way i should get usertype seperated from other session attributes. because i need to call it in sql statement – Pitch Aug 19 '13 at 08:26
  • @Pitch You can do that I have shown you the code sample in the second part of my answer . I put the entire `Person` in `session` and then retrieve the entire `Person` and then access its individual properties. – AllTooSir Aug 19 '13 at 08:27
  • I did what exactly u said :) and i understand your point but now im getting null exception here String personType = user.getType(); – Pitch Aug 19 '13 at 08:32
0

Well, you're setting usertype to be tmp.getType(), so it's not that surprising. You should either be storing tmp under usertype or casting usertype to whatever the type of tmp.getType() is.

chrylis -cautiouslyoptimistic-
  • 75,269
  • 21
  • 115
  • 152
-1

You can see this error in Java's documentation. This one line will help you to understand and fix it.

Thrown to indicate that the code has attempted to cast an object to a subclass of which it is not an instance. For example, the following code generates a ClassCastException:

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

You can yourself figure it out looking at the logcat output. Also post your LogCat output so that we could tell you where the exact problem is.

Master
  • 2,945
  • 5
  • 34
  • 65