我试图运行我的程序,但似乎在它发布到servlet之后,它就停在那里并且不会发送到下一页,只是一个带有PurchaseCreate上的url的空白页面。我需要你的帮助。
我的servlet位于源包下的控制器文件夹下,JSP位于Web Application下 - >网页 - > MemberAccess,HTML在Web Application下 - >网页
的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">
</p>
<p>Quantity :
<input type="text" name="quantity">
</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 :(得分:0)
这是因为你在try / catch块中调用dispatcher。在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);