File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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 Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "access beans into servlets" Watch "access beans into servlets" New topic
Author

access beans into servlets

rashmi kulkarni
Greenhorn

Joined: May 24, 2013
Posts: 2
in my web based java project, i have the following file structure in tomacat7
webapps
------------code
-----------------web-inf
----------------------classes
----------------------lib
----------------------src
-------------------------Bean
------------------------------MyBean.class
-------------------------Servlets
------------------------------ChangePassword.java



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 ChangePassword.java.

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

please help..........
thanQ....
Devaka Cooray
ExamLab Creator
Saloon Keeper

Joined: Jul 29, 2008
Posts: 2885
    
  13

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
My Home Page -- Twitter Profile -- JavaRanch FAQ -- How to Ask a Question
bharat salunkhe
Greenhorn

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
Greenhorn

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.

Regards,
Ronald Reagan Jr.
Devaka Cooray
ExamLab Creator
Saloon Keeper

Joined: Jul 29, 2008
Posts: 2885
    
  13

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

Joined: May 28, 2013
Posts: 7
Rashmi,

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

While deploying in tomcat,

Please make the following folder structure.

$Tomcat_Root\webapps
+<all jsp files>
+<WEB-INF>
-------- +classes
---------- +Bean
------------- +MyBean.class
-------- +Servlets
------ +web.xml
+<META-INF>

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

Regards,
Ronald
rashmi kulkarni
Greenhorn

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....
Thanks.....
I get back to you soon......
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60071
    
  65

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] [Bear's FrontMan] [About Bear] [Books by Bear]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: access beans into servlets
 
Similar Threads
Compiling class with package
loading image in panel
Consume web service in spring
404 error in spring 3.0 mvc.
referencing constant defined in another class and package