• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

bean always should be in package ?

 
Kaspar Minosiants
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello !
I've been trying to create and use a simple bean in jsp page
I put it under \webapps\try\WEB-INF\classes\beanspackage
where beanspackage is the package of this bean. Everything works fine but when I'm trying to put it under \webapps\try\WEB-INF\classes\ and
comment the package diclaration inside the bean i get error that it's impossible to create the object of the bean.
Is it a necessary point to put beans into packages ?
Thanks !
 
R Laksh
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is not necessary to have it in package.It works fine even if it is not in package.
Are you using jsp:usebean ? Please paste the jsp code and the error...so that we can have a look at it
 
Shankar Shanmugam
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Thats the problem with the Tomcat engine...the one that converts the JSP file into a Java Servlet.
If you open the servlet, it will have a syntax error.
If you have a page import with "MyClass"..
It wil try to generate a Java Servlet code with..
import MyClass;
Which is wrong by java syntax...we are not suppose to import a java class which is in the same package.
So, you might try to remove the page import from jsp file, even then Tomact will nto do the job.
I am not sure, one of my friend did something and fixed this issue. I am not aware of it.
Anyhow, It is adivisable to place the file in a package.
 
Kaspar Minosiants
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It works fine
%CATALINA_HOME%\webapps\try\index.jsp

%CATALINA_HOME%\webapps\try\WEB-INF\classes\beans\Bean_.class
%CATALINA_HOME%\webapps\try\WEB-INF\classes\beans\Bean_.java

--------------------------------------------------------------------
It has a problem
%CATALINA_HOME%\webapps\try\index.jsp

%CATALINA_HOME%\webapps\try\WEB-INF\classes\Bean_.class
%CATALINA_HOME%\webapps\try\WEB-INF\classes\Bean_.java

ERROR HTTP Status 500

[ September 03, 2003: Message edited by: Kaspar Minosiants ]
 
Ron Newman
Ranch Hand
Posts: 1056
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your bean MUST be in a package. Period.
This has to be the most commonly asked question around here. It's the first question I ever asked.
Java 1.4 does not allow a packaged class to import or otherwise access a packageless class. When Tomcat translates your JSP into a Java class file, it puts the class into a package.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64715
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is not necessary to have it in package.It works fine even if it is not in package.

As Ron pointed out, the above is incorrect. Always package your classes.
hth,
bear
[ September 03, 2003: Message edited by: Bear Bibeault ]
 
Tiago Fernandez
Ranch Hand
Posts: 167
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your bean MUST be in a package.

I don't think so, you don't need to pack your beans to use 'em in your JSP. I got an web-app that works without packages:
FooJSP.jsp:
<%@ page language="java" import="FooBean" %>
<jsp:useBean id="anything" scope="request" type="FooBean"/>
The source of FooBean is in my classpath root (/WEB-INF/classes). And it works fine.
 
Ron Newman
Ranch Hand
Posts: 1056
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you using Java 1.3? This changed in 1.4 .
See http://developer.java.sun.com/developer/bugParade/bugs/4361575.html
[ September 03, 2003: Message edited by: Ron Newman ]
 
Kaspar Minosiants
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot for your help !
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13058
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Every class used in a JSP or servlet should be in a package. You may get one particular configuration of server to work without it but you are just asking for trouble down the road with mysterious errors.
Bill.
 
Pradeep bhatt
Ranch Hand
Posts: 8927
Firefox Browser Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Every class used in a JSP or servlet should be in a package.

Is it mentioned in the spec?
 
Ron Newman
Ranch Hand
Posts: 1056
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Servlet classes do not need to be in packages, though it's a good idea. But any class used by a JSP MUST be in a package.
 
Pradeep bhatt
Ranch Hand
Posts: 8927
Firefox Browser Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But any class used by a JSP MUST be in a package.

but why?
 
Ron Newman
Ranch Hand
Posts: 1056
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tomcat translates your JSP into a servlet class in the package "org.apache.jsp". Other containers probably do something similar.
Since your servlet is in a package, its code cannot access any packageless class. This is a change to Java, as of 1.4.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic