2014-12-13 2 views
1

Я пытаюсь запустить свою программу, но кажется, что после ее отправки в сервлет она просто останавливается и не отправляется на следующую страницу, просто пустую страницу с URL-адресом PurchaseCreate. Мне нужна ваша помощь, пожалуйста.Не удается отправить на страницу jsp

Мой сервлет в папке контроллера, который находится под пакетами исходных текстов, JSP находится в веб-приложения -> Веб-страницы -> MemberAccess, HTML под веб-приложения -> Веб-страницы

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> 

ответ

0

Это потому, что вы вызываете диспетчер в блок try/catch. В блоке catch вы ничего не делаете, кроме как регистрируете исключение. Если вы переместите код с диспетчером из блока try/catch, он должен отправиться, даже если в нем есть ошибки. Измените свой код на

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

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

Однако никаких изменений я не вижу. Это еще пустая страница. – user3900009

+0

Какие изменения вы ищете? –

+0

Потому что я устанавливаю атрибуты и получаю атрибуты, поэтому, если он подключается к jsp, должна ли отображаться таблица? – user3900009

Смежные вопросы