2

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

  • Inside my .jsp I want to check if the executing user has suffient rights to do so.
  • the user object from the class user is one of the session attributes
  • inside my .jsp file I get the Object from the session
  • I then cast it into user
  • then I can access its stored values (which is an array list)

  • Then when I go and test my code I get the error listed below.

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");
Alkahna
  • 441
  • 7
  • 21
  • how looks the import section of your jsp? – nano_nano Dec 02 '15 at 15:07
  • it does not have any imports as the classes are all in the default package, so they should be accessable – Alkahna Dec 02 '15 at 15:08
  • What is line 14? Is this error happening when you cast it, or when you try to access it? Maybe check if `user` is null first? – Hanlet Escaño Dec 02 '15 at 15:09
  • 1
    Line 14 is: user user = (user) session.getAttribute("obj_user"); – Alkahna Dec 02 '15 at 15:11
  • You are positive `obj_user` is the right attribute, and that furthermore, it is of type `user`? and that it is not `null`? – Hanlet Escaño Dec 02 '15 at 15:13
  • 1
    I create the user object in a login class like this: `user user = new user(user_id, username, user_rights);` and after that i put it into the session with `session.setAttribute("obj_user", user);` – Alkahna Dec 02 '15 at 15:15
  • 2
    FYI: According to [JLS](https://docs.oracle.com/javase/specs/jls/se7/html/jls-6.html#jls-6.2), class names should start with uppercase letter (`User`): *"Names of class types should be descriptive nouns or noun phrases, not overly long, in mixed case with the first letter of each word capitalized."* – Andreas Dec 02 '15 at 15:15
  • yeah I have to change class names to start with uppercase letter later on ;) – Alkahna Dec 02 '15 at 15:17
  • did change class names but that has no effect on the error & there is nothing to import as all classes are inside the default package – Alkahna Dec 02 '15 at 15:24
  • 1
    I think you should import the classes you use like this `<%@page import="names.name_package.*"%>` in the jsp – yaylitzis Dec 02 '15 at 15:26

2 Answers2

2

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.

Karibasappa G C
  • 2,686
  • 1
  • 18
  • 27
  • did that but by moving all my classes to a package (SSP in my case) I get some more errors and the application will not deploy anymore – Alkahna Dec 02 '15 at 15:34
  • now i just moved the class User in a package (other classes are in default package still) But how can I import a class from default package to the newly created package (I have to access some other class from there) – Alkahna Dec 02 '15 at 15:39
  • @Alkahna As I said in my answer, you *cannot* import from the unnamed package. The unnamed package is only intended for use by small programs where the entire program is in the unnamed package. If you use packages (and you have to in a webapp), *all* your classes must be in packages. – Andreas Dec 02 '15 at 16:39
  • 1
    I would suggest you to keep all classes in the respective package. dont maintain default package at all. and give access modifier as public to access those classes in different package. – Karibasappa G C Dec 03 '15 at 03:41
2

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.

Community
  • 1
  • 1
Andreas
  • 154,647
  • 11
  • 152
  • 247