Alkahna
Alkahna

Reputation: 441

can not cast object in jsp to java object

I can not get this working and I dont know why...

Any Idea how to solve this Problem??

user_management.jsp

<% 
    User user = (User) session.getAttribute("obj_user");
    boolean test = user.oe_fac_role_right.get(2).get(0).equals(1);
    if (test) { 
%>
- some html code will be displayed here is the user is allowed to see it
<%  }  %>

User class

public class User {
    int user_id;
    String username;
    List<List<Integer>> oe_fac_role_right = new ArrayList<List<Integer>>(4);

    public User(){}

    ....
    ....
}

Creation of User Object in login class:

User user = new User(user_id, username, user_rights);

Passing User Object to session:

session.setAttribute("obj_user", user);

Error message from Netbeans

 An error occurred at line: 14 in the jsp file: /user_management.jsp
 user cannot be resolved to a type

 Line 14 in this case is: User user = (User) session.getAttribute("obj_user");

Upvotes: 2

Views: 3395

Answers (2)

Andreas
Andreas

Reputation: 159175

it does not have any imports as the classes are all in the default package, so they should be accessable

Incorrect. The unnamed package is not accessible from another package, and the unnamed package cannot be imported.

Since a JSP is compiled to a Java class in some package, it can never access a class in the unnamed package.

Solution: You have to declare a package for the User class, then import the class in the JSP.

See Import package with no name Java.

Upvotes: 2

Karibasappa G C
Karibasappa G C

Reputation: 2732

Your line is having user starting with small letter but class name is User starting with capital letter.

change

user user = (user) session.getAttribute("obj_user");

to

User user = (user) session.getAttribute("obj_user");

Also make your class public and import your class in jsp as below

<%@ page import="yourPackage.User%>

if this is not working, let me know.

Upvotes: 2

Related Questions