Directory Structure & Packages

Hi

I'm using Tomcat 3.3 to deploy my app, on Win 2K. I have some jsp files in webapps\myApp\*.jsp , and some additional jsp files in a subfolder of this called webapps\myApp\admin\*.jsp. These are protected by additional security restrictions.

I want both groups of jsp files to be able to access a bean that I have under webapps\myApp\WEB-INF\classes\*.class. The jsp pages under admin fail to find the bean because they expect it to be in a package called admin.

Any ideas?

[509 byte] By [rfullert] at [2007-9-26 18:58:13]
# 1
Since its a commonly used bean, how about putting it inside $tomcat\common\classes?
scorpioz at 2007-7-3 3:58:40 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2

Try setting the scope attibute of the bean first.

There are 4 ways to set the scope of a bean and they are;

<b>Page</b>

This is bound to the local variable. The bean object should be placed in the <i>PageContext</i> object for the duration of the current request. Storing the object there means thatsevlet code can access it by calling getAttribute on the pageContext variable.

<b>application</b>

It is also bound to the local variable but is also stored in the shared <i>ServletContext</i>. It is available through the application variable or by a call to <i>getServletContext()</i>. <i>ServletContext</i>is shared by all servlets in the same web application and values can be retrieved from the <i>ServletContext</i> by the getAttribute method.

This can do 2 things for you.

1 It allows for multiple servlets and JSP pages to access the same object.

2 It allows for servlets to create a bean that will be used in JSP pages, not just access one prieviously created. This is great when you are following the MVC architecture(or model 2 approach) because the servlet is able to handle complexe interactions and forward the results to an appropriate JSP page.

<b>session</b>

Also bound to the local variable but it also means that the bean will also be stores in the <i>HttpSession</i> object for the duration of the current request. It can be retrieved using the getAttribute method.

<b>request</b>

Again bound to the local variable but als to <i>ServletRequest</i> objectfor the duration of the current request.It can be made available with the getAttribute method. Storing values in the request object is common when using the MVC (model2) architecture.

You would set this option in the following;

<jsp:useBean id="blah" class="package.directory.BlahBlah" scope="application">

<%-- scope could equal any of the 4 dependiong on your needs --%>

I hope this helps.

mercenarysteel at 2007-7-3 3:58:40 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 3
ooops i forgot this hehe ..</jsp:useBean>
mercenarysteel at 2007-7-3 3:58:40 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 4

>>The jsp pages under admin fail to find the bean because >>they expect it to be in a package called admin.

I am not sure if that is the problem. I havnet used beans too much before but to fing beans won't the page be looking for the bean class with proper package name in the Web-Inf folder?

I think that something else could be wrong.

Pls. retest to make sure and post the results.

thanks.

dasnani at 2007-7-3 3:58:40 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 5

> I want both groups of jsp files to be able to access a

> bean that I have under

> webapps\myApp\WEB-INF\classes\*.class. The jsp pages

> under admin fail to find the bean because they expect

> it to be in a package called admin.

put the bean in a proper package and add an import directive for this package in the jsp.

robert

r_klemme at 2007-7-3 3:58:40 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 6

Thanks for everyone's help, I created a new package and by explicitly referencing the bean through its package name the problems are solved.

- Robert, is there any advantage to importing the Bean with an import directive over <jsp:useBean class = ... /> ?

Thanks,

Ross

rfullert at 2007-7-3 3:58:40 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 7
hm, i guess it's only this: when you have multiple beans in that package you can refer them directly without the package name.robert
r_klemme at 2007-7-3 3:58:40 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...