Can't dispatch to jsp page

340 Views Asked by At

I'm trying to run my program but it seems that after it post to servlet, it just stops there and won't dispatch to the next page, just a blank page with the url on PurchaseCreate. I need your help please.

My servlet is under controller folder that is under source packages, JSP is under Web Application -> Web Pages -> MemberAccess, HTML is under Web Application -> Web Pages

Servlet:

   public class PurchaseCreate extends HttpServlet {

    @PersistenceContext
    EntityManager em;
    @Resource
    UserTransaction utx;
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
         Members member = new Members();
            Stocks stock = new Stocks();
            Purchase purc = new Purchase();
            String purcid = null;
            String stockid = null;
            String purcdid = null;
            int quantity = 0 ;
            try (PrintWriter out = response.getWriter()) {


            int idNum = GeneratePdid();
            if(idNum<10)
                 purcdid = "PD0000"+idNum;
            else if(idNum<100)
                 purcdid = "PD000"+idNum;       
            else if(idNum<1000)
                 purcdid = "PD00"+idNum;
            else if(idNum<10000)
                 purcdid = "PD0"+idNum;
            else if(idNum<100000)
                 purcdid = "PD"+idNum;
            int PidNum = GeneratePid();
            if(PidNum<10)
                 purcid = "P000"+PidNum;
            else if(PidNum<100)
                 purcid = "P00"+PidNum;       
            else if(PidNum<1000)
                 purcid = "P0"+PidNum;
            else if(PidNum<10000)
                 purcid = "P"+PidNum;


            stockid = request.getParameter("stockid");
            stock.setStockid(stockid);
            purc.setPurchaseid(purcid);
            List<Stocks> listPrice = getStock(stockid);
            quantity = Integer.parseInt(request.getParameter("quantity"));
            Purchasedetails purchased = new Purchasedetails(purcdid,quantity,stock,purc);

            HttpSession session = request.getSession();
            session.setAttribute("purchased", purchased);
            session.setAttribute("listPrice", listPrice);

            RequestDispatcher rd = request.getRequestDispatcher("/MemberAccess/PurchaseCreateM.jsp");
               rd.forward(request, response);

        } catch (Exception ex) {
            Logger.getLogger(PurchaseCreate.class.getName()).log(Level.SEVERE, null, ex);
        } 
    }
 public List<Stocks> getStock(String stockid){
     Query query = em.createQuery("SELECT * FROM Stocks s WHERE s.stockid = :stockid").setParameter("stockid", stockid);
     List<Stocks> stockPrice = query.getResultList();
        return  stockPrice;
 }

public int GeneratePid(){  
            Query query = em.createNamedQuery("Purchase.findAll");
            List<Purchase> purchaseList = query.getResultList();
            String lastId = null;

                    if(!purchaseList.isEmpty()){
                        lastId = purchaseList.get(purchaseList.size()-1).getPurchaseid();
                    }
                String subString = lastId.substring(1,4);
                int realId = Integer.parseInt(subString) +1;
            return realId;   
         }

public int GeneratePdid(){
    Query query = em.createNamedQuery("Purchasedetals.findAll");
    List<Purchasedetails> purchaseDetailsL = query.getResultList();
    String lastId = null;


                    if(!purchaseDetailsL.isEmpty()){
                        lastId = purchaseDetailsL.get(purchaseDetailsL.size()-1).getPurchasedetailid();
                    }
                String subString = lastId.substring(2,6);
                int realId = Integer.parseInt(subString) +1;
            return realId;   

}

HTML :

  <h1>Purchase</h1>
    <form  action="../PurchaseCreate" method="post">

        <p>Please enter the fields below to make your purchase</p>
        <p>
            Stock ID :
            <input type ="text" name ="stock">&nbsp;
        </p>
        <p>Quantity :
            <input type="text" name="quantity">&nbsp;
        </p>
            <input type="submit" name="create" class ="button" value="Add into cart">

    </form> 

JSP :

 <% List<Purchasedetails> list = (List<Purchasedetails>)request.getAttribute("purchased"); %>
     <% List<Stocks> listPrice = (List<Stocks>)request.getAttribute("listPrice"); %>
     <% int size= list.size(); %>
     <%! String pDID = "";%>
     <%! int pDIDno = 0; %>
     <%! String pDIDSub = ""; %>
     <%! String pDIDreal = ""; %>
     <body>
          <% if (size != 1){ %>
     <% pDID = list.get(list.size()-1).getPurchasedetailid(); %>
     <% pDIDSub = pDID.substring(2, 6); %>
     <% pDIDno = Integer.parseInt(pDIDSub) + (size-1);}%>
     <% if(pDIDno<10)
                 pDIDreal = "PD0000"+pDIDno;
            else if(pDIDno<100)
                 pDIDreal = "PD000"+pDIDno;       
            else if(pDIDno<1000)
                 pDIDreal = "PD00"+pDIDno;
            else if(pDIDno<10000)
                 pDIDreal = "PD0"+pDIDno;
            else if(pDIDno<100000)
                 pDIDreal = "PD"+pDIDno;

                  list.get(list.size()-1).setPurchasedetailid(pDIDreal);
     %>
   <table border="1">
            <thead>
                <tr>
                    <th>No.</th>
                    <th>Purchase Details ID</th>
                    <th>Stock ID</th>
                    <th>Quantity</th>
                    <th>Price</th>
                </tr>
            </thead>
            <tbody>

                <% for (int i = 1; i <list.size(); i++) { %>

                <tr>
                    <td><%= i-1 %></td>
                    <td><%= list.get(i).getPurchasedetailid() %></td>
                    <td><%= list.get(i).getStockid()  %></td>
                    <td><%= list.get(i).getOrderqty() %></td>
                    <td><%= listPrice.get(i).getStockprice() %></td>
                </tr>
                <% i++;%>
                <% } %>
            </tbody>
        </table>
     </body>
1

There are 1 best solutions below

5
On

This is because you are invoking dispatcher in try/catch block. In the catch block you are doing nothing but logging the exception. If you move a code with dispatcher out of the try/catch block then it should dispatch even if you has errors in it. Change your code with

} catch (Exception ex) {
    Logger.getLogger(PurchaseCreate.class.getName()).log(Level.SEVERE, null, ex);
} 

RequestDispatcher rd = request.getRequestDispatcher("/MemberAccess/PurchaseCreateM.jsp");
   rd.forward(request, response);