
I want the code of fetch the data from the database & show in a jsp page using jsp:usebean in MVC model 2 architecture. Suppose after login a user can view his/her details which he entered at the time of registration.

Here is an application that retrieves the data from the database through java bean and display it in jsp page through servlet.
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>
For more information, visit the following link:

In the beandata.jsp you are using scriptlet tag(<% %>).But I am wanting this should be done using jsp:usebean..means scriptless jsp..How can I do that? Thanks in advance
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.