После входа в систему путем создания сеанса данные не отображаются на странице jsp, полученной из базы данных. - PullRequest
2 голосов
/ 30 апреля 2019

Я пытался перечислить данные, которые находятся в конкретной таблице в моей базе данных.Данные извлекаются только тогда, когда я просто выполняю конкретный сервлет.Он не получает этот список, когда я вошел в систему, создал сеанс и направлен на эту конкретную страницу JSP.Я получаю только пустую страницу.

Я пытался получить атрибут сеанса для этой страницы JSP, но он не работает

list-Medicine.jsp

<body>
<%
if(session.getAttribute("email")==null)
{
    response.sendRedirect("login.jsp");
}
%>


<div>
    <div id="wrapper">
        <div id="header">
            Welcome ${email}
            add medicine
        </div>
    </div>

<form action="UserLogoutControllerServlet">
  <input type="submit" value="Logout">
</form>

<div id="container">
    <div id="content">
    <table>
    <tr>
        <th>Name</th>
        <th>Amount</th>
        <th>Price</th>
        <th>Email of the user</th>
    </tr>   

    <c:forEach var="tempStudent"  items="${MEDICINE_LIST}">
    <tr>
        <td>${tempStudent.name}</td>
        <td>${tempStudent.amount}</td>
        <td>${tempStudent.price}</td>
        <td>${tempStudent.email}</td>
    </tr>
    </c:forEach>

    </table>
    </div>
</div>

</div>
</body>

MedicineControllerServlet.java

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

        try
        //list tje medicine...in MVC fashion
        {
            listMedicine(request,response);
        }
        catch(Exception e)
        {
            throw new ServletException(e);
        }


    }



    private void listMedicine(HttpServletRequest request, HttpServletResponse response) throws Exception {
        //get medicine from db util
        List<Medicine> medicine=medicineDbUtil.getMedicine();

        //add medicne from db util
        request.setAttribute("MEDICINE_LIST", medicine);

        //send to jsp view(view)
        RequestDispatcher dispatcher=request.getRequestDispatcher("/list-medicine.jsp");
        dispatcher.forward(request, response);

    }

MedicineDbUtil.java

public List<Medicine> getMedicine() throws Exception
    {
        List<Medicine> medicine=new ArrayList<>();

        Connection myConn=null;
        Statement myStmt=null;
        ResultSet myRs=null;

        try
        {
        //get a connection
        myConn = dataSource.getConnection();

        //create sql statement
        String sql="select name,amount,price,users_email from  medicine ";

        myStmt= myConn.createStatement();

        //execute query
        myRs=myStmt.executeQuery(sql);

        //process result set
        while(myRs.next())
        {

            //retrieve data from result set row
            //int id=myRs.getInt("id");
            String name=myRs.getString("name");
            int amount=myRs.getInt("amount");
            double price=myRs.getDouble("price");
            String email=myRs.getString("users_email");


            //create new medicine
            Medicine theMedicine=new Medicine(name,amount,price,email);


            //add it to list of medicine
            medicine.add(theMedicine);

        }
        return medicine;

        }finally
        {
            //close jdbc objects
            close(myConn,myStmt,myRs);
        }




    }

Я хочу, чтобы список данных, извлеченный из базы данных, отображался на странице jsp, когда я вошел в систему через сеанс

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...