-3

Hello Java EE developers;

Please I need to have a list of object ( ArrayList ) from database in my JSP when I refresh the page, that mean without submitting the form. In my " myJsp.jsp " I have a select with options are the Id of all Employee in the database.

i.e -> I want when The page is opened I want see all Ids of Employee in the option without submitting the form ?

I could do it just by submitting the form to the servlet and the servlet return back the response (list of object ) .

Please is there a solution for that and how Could I solve it ?

Thank you in Advance

B.Bechir
  • 21
  • 1
  • 6
  • 1
    Hello. Java EE developers in 2017 doesn't use JSP. Give code with Your tries, not story – Jacek Cz Sep 23 '17 at 17:56
  • I just developping a small application. I want just to find the idea on how to proceed this. Should I use Ajax or any thing like it to retieve the data? – B.Bechir Sep 23 '17 at 18:06
  • Hard to uderstand You (without details and code). Probably "refresh without submit" is the simplest possible JSP case (in my understanding). SO require Your attempt – Jacek Cz Sep 23 '17 at 18:16
  • Exaclty, I want when I open the file I get all my employee in the DB ( ir -> no need to click button to get them ) . – B.Bechir Sep 23 '17 at 18:25
  • Vote down because of no attempt to resolve any small part of problem – Jacek Cz Sep 23 '17 at 19:01

2 Answers2

-1

Just write one service to get employee record and call that service from ajax in onload of JSP page.

$(document).ready(function(){
var emplist = [];
$.ajax({ url: "[API Service]",
        success: function(data){
           alert("done");
           emplist = data;
         // Here you got EMP ID List Obj
        }});
});

So you can use 'emplist' variable to display employee id(s)

-2

Need to write One Pojo class(employee class), to retrieve db data in JSP.

public class Employee {
    String id;
    String name;
    String mobileNo;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getMobileNo() {
        return mobileNo;
    }

    public void setMobileNo(String mobileNo) {
        this.mobileNo = mobileNo;
    }
}

JSP provide below:

<%@page import="java.sql.DriverManager"%>
<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.Statement"%>
<%@page import="java.sql.Connection"%>

<%
String id = request.getParameter("userId");
String driverName = "com.mysql.jdbc.Driver";
String connectionUrl = "jdbc:mysql://localhost:3306/";
String dbName = "dbname";
String userId = "root";
String password = "password";

try {
Class.forName(driverName);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}

Connection connection = null;
Statement statement = null;
ResultSet resultSet = null;
%>
<h2 align="center"><font><strong>Retrieve data from database in jsp</strong></font></h2>
<table align="center" cellpadding="5" cellspacing="5" border="1">
<%
try{ 
connection = DriverManager.getConnection(connectionUrl+dbName, userId, password);
statement=connection.createStatement();
String sql ="select `id`,`first_name`,`mobile_no` from `user_info` where `first_name` like '%Chettupalli%'";

List<Employee> empList = new ArrayList<Employee>();
Employee emp;
resultSet = statement.executeQuery(sql);
while(resultSet.next()){
    emp = new Employee();
    emp.setId(resultSet.getString("id"));
    emp.setName(resultSet.getString("first_name"));
    emp.setMobileNo(resultSet.getString("mobile_no"));
    empList.add(emp);
}

} catch (Exception e) {
e.printStackTrace();
}
%>
<form>
<!-- You can prepare your form based on your requirement-->
</form>
</table>

empList will gets prepared at the time of rendering this JSP. So you just relay on empList object to prepare your form with dynamic employee data.

I think this will be work for your requirement.

  • 1000 Thanks @Rajesh . Your answear was very helpful and It is very clear. But Im using Hibernate So Instead of opening and Executing the sql query in JSP , Could I using the hibernate in the sama jsp to access database directly ? Thanks – B.Bechir Sep 23 '17 at 22:01