Forums Register Login

access beans into servlets

+Pie Number of slices to send: Send
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....
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
hi import specific class like
import Bean.MyBean;
and make sure your beans or jar are added properly
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
I believe that OP's question was about deployment, not about beans how-to.
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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......
+Pie Number of slices to send: Send
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.
Could you hold this kitten for a sec? I need to adjust this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 1120 times.
Similar Threads
loading image in panel
404 error in spring 3.0 mvc.
referencing constant defined in another class and package
Consume web service in spring
Compiling class with package
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 04:56:34.