1

所以,我所做的是调用 servlet(purchasecreate.java),然后它应该将我重定向到 purchaseCreateM.jsp。我不知道出了什么问题,但每次我运行时,它都会将我重定向到 PurchaseCreate 的空白页面。因此,我取消了 RequestDispatcher 并转发以检查程序可以运行多远。该程序能够完全运行,但是当我重新设置调度程序时,它再次将我带到空白页面。任何人都可以帮助我吗?

购买创建.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;   

}

购买创建.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>

编辑(14 年 15 月 12 日):这是打印在服务器日志上的内容。

   [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)]]
4

1 回答 1

1

您在 JSP 中出现编译错误:jsp 中的第 20 行,“java.lang.Object 无法转换为双精度”

陈述

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

需要是,

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

对象不能直接类型转换为原始变量。改用包装类 Double

于 2014-12-14T17:27:21.583 回答