Home Answers Viewqa Framework Pojo and java beans

 
 


pradeep Kundu
Pojo and java beans
1 Answer(s)      11 months ago
Posted in : Framework

Can anyone tell me where we should use pojo class and where we should use beans. We use pojo class to make the object of mapping file in hibernate framework. Can I use pojo class in those applications where I don't use any database and how ? Please give any example. Can I use pojo class in any simple application ? Can I use pojo class in struts application in which I don't use Hibernate.

Thanks in advance

View Answers

June 8, 2012 at 5:18 PM


Apart from Hibernate, you can use POJO class with JSP and Servlets also. Here is an example of POJO class which is connected to database and store data into arraylist. The Servlet class this POJO class and store the list into request object using setAttribute() method. This request is then send to jsp page. Using the getAttribute() method, the jsp page get the data and display it in the on the browser.

1)EmpBean.java:

package form;
import java.sql.*;
import java.util.*;
public class EmpBean {

public List dataList(){
ArrayList list=new ArrayList();
try{
     Class.forName("com.mysql.jdbc.Driver");
           Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "root");
           Statement st=con.createStatement();
           ResultSet rs=st.executeQuery("select * from employee");
           while(rs.next()){
               list.add(rs.getString("name"));
               list.add(rs.getString("address"));
               list.add(rs.getString("contactNo"));
               list.add(rs.getString("email"));

}
}
catch(Exception e){}
return list;

}
}

2)BeanInServlet.java:

import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class BeanInServlet extends HttpServlet{
    protected void doGet(HttpServletRequest req, HttpServletResponse res)
        throws ServletException, IOException{
        form.EmpBean p = new form.EmpBean();
        List list=p.dataList();

        req.setAttribute("data", list);
        RequestDispatcher rd = req.getRequestDispatcher("/jsp/beandata.jsp");
        rd.forward(req, res);
    }
}

3)beandata.jsp:

<%@page language="java" import="java.util.*" %>
<html>
<body> 
<table border="1" width="303">
<tr>
<td width="119"><b>Name</b></td>
<td width="168"><b>Address</b></td>
<td width="119"><b>Contact no</b></td>
<td width="168"><b>Email</b></td>
</tr>
<%Iterator itr;%>
<% List data= (List)request.getAttribute("data");
for (itr=data.iterator(); itr.hasNext(); ){
%>
<tr>
<td width="119"><%=itr.next()%></td>
<td width="168"><%=itr.next()%></td>
<td width="168"><%=itr.next()%></td>
<td width="168"><%=itr.next()%></td>
</tr>
<%}%>
</table>
</body>
</html>









Related Pages:
Pojo and java beans
Pojo and java beans  Can anyone tell me where we should use pojo class and where we should use beans. We use pojo class to make the object of mapping file in hibernate framework. Can I use pojo class in those applications where
Introduction to POJO (Plain Old Java Object) Programming Model
Introduction to POJO (Plain Old Java Object) Programming Model... features of added by Sun Microsystems in EJB 3.0 is POJO (Plain Old Java.... Therefore, all normal Java objects are POJO?s only. The following classes
Introduction to POJO (Plain Old Java Object) Programming Model
Introduction to POJO (Plain Old Java Object) Programming Model... features of added by Sun Microsystems in EJB 3.0 is POJO (Plain Old Java Object..., all normal Java objects are POJO?s only. The following classes
Java Beans
Java Beans  hii What is Java Beans?   hello,, A Java Bean is a reusable software component that can be manipulated visually in a builder tool
Hibernate create POJO classes
Hibernate create POJO classes In this tutorial you will learn how to create a POJO class in Hibernate. POJO (Plain Old Java Object) is a programming model... be a default constructor in a POJO class that are instantiated
net beans
net beans  Write a JAVA program to read the values of an NxN matrix and print its inverse
Enterprise Beans
Enterprise Beans       Enterprise beans are the Java EE server side components that run inside the ejb container... by many users. Enterprise beans are used to perform various types of task like
net beans
net beans  Write a JAVA program to parse an array and print the greatest value and the number of occurrences of that value in the array. You can initialize the array random values in the program without the need to read them
net beans
net beans  Write a JAVA program to validate the credit card numbers using Luhn Check algorithm. You will need to search the Internet to understand how the algorithm works.   Hi Friend, Try the following code: import
net beans
net beans  Write a JAVA program to auto-grade exams. For a class of N students, your program should read letter answers (A, B, C, D) for each student. Assume there are 5 questions in the test. Your program should finally print
java beans - Java Beginners
java beans  how and where are java beans implemented  Hi Friend, Please visit the following links: http://www.roseindia.net/jsp/using-bean-counter.shtml http://www.roseindia.net/jsp/java-bean-example-jsp.shtml
EJB,java beans
EJB,java beans  What is EJB poles,mainfest,jar files? What is nongraphical bean? Please send me as notes
java beans - JSP-Servlet
java beans  how to get the values from this page to java page bcoz i want to insert these values into table Reserved By: Conference Hall No: Select Hall Numbers
Java Beans pls!! - Java Beginners
Java Beans pls!!  hello,, can someone give me a simple program of java beans, and Jsp. pls.. im begging you guys.. i really need it!! plss.. thanks...://www.roseindia.net/jsp/java-bean-example-jsp.shtml http://www.roseindia.net/jsp
java beans code in jsp - WebSevices
java beans code in jsp  can you tell me how to use java beans in jsp in step wise. iam doing online banking project. i need java beans code... summary page and funds transfer page using java beans.   Hi Friend
net beans 4
net beans 4  Write a JAVA program to read an initial two number x1 and x2, and determine if the two numbers are relatively prime. Two numbers are relatively prime. Two numbers are relatively prime if the only common factor
Enterprise Beans
Enterprise Beans       Enterprise beans are the Java EE server side components that run inside the ejb container... by many users. Enterprise beans are used to perform various types of task like
calling java beans - JSP-Servlet
calling java beans  Sir, I want to know where to place the java beans java file and class file inside tomcat web server. and how to call them from jsp file.  Hi Friend, Java Bean is placed in classes\form
How to access Enterprise Beans
JNDI lookup : java:global :- Used to find out the remote enterprise beans. java... ). java:module :- use to find out the local enterprise beans inside the same... bean. java:app :- used to find out the local enterprise beans which
How to use Java Beans in JSP?
How to use Java Beans in JSP?  Hi, I have developed a employee bean in Java: public class Employee{ private String name; private String code; ..... } Please tell me how I can use Employee Java bean in JSP page? Thanks
Introduction To Enterprise Beans
Introduction To Enterprise Beans In this tutorial you will learn about the Enterprise Beans. Enterprise Bean :- A bean that is a component of Java Enterprise... is called Enterprise Beans. An Enterprise Beans is a server side component
jsp using Beans
(req, res); } } 3)beandata.jsp: <%@page language="java" import
Message Driven Beans
Message Driven Beans       Message driven beans are the light weight components used for communication. In message driven beans the messaging service is in asynchronous mode because
Types of Enterprise Beans
Types of Enterprise Beans In this section we will discuss about the types of Enterprise Beans. Summarily there are two types of Enterprise Beans... discussing these beans in detail Session Bean A Session bean is a non persistent
Using Beans in JSP. A brief introduction to JSP and Java Beans.
USING BEANS IN JSP           Java Beans Java Beans are reusable components
jsp,beans,jdbc - JSP-Servlet
jsp,beans,jdbc  I have created jsp page and corresponding servlet java class.In servlet i write query for retriving data and sum of data from database for specific date .this data is retrived on jsp page by beans.i use beans
pojo generation - Hibernate
pojo generation  Can we create a pojo from a data of two different tables
Writing Java Object (POJO)
Writing Java Object (POJO)     ... in the project\WEB-INF\src\java\roseindia\dao\hibernate directory.  POJO Class Here... into the database and we need one POJO class and the hibernate mapping file to map
Register page using servlets,jsp and java beans
Register page using servlets,jsp and java beans  i want code for register page using jsp,serlets and java beans.iam getting error for my code in java beans pls help me   Hi Friend, Please visit the following links
Java Beans Books
Java Beans Books         Java Beans book review The book...)    Developing Java Beans Java
pojo class having problem
pojo class having problem  Cannot find any information on property 'Username' in a bean of type 'LoginBean.LoginBeanClass' org.apache.jasper.runtime.JspRuntimeLibrary.handleSetProperty(JspRuntimeLibrary.java:664
java Using net beans - Java Beginners
java Using net beans  Dear Sir, I just like to ask why i cant add a data to the answer you have given me last time where i have to pick the data of the student like id , name , class , age & address using netbeans jframe then i
java Using net beans - Java Beginners
java Using net beans  Dear Sir, I just like to ask why i cant add a data to the answer you have given me last time where i have to pick the data of the student like id , name , class , age & address using netbeans jframe then i
java Using net beans - Java Beginners
java Using net beans  Dear Sir, I just like to ask why i cant add a data to the answer you have given me last time where i have to pick the data of the student like id , name , class , age & address using netbeans jframe then i
Java programming or net beans - Java Beginners
Java programming or net beans  Help with programming in Java? Modify the dog class to include a new instance variable weight (double) and the Cat class to include a new instance variable coatColor (string). Add the corresponding
Error in using java beans - JSP-Servlet
Error in using java beans  I am getting the following error when I run the jsp code. type Exception report message description The server... to place the java file, then where should I place it?  Hi Friend
Message Driven Beans
Message Driven Beans       Message driven beans are the light weight components used for communication. In message driven beans the messaging service is in asynchronous mode because
How to display data in jsp from dao using java beans?
How to display data in jsp from dao using java beans?  Hi I need to display data in jsp pulling from dao using java beans, Please can anyone give me the sample application with above topics. Any help would be highly appreciated
what is difreence between javabeans and enterprise beans - EJB
what is difreence between javabeans and enterprise beans  what is difreence between javabeans and enterprise beans  Hi Friend, Differences: 1)Java Beans are used for building extendable components and contain
displaying created images with java beans - Java Server Faces Questions
displaying created images with java beans  hello I am sorry I think I have to elaborate my previous message. I had used image tag of jsf. I... in backing beans displays nothing. what should I do ? regards 
Stateful Session Beans Example, EJB Tutorial
;    In this part of Enterprise Session Beans, you will learn how to develop, deploy, and run a simple Java EE application named account using...) The Business Interface: The Account business interface is a plain Java interface
PROJECT ON JAVA NET BEANS AND MYSQL !! PLEASE HELP URGENT
PROJECT ON JAVA NET BEANS AND MYSQL !! PLEASE HELP URGENT  i need a project based on connectivity..it can be based on any of the following topics...://www.roseindia.net/tutorial/java/swing/studentApplication.html
Diff Bn Actionform and Pojo - Struts
Diff Bn Actionform and Pojo   Hi Friends, I am confused with Pojo and ActionForm ,bcoz both have getter and setter methods.In both we declare fields and corresponding getter n setters also.plz  
hibernate pojo setter method problem
hibernate pojo setter method problem  how to pass a date type variable to a setter method in hibernate pojo class? //this is my pojo class import java.util.*; public class Employee { private String empname; private int
EJB, Enterprise java bean- Why EJB (Enterprise Java Beans)?
Why EJB (Enterprise Java Beans)?        Enterprise Java Beans or EJB for short is the server-side component architecture for the Java 2 Platform
Hibernate Annotations
the metadata is clubbed into the POJO java file along with the code this helps... table. Most classes will also have a Java-Beans-style property holding the unique... a metadata to govern the transformation of data from POJO to database tables
Java Session Beans
Java Session Beans      ... or database access, for the client request. Session beans are divided into two parts: stateless and stateful. Stateless session beans do not use the class
net beans
net beans  how to calculate electric bill,units,load in net beans and how to insert this calculated bill ,unit and load into database and how this calculate value comes on GUI
net beans
net beans  how to calculate electric bill,units,load in net beans and how to insert this calculated bill ,unit and load into database and how this calculate value comes on GUI

Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.