aspose file tools*
The moose likes JSP and the fly likes bean always should be in package ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "bean always should be in package ?" Watch "bean always should be in package ?" New topic
Author

bean always should be in package ?

Kaspar Minosiants
Greenhorn

Joined: Jun 21, 2003
Posts: 13
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

Joined: Aug 27, 2003
Posts: 25
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

Joined: Sep 12, 2001
Posts: 23
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.


Shankar Shanmugam<br />Sun Certified Programmer for Java 2 Platform<br />Sun Certified Developer for Java 2 Platform<br />Email: shankar.s@vsnl.net<br /> "Walking on water and building IT Architecture from <br /> specification are easy if and only if both are frozen"<br />-----------------------------------------------
Kaspar Minosiants
Greenhorn

Joined: Jun 21, 2003
Posts: 13
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

Joined: Jun 06, 2002
Posts: 1056
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.


Ron Newman - SCJP 1.2 (100%, 7 August 2002)
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61437
    
  67

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 ]

[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Tiago Fernandez
Ranch Hand

Joined: May 16, 2003
Posts: 167
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.


Tiago Fernandez
http://www.tiago182.spyw.com/
Ron Newman
Ranch Hand

Joined: Jun 06, 2002
Posts: 1056
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

Joined: Jun 21, 2003
Posts: 13
Thanks a lot for your help !
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12806
    
    5
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

Joined: Feb 27, 2002
Posts: 8919

Every class used in a JSP or servlet should be in a package.

Is it mentioned in the spec?


Groovy
Ron Newman
Ranch Hand

Joined: Jun 06, 2002
Posts: 1056
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

Joined: Feb 27, 2002
Posts: 8919

But any class used by a JSP MUST be in a package.

but why?
Ron Newman
Ranch Hand

Joined: Jun 06, 2002
Posts: 1056
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.
 
wood burning stoves
 
subject: bean always should be in package ?