-1

Hi how can I show the elements in my .jsp when I add something to my database. Here is my empservlet or the servlet of my code

    //empServlet
  package emppackage;
   import java.io.IOException;
   import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;



public class empServlet extends HttpServlet {

String forward;
//empDAO myempDAO;
private static final long serialVersionUID = 1L;
//private empDAO disempdao;

public empServlet() {
    super();

  // myempDAO= new empDAO();
}



protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {


}


protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    empServices empserv= new empServices();


    empserv.empadd(request.getParameter("eFname"),request.getParameter("eLname"),request.getParameter("eNameRes"),request.getParameter("eSerials"),request.getParameter("eJrss"),request.getParameter("eBand"),request.getParameter("eAcct"),request.getParameter("ePMPs"),request.getParameter("esJRSS"),request.getParameter("eOpenSeatDesc"),request.getParameter("eReqSkills"),request.getParameter("eReqBand"),request.getParameter("eDreject"),request.getParameter("eRreject"),request.getParameter("eDetActPlan"),request.getParameter("eDataComplete"),request.getParameter("eStat"));

    //doGet(request, response);
}

}

Here is my employee dao where it is at arraylist

         //empDAO
     public List<empGetSet> getAllEmp()
 {

List<empGetSet> empdetails = new ArrayList<empGetSet>();

try {

    Connection conn = getConnection();
    String showsql = "SELECT *FROM csemp;";
    PreparedStatement psread= conn.prepareStatement(showsql);
    ResultSet rsread = psread.executeQuery();

    while (rsread.next()) 
    {

        empGetSet readgetset = new empGetSet();

        readgetset.setFname(rsread.getString(1));
        readgetset.setLname(rsread.getString(2));
        readgetset.setNameRes(rsread.getString(3));
        readgetset.setSerials(rsread.getString(4));
        readgetset.setJrss(rsread.getString(5));
        readgetset.setBand(rsread.getString(6));
        readgetset.setAcct(rsread.getString(7));
        readgetset.setPMPs(rsread.getString(8));
        readgetset.setsJRSS(rsread.getString(9));
        readgetset.setOpenSeatDesc(rsread.getString(10));
        readgetset.setReqSkills(rsread.getString(11));
        readgetset.setReqBand(rsread.getString(12));
        readgetset.setDreject(rsread.getString(13));
        readgetset.setsRreject(rsread.getString(14));
        readgetset.setDetActPlan(rsread.getString(15));
        readgetset.setDataComplete(rsread.getString(16));
        readgetset.setStat(rsread.getString(17));


        empdetails.add(readgetset);
        psread.close();
        conn.close();
    }


}

Here is my created jsp

 <%@ page language="java" 
   contentType="text/html; 
  charset=ISO-8859-1"
  pageEncoding="ISO-8859-1"
   import = "thisPackage.GetSet"
    import = "thisPackage.LoginServlet"
    import = "thisPackage.LogoutServlet"
     %>

   <%
     response.setHeader("Cache-Control", "no-store, no-cache,         must-revalidate");
   response.setHeader("Pragma", "no-cache");

    %>


 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"     "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
   <title>Hi welcome</title>
    </head>
   <body>


<center>

    <h3>Hello </h3> 

</center>

   <form action ="LogoutServlet" method="post">

   <div align="right"><input type = "submit"  value= "Logout"></div>

 </form>

  <table border =1 cellpadding="18">
 <thead>
<tr>
         <th>     First Name</th>
         <th>      Last Name</th>
        <th>      Name of Resource (LN ID Format)</th>
        <th>      Serial Number</th>
        <th>     JRSS</th>
         <th>     Band</th>
         <th>    Account (Proposed)</th>
        <th>    PMP Seat</th>
        <th>    Seat JRSS</th>
        <th>    Open Seat Description</th>
        <th>     Required Skills</th>
        <th>    Required Band low/high</th>
        <th>    Date of Rejection</th>
        <th>Reason for Rejection</th>
        <th>Detailed Action Plan</th>
        <th>Target Date of Completion</th>
        <th>Status(Ongoing/Closed)</th>
        <th colspan=2>Do This</th>

   </tr>
  </thead>


   </table>
  <a href ="addDetails.jsp">Add Details</a>
 </body>
</html>

How can I make it show, I've found some resources but I cant somehow put that into my project because it would not fit the way I write my code.

Chiran K.
  • 406
  • 3
  • 16

1 Answers1

1

In your servlet, you can set the request attribute to your DAO query result object. And then loop through the same to print the data grid in your jsp page.

Servlet Code >>

protected void listEmployees(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    List<empGetSet> empdetails = employeeDAO.getAllEmp(); //or call employeeService layer which calls employeDAO
    request.setAttribute("empdetails", empdetails);
    //dispatcher forward code
}

JSP Code >>

<c:forEach var="emp" items="${empdetails}">
    <tr>
        <td><c:out value="${emp.id}" /></td>
        <td><c:out value="${emp.firstName}" /></td>
        <td><c:out value="${emp.lastName}" /></td>
        <!--- etc etc data -->      
    </tr>
</c:forEach>

NOTE: Please correct your DAO, you are closing PreparedStatement & Connection inside the while loop of resultset read. Take it out of while loop of rsread.next(). If you are using Java 1.7 and above, I always recommend to use try with resources, much cleaner & safer code.

UPDATE:

You have two options for JSP action mapping:

  1. Using Link
    <a href="/list">Show List</a> <!-- this will call doGet() method -->
  2. Using Form
    <form action="list" method="post">
            <input type="submit" value="Show List" /> <!-- this will call doPost() method -->
       </form>

You have two options at servlet side url mapping:

  1. Annotation (on Servlet class)

    @WebServlet("/employees") public class EmpServlet extends HttpServlet { /* code */ }

  2. Configuration (in web.xml)

    <servlet> <servlet-name>EmpServlet</servlet-name> <servlet-class>com.test.EmpServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>EmpServlet</servlet-name> <url-pattern>/employees</url-pattern> </servlet-mapping>

And finally your servlet code will look like this:

protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    doGet(request, response); //if form action chosen
}

// if using doGet() / href option
protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String action = request.getServletPath();

        //you can implement switch case here if having more possible actions/links on the page
        if(action.equals("/list")){
            List<empGetSet> empdetails = employeeDAO.getAllEmp(); //or call employeeService layer which calls employeDAO
            request.setAttribute("empdetails", empdetails);
            RequestDispatcher dispatcher = request.getRequestDispatcher("Employees.jsp");
            dispatcher.forward(request, response);
        }
}
Amith Kumar
  • 4,400
  • 1
  • 21
  • 28
  • thanks man, I've encountered this when I wrote ------> "var" does not support runtime expressions. I have already added this tag <%@ taglib uri="http://java.sun.com/jstl/core_rt" prefix="c"%> – Yves Francisco Jun 14 '18 at 05:38
  • Try JSTL1.1 taglib `<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> ` – Amith Kumar Jun 14 '18 at 05:47
  • how can i print the display when I click a button – Yves Francisco Jun 14 '18 at 06:53
  • I am guessing you are talking about printer print at client side, which directs that you have to use javascript for that. This [post](https://stackoverflow.com/questions/20101409/how-can-i-insert-a-print-button-that-prints-a-form-in-a-webpage) will assist you. – Amith Kumar Jun 14 '18 at 07:07
  • i mean when I click a button named "Show List" it will display the contents of each column from the database to the .jsp form – Yves Francisco Jun 14 '18 at 07:24