Java 无法分派到jsp页面
我试图运行我的程序,但似乎在它发布到servlet之后,它就停在那里,不会发送到下一个页面,只是一个带有PurchaseCreate上url的空白页面。我需要你的帮助 我的servlet位于源程序包下的controller文件夹下,JSP位于Web应用程序->Web页面->MemberAccess下,HTML位于Web应用程序->Web页面下 Servlet:Java 无法分派到jsp页面,java,jsp,servlets,requestdispatcher,Java,Jsp,Servlets,Requestdispatcher,我试图运行我的程序,但似乎在它发布到servlet之后,它就停在那里,不会发送到下一个页面,只是一个带有PurchaseCreate上url的空白页面。我需要你的帮助 我的servlet位于源程序包下的controller文件夹下,JSP位于Web应用程序->Web页面->MemberAccess下,HTML位于Web应用程序->Web页面下 Servlet: public class PurchaseCreate extends HttpServlet { @Persisten
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:
这是因为您正在try/catch块中调用调度程序。在catch块中,除了记录异常之外,您什么也不做。如果您将带有dispatcher的代码移出try/catch块,那么即使您有错误,它也应该被分派。使用更改代码 }捕获异常{ Logger.getLoggerPurchaseCreate.class.getName.logLevel.SEVERE,null,ex; } RequestDispatcher rd=request.getRequestDispatcher/MemberAccess/PurchaseCreateM.jsp; 请求、响应;
不过,我看不到任何变化。它仍然是一个空白页。您正在查看什么样的更改?因为我设置了属性并获取了属性,所以如果它连接到jsp,是否应该显示该表?不,您弄错了。如果您的代码在与数据库通信时抛出异常,则不会填充属性。@user3900009不要在JSP中使用Scriptlet。getter和setter不足以显示一个表,但是使用with EL可以用foreach标记填充它。
<% 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>