Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Answers Viewqa Framework Pojo and java beans
Login         

View Questions and Answers by Category

Advertisements


 
Have Programming Question? Ask it here!
 
 
 


pradeep Kundu
Pojo and java beans
1 Answer(s)      2 years and 6 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


Advertisement
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 Tutorials/Questions & Answers:
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
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
Advertisements
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 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
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
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 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
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 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
pojo generation - Hibernate
pojo generation  Can we create a pojo from a data of two different tables
Using Beans in JSP. A brief introduction to JSP and Java Beans.
USING BEANS IN JSP           Java Beans Java Beans are reusable components
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
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 Beans Books
Java Beans Books         Java Beans book review The book...)    Developing Java Beans Java
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
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
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
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
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 
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  
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
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
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
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  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
 

 

 

DMCA.com