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 calling bean in servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "calling bean in servlet" Watch "calling bean in servlet" New topic
Author

calling bean in servlet

deep raj
Ranch Hand

Joined: Dec 11, 2009
Posts: 186
i made a servlet page called first.java
in that page i used verfication code with which i have to do database connectivity for that i used bean page.
name of bean page is db.java which is in as package.
how can i call that bean into servlet page(first.java)


can we write <jsp:bean id="first" scope="application" class="as.db"> in servlet page
Nicola Garofalo
Ranch Hand

Joined: Apr 10, 2010
Posts: 308
In a Servlet a jsp tag? No, you can't.

You should rebuild your bean with data stored in an HttpSession object, for example.

Anyway i did not understand very well what you want to do:
why don't you try to post some code, so we can try to comment it together?

Bye,
Nicola
deep raj
Ranch Hand

Joined: Dec 11, 2009
Posts: 186
import java.io.*;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class firstservlet extends HttpServlet
{
public void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException
{
PrintWriter out;
response.setContentType("text/Html");
out=response.getWriter();
HttpSession session=request.getSession(true);

out.print("<jsp:useBean id='first' scope='application' class='as.db'>");
try
{


String t1=request.getParameter("user");

first.rs=first.st.executeQuery("select user,pass,emp_fname,emp_lname from employee_personal where user='"+t1+"'");
while(first.rs.next())
{
out.print(first.rs.getString(1);
}
}
catch(Exception w)
{
out.print(w);
}

}

}







this servlet file is not compiled due to following error
first is not identified

therefore i asked that is there any other method to calling servlet
Nicola Garofalo
Ranch Hand

Joined: Apr 10, 2010
Posts: 308
Ok, you are building line to line your response, you aren't actually using a jsp, so jsp tag are not even imaginable here.

You must initialize your object first of class as.db

The matter now is how your as.db is done.

Perhaps it could be enough something like this to initialize your bean:



In the constructor something could initialize your Connection and Statement objects that are properties of your bean. They are all you need to execute your query.

I should see how as.db works. Do you know it?

deep raj
Ranch Hand

Joined: Dec 11, 2009
Posts: 186
when i write this
as.db first=new as.db();

it shows error
as does not exists
Nicola Garofalo
Ranch Hand

Joined: Apr 10, 2010
Posts: 308
Ok, please can you tell me how do you initialize your as.db class?

When you know this you solved your problem


deep raj
Ranch Hand

Joined: Dec 11, 2009
Posts: 186
first is not an object it is an id of jsp:bean
so i think no need to create it like as
as.db first=new as.db()
Nicola Garofalo
Ranch Hand

Joined: Apr 10, 2010
Posts: 308
Ok if it's an application attribute, you get it in a servlet by your ServletContext

Try



if first is not null after that statement you got it from there.



Mark E Hansen
Ranch Hand

Joined: Apr 01, 2009
Posts: 650
amrita singhal wrote:i made a servlet page called first.java
in that page i used verfication code with which i have to do database connectivity for that i used bean page.
name of bean page is db.java which is in as package.
how can i call that bean into servlet page(first.java)


can we write <jsp:bean id="first" scope="application" class="as.db"> in servlet page


Your terminology is going to be confusing. There is no Servlet page. There is no bean page.
The Servlet is a Java Class. A Java Bean is a Java Class.

You cannot send JSP tags as content from within your Servlet. JSP tags are to be placed in JSP pages.

The page you wish to be used as your response should be a JSP page. Your servlet would then just forward to this JSP page.
The JSP page would access any scoped data which the servlet places in the session/request/page scope. This is where you should place an instance of your Java Bean.

For example:

- In your Servlet's doGet/doPost method, do any application logic, validation of request parameters, etc., then create an instance of your Java bean and place it in the page, request or session scope.
- The Servlet would then forward to the JSP page.
- The JSP page would access the information in the Java bean via tags. JSTL makes this real easy.

Good luck.
deep raj
Ranch Hand

Joined: Dec 11, 2009
Posts: 186
Mark E Hansen
thankx for your suggestion
i know that both servlet and bean are java files. but for describe my problem i used that words.

bytheway i used servlet for verify the data from database
so i write sql queries inside servlet.
now to run sql queries first make connection from database
that part i did in bean
so in servlet how can i run queries
deep raj
Ranch Hand

Joined: Dec 11, 2009
Posts: 186
Nicola Garofalo
i typed
as.db first = (as.db)getServletContext().getAttribute("first");
but still it shows package as not exists
Mark E Hansen
Ranch Hand

Joined: Apr 01, 2009
Posts: 650
amrita singhal wrote:Mark E Hansen
thankx for your suggestion
i know that both servlet and bean are java files. but for describe my problem i used that words.

Yes, and like I said, if you use the wrong names for things, you will confuse people that are trying to help you. Use the correct names and you won't have that problem.
amrita singhal wrote:bytheway i used servlet for verify the data from database
so i write sql queries inside servlet.
now to run sql queries first make connection from database
that part i did in bean
so in servlet how can i run queries

It sounds like you're asking how to execute your database queries from within the Servlet code. Is this what you mean?
It's done the same in the Servlet as it would be anywhere else. What kind of problem are you having?
Nicola Garofalo
Ranch Hand

Joined: Apr 10, 2010
Posts: 308
Hi amrita singha,
what is then the name of your bean class? In what package can you find it?
And when, in your code, are its fields filled to establish a connection with your database?

if you can, please post some code.

Thank you.
deep raj
Ranch Hand

Joined: Dec 11, 2009
Posts: 186
Nicola Garofalo
bean class name=db
it is stored in package=as
and in the db class i specify fields for database connection
means statement,connection,resultset objects
deep raj
Ranch Hand

Joined: Dec 11, 2009
Posts: 186
Mark E Hansen
ya i have that problem
but i already did database connection coding in bean
and queries will run in servlet
then how can we access those fields of database (resultset etc.) in servlet java file
Mark E Hansen
Ranch Hand

Joined: Apr 01, 2009
Posts: 650
amrita singhal wrote:Mark E Hansen
ya i have that problem
but i already did database connection coding in bean
and queries will run in servlet
then how can we access those fields of database (resultset etc.) in servlet java file


First, it sounds like you're not able to locate your bean class from within the Servlet code. This sounds like a packaging or classpath problem. If you can't create an instance of your bean class within the Servlet, then you need to fix that first. For example, can you
do this in your Servlet:
If not, then you need to fix that first.

Where is your db.class file located within your web application? It can be placed in a variety of different places depending on how you want to structure you web application, but one simple place would be in WEB-INF/classes/as. This way, the class file will be placed on the classpath for use at runtime.

Also, two minor points:

1. By convention, class names begin with a capital letter. Also, you should probably use a more meaningful name. If you must use "db", then use "Db" or "DB" instead. However, give some thought to what this bean really is. It should be a container for some domain data, like a User, or an Address, or a Widget, etc. Name it based on what it is. This is the "data" which you want to pass to the JSP (view) page.
If it is a utility class for accessing the database, then it shouldn't be a bean, it should be a utility class. I think you may be confused about this point, so ask if you need a clarification.

2. When you provide code (or listings of any sort) please UseCodeTags. You should edit your post above and add code tags around the source code listing there.
deep raj
Ranch Hand

Joined: Dec 11, 2009
Posts: 186
i already create instance of bean class
and my db.class file is inside \WEB-INF\classes\as
deep raj
Ranch Hand

Joined: Dec 11, 2009
Posts: 186
still it shows package as not exists
i tell the path of all files
1.) db.java file in bean folder
2.) db.class file in as folder(package)
now that file (in which i want to use db class members) is first.java
when i compile it ,shows as pack. not exists
i think it shows problem due to path of first.java
please tell me where i should save first.java(i already told all paths of files)
Mark E Hansen
Ranch Hand

Joined: Apr 01, 2009
Posts: 650
You need to have the directory which contains the 'as' directory on your build classpath. If you're building from the command line, you can set the CLASSPATH environment variable. If you're building from within an IDE, you can check with the IDE forum for help in configuring your particular IDE.
deep raj
Ranch Hand

Joined: Dec 11, 2009
Posts: 186
classpath already set as environment variable.
classpath- F:\SOFT\Tomcat 5.0\common\lib\servlet-api.jar
deep raj
Ranch Hand

Joined: Dec 11, 2009
Posts: 186
hi that problem is solved.
now it shows ClassNot FoundException
for creating instance of db class
as.db first=new as.db();
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: calling bean in servlet