4

I'm just playing around with JSP. I just wanted to test some <jsp:useBean> stuff, but I can't. Every time if I'm using <jsp:useBean>, I get an error. Even if I just have this, I get an error:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Test</title>
  </head>
  <body>
    <jsp:useBean id="mybean" class="Users" scope="session" >
      <jsp:setProperty name="mybean" property="name" value="Hello world" />
    </jsp:useBean>
  </body>
</html>

Without the <jsp:useBean> it runs fine. With the <jsp:useBean> I get an error like:

Servlet.service() for servlet [jsp] in context with path [/JSPTest] threw exception [Unable to compile class for JSP: 

An error occurred at line: 10 in the jsp file: /index.jsp
Users cannot be resolved to a type
7:     <title>Insert title here</title>
8:   </head>
9:   <body>
10:     <jsp:useBean id="mybean" class="Users" scope="session" >
11:       <jsp:setProperty name="mybean" property="name" value="Hello world" />
12:     </jsp:useBean>
13:   </body>

I am using Eclipse, Tomcat 7.0.23 and Java 1.7.0_01.

Any ideas?

PS: I had to change the port 8xxx to 9xxx because the oracle DB is using the standard 8xxx. But that's likely not the cause of the problem.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
OverStack
  • 747
  • 3
  • 12
  • 18

1 Answers1

11

You need to put classes in a package in order to be able to use them in another classes. Classes in the default package are invisible to classes which are by itself in a package (like as the JSP would end up).

So, give the Users class a package like so:

package com.example;

public class Users {
    // ...
}

Recompile and put it in /WEB-INF/classes/com/example/Users.class.

Then you can reference it as follows:

<jsp:useBean id="myBean" class="com.example.Users" />

Unrelated to the concrete problem, having a plural as class name of an entity is usually a smell. Does a single instance of that class really represent multiple users? Why would you not have a List<User> for example? Or does it actually represent a single user? It should then be named User instead.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • Hi. That can work, but now I found an easier way. I just have to import the class I need and everything is ok. Something like this: <%@ page import = "com.MyClass" %> , and lets dont about the plurar and List stuff ^^ this should be only a test nothing else. Just want to learn how to jsp. – OverStack Dec 05 '11 at 00:06
  • That would only work if you were using old school *scriptlets* to access the data, which means that you're stepping backwards from the proper MVC approach (not that `` is part of it as it essentially only binds M with V without C). *Scriptlets* are however discouraged almost a decade. See also http://stackoverflow.com/questions/3177733/how-to-avoid-java-code-in-jsp-files and http://stackoverflow.com/tags/jsp/info – BalusC Dec 05 '11 at 00:09
  • Oh good to know. But thats really stressful. I tought jsp is nice because you can use your javacode/classes AND if you want to use them you can just use also javacode (scriptlets). And now I have to learn jstl and other stuff. Thats not nice of jsp ^^ - but yeah I will try it. But it looks really complex :/ – OverStack Dec 05 '11 at 15:12
  • 1
    You can also write JSPs "the PHP way", but that would only end up in maintainability disaster :) – BalusC Dec 05 '11 at 15:28