This week's giveaway is in the Spring forum.
We're giving away four copies of REST with Spring (video course) and have Eugen Paraschiv on-line!
See this thread for details.
The moose likes Servlets and the fly likes access beans into servlets Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "access beans into servlets" Watch "access beans into servlets" New topic

access beans into servlets

rashmi kulkarni

Joined: May 24, 2013
Posts: 2
in my web based java project, i have the following file structure in tomacat7

Here, Bean is a package with a single class that performs database operations.
Servlets contains the java servlets. ChangePassword is one of them.
I want to call the functions of MyBean in the

the statement "import Bean.*;"
gives error message
"package Bean not found"
when i try to compile

please help..........
Devaka Cooray
ExamLab Creator

Joined: Jul 29, 2008
Posts: 3783

It is pointless and insecured to have Java source files placed in your web app. Move all your source files to a separate place and compile from right there; then you can copy your compiled classes (class structure) to /WEB-INF/classes directory. If MyBean.class doesn't have a source available, best way to deploy it would be as a JAR file. Make a JAR containing Bean/MyBean.class, and deploy it to WEB-INF/lib directory. You should also have it in your classpath when you compile.
Also note that it should be WEB-INF, not web-inf. Moreover, we have a convention that package names are always written in lowercase letters, so one can realize that 'bean' is a package name and 'Bean' is a class name.

Author of ExamLab ExamLab - a free SCJP / OCPJP exam simulator
What would SCJP exam questions look like? -- Home -- Twitter -- How to Ask a Question
bharat salunkhe

Joined: May 28, 2013
Posts: 25

hi import specific class like
import Bean.MyBean;
and make sure your beans or jar are added properly
Rajesh Ronald Reagan

Joined: May 28, 2013
Posts: 7
Hi Rashmi,

Be specific, are you talking about application specific beans ? the one with getter and setter methods ?

Since it is like the Java Class, you can keep it in your package.

Please take a look below.

i have a package called com.xx.yy.beans. i have so many beans in it. I require the following four beans in one servlet.

Step 1 Import the package in the servlet.

Step 2. In servlet where you initialize this, either in the init() method ?

For Example the following is a bean code.

I need this to be in my servlet. Say a HttpServlet.

This is how i access the Bean.

I hope this would make you understand much better.

I believe you are trying in eclipse. A suggestion, Do not create servlet class in eclipse using the template that is the wizard. Create a general class and add the methods one by one.

Ronald Reagan Jr.
Devaka Cooray
ExamLab Creator

Joined: Jul 29, 2008
Posts: 3783

I believe that OP's question was about deployment, not about beans how-to.
Rajesh Ronald Reagan

Joined: May 28, 2013
Posts: 7

The answer to your query is , in your bean class.

While deploying in tomcat,

Please make the following folder structure.

+<all jsp files>
-------- +classes
---------- +Bean
------------- +MyBean.class
-------- +Servlets
------ +web.xml

As Devaka Cooray rightly pointed, please do not move the source to your webapps directory.

rashmi kulkarni

Joined: May 24, 2013
Posts: 2
hi Devaka, Rajesh n Bharat.........

Reading all your posts, I understand that i have to create a .jar file for Bean.
And it is already WEB-INF in my file structure(to be clear)
The thing is that i didn' know where to place the .jar file.
I will try copying it into WEB-INF/lib... and set the classpath accordingly....
I get back to you soon......
Bear Bibeault
Author and ninkuma

Joined: Jan 10, 2002
Posts: 63540

Yes, jar files are placed in WEB-INF/lib. But you don't need to put your classes in a jar file. They can alos be placed in the appropriate package hierarchy under WEB-INF/classes.

[Asking smart questions] [About Bear] [Books by Bear]
I agree. Here's the link:
subject: access beans into servlets
jQuery in Action, 3rd edition