简体   繁体   中英

RequestDispatcher unable to fetch me to JSP page

So, what I did was call the servlet(purchasecreate.java) and then it should redirect me to purchaseCreateM.jsp. I don't know what's wrong but evventually everytime when I run, it will redirect me to a blank page of PurchaseCreate. So, I canceled out the RequestDispatcher and forward to check how far the program can run. The program are able to run fully but when I set dispatcher back, it brings me to the blank page again. Anyone can help me please ?

PurchaseCreate.java

 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 = "";
            stock.setStockid(request.getParameter("stockid"));
            String stockid = stock.getStockid();

            String purcdid = "";
            int quantity = 0 ;
            PrintWriter out = response.getWriter();
            try{
                       out.println(stockid);
            out.println("Hello");
            int idNum = GeneratePdid();
            if(idNum<10)
                 purcdid = "PD0000"+String.valueOf(idNum);
            else if(idNum<100)
                 purcdid = "PD000"+String.valueOf(idNum);       
            else if(idNum<1000)
                 purcdid = "PD00"+String.valueOf(idNum);
            else if(idNum<10000)
                 purcdid = "PD0"+String.valueOf(idNum);
            else if(idNum<100000)
                 purcdid = "PD"+String.valueOf(idNum);
            int PidNum = GeneratePid();
            if(PidNum<10)
                 purcid = "P000"+String.valueOf(PidNum);
            else if(PidNum<100)
                 purcid = "P00"+String.valueOf(PidNum);       
            else if(PidNum<1000)
                 purcid = "P0"+String.valueOf(PidNum);
            else if(PidNum<10000)
                 purcid = "P"+String.valueOf(PidNum);

            out.println("Hello");
            out.println(stockid);
            stock.setStockid(stockid);
            out.println(purcid);
            purc.setPurchaseid(purcid);
            out.println(purcdid);


            out.println("Hello");
            quantity = Integer.parseInt(request.getParameter("quantity"));
            out.println(quantity);
            Purchasedetails purchased = new Purchasedetails(purcdid,quantity,stock,purc);
            out.println(stock);
            out.println(purc);         
            Query query = em.createNamedQuery("Purchasedetails.findAll");
            List<Purchasedetails> listDetails = query.getResultList();

             Query query2 = em.createNamedQuery("Stocks.findAll");
               List<Stocks> stockPrice = query2.getResultList();
               double stockPriceReal = 0; 
     for(int i = 0 ; i <stockPrice.size(); i++){
         String stockid2 = stockPrice.get(i).getStockid();
         if(stockid2.equals(stockid))
         {
          stockPriceReal = Double.parseDouble(stockPrice.get(i).getStockprice().toString());
         }
     }
            out.print(stockPriceReal);
            HttpSession session = request.getSession();
            request.setAttribute("listDetails",listDetails);
            request.setAttribute("stockPriceReal",stockPriceReal);
            out.print(stockPriceReal);
           //  response.sendRedirect("MemberAccess/PurchaseCreateM.jsp");
            RequestDispatcher rd = request.getRequestDispatcher("MemberAccess/PurchaseCreateM.jsp");
            rd.forward(request, response);

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


    }


public int GeneratePid(){  

            Query query = em.createNamedQuery("Purchase.findAll");
            List<Purchase> purchaseList = query.getResultList();
            String lastId = "";

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

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


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

}

PurchaseCreate.jsp

<%@page import="java.util.ArrayList"%>\
<%@page import="model.*"%>
<%@page import="java.util.List"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>


    <html>
        <head>

            <title>Cart</title>
    </head>
    <% List<Purchasedetails> list = (List<Purchasedetails>)request.getAttribute("listDetails"); %>


     <% double StockPrice = (double)request.getAttribute("stockPriceReal"); %>
     <%!  double stockArray[]; %>
     <%     if(stockArray.length == 0)
        stockArray[0] = StockPrice;
        else
         for(int i = 0; i <stockArray.length;i++)
         stockArray[i] = StockPrice;
                %>

     <% 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"+ String.valueOf(pDIDno);       
            else if(pDIDno<1000)
                 pDIDreal = "PD00"+ String.valueOf(pDIDno);
            else if(pDIDno<10000)
                 pDIDreal = "PD0"+ String.valueOf(pDIDno);
            else if(pDIDno<100000)
                 pDIDreal = "PD"+String.valueOf(pDIDno);
                 Purchasedetails pd = new Purchasedetails(pDIDreal);
               // list.get(list.size()-1).setPurchasedetailid(pDIDreal);
                  list.set(list.size()-1, pd);
     %>
   <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 = 0; 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><%= stockArray[i] %></td>
                </tr>
                <% i++;%>
                <% } %>
            </tbody>
        </table>
     </body>
</html>

EDIT (15/12/14): This is what printed on the server log.

   [2014-12-15T01:07:06.526+0800] [glassfish 4.0] [SEVERE] [] [] [tid: _ThreadID=107 _ThreadName=Thread-4] [timeMillis: 1418576826526] [levelValue: 1000] [[
  org.apache.jasper.JasperException: PWC6033: Error in Javac compilation for JSP

PWC6199: Generated servlet error:
source value 1.5 is obsolete and will be removed in a future release

PWC6199: Generated servlet error:
target value 1.5 is obsolete and will be removed in a future release

PWC6199: Generated servlet error:
To suppress warnings about obsolete options, use -Xlint:-options.

PWC6197: An error occurred at line: 20 in the jsp file: /MemberAccess/PurchaseCreateM.jsp
PWC6199: Generated servlet error:
incompatible types: java.lang.Object cannot be converted to double

PWC6199: Generated servlet error:
/PurchaseCreateM_jsp.java uses unchecked or unsafe operations.

PWC6199: Generated servlet error:
Recompile with -Xlint:unchecked for details.


    at org.apache.jasper.compiler.DefaultErrorHandler.javacError(DefaultErrorHandler.java:129)
    at org.apache.jasper.compiler.ErrorDispatcher.javacError(ErrorDispatcher.java:299)
    at org.apache.jasper.compiler.Compiler.generateClass(Compiler.java:392)
    at org.apache.jasper.compiler.Compiler.compile(Compiler.java:453)
    at org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:625)
    at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:375)
    at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:473)
    at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:377)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:790)
    at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1682)
    at org.apache.catalina.core.ApplicationDispatcher.doInvoke(ApplicationDispatcher.java:875)
    at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:739)
    at org.apache.catalina.core.ApplicationDispatcher.processRequest(ApplicationDispatcher.java:575)
    at org.apache.catalina.core.ApplicationDispatcher.doDispatch(ApplicationDispatcher.java:546)
    at org.apache.catalina.core.ApplicationDispatcher.dispatch(ApplicationDispatcher.java:428)
    at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:378)
    at controller.PurchaseCreate.doPost(PurchaseCreate.java:106)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:707)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:790)
    at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1682)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:318)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:160)
    at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:734)
    at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:673)
    at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:99)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:174)
    at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:357)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:260)
    at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:188)
    at org.glassfish.grizzly.http.server.HttpHandler.runService(HttpHandler.java:191)
    at org.glassfish.grizzly.http.server.HttpHandler.doHandle(HttpHandler.java:168)
    at org.glassfish.grizzly.http.server.HttpServerFilter.handleRead(HttpServerFilter.java:189)
    at org.glassfish.grizzly.filterchain.ExecutorResolver$9.execute(ExecutorResolver.java:119)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:288)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:206)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:136)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:114)
    at org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:77)
    at org.glassfish.grizzly.nio.transport.TCPNIOTransport.fireIOEvent(TCPNIOTransport.java:838)
    at org.glassfish.grizzly.strategies.AbstractIOStrategy.fireIOEvent(AbstractIOStrategy.java:113)
    at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.run0(WorkerThreadIOStrategy.java:115)
    at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.access$100(WorkerThreadIOStrategy.java:55)
    at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy$WorkerThreadRunnable.run(WorkerThreadIOStrategy.java:135)
    at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:564)
    at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:544)
    at java.lang.Thread.run(Thread.java:745)]]

You have a compilation error in JSP at line: 20 in the jsp, "java.lang.Object cannot be converted to double"

Statement

double StockPrice = (double)request.getAttribute("stockPriceReal");

needs to be,

double StockPrice = (Double)request.getAttribute("stockPriceReal");

Object cannot be directly type cast to a primitive variable. Use wrapper class Double instead

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM