Http状态404 - 未找到但可以中途运行

时间:2014-12-15 15:30:21

标签: java jsp servlets http-status-code-404

好的,这是问题所在。每当我运行PurchaseM.jsp时,当我重定向到PurchaseCheckOut.java时,我将获得http status 404 - not found。我不知道为什么会这样。如果我从PurchaseCart.jsp开始,我已经有会话值(所以没有空例外),那么它将运行。有人帮我请。

PurchaseM.jsp

 <%@ taglib prefix = "c" uri = "http://java.sun.com/jsp/jstl/core" %>
    <%@ taglib prefix="sql" uri="http://java.sun.com/jsp/jstl/sql" %>

<html>
    <head>
        <title>Purchasing</title>

    </head>
    <body>


        <h1>Purchase</h1>
    <sql:setDataSource url="jdbc:derby://localhost:1527/healthdb;user=nbuser;password=nbuser" driver="org.apache.derby.jdbc.ClientDriver"  
                       var="Derbydata"/>  
<sql:query sql=" SELECT stockid FROM STOCKS" dataSource="${Derbydata}" var="STOCKS" />

        <form  action="../PurchaseCreate" method="post">
         <p>Please enter the fields below to make your purchase</p>

    <strong>Select a product :</strong>
    <select name="stockid">
     <c:forEach var="row" items="${STOCKS.rowsByIndex}">
      <option><c:out value="${row[0]}"/></option>
     </c:forEach>
    </select>
    </p>
                <p><strong>Quantity</strong> :
                <input type="text" name="quantity">&nbsp;
            </p>
                <input type="submit" name="create" class ="button" value="Add into cart">

        </form> 

    </body>
</html>

PurchaseCreate.java

package controller;

import entity.Login;
import entity.LoginPK;
import entity.Purchasedetails;
import entity.Members;
import entity.Purchase;
import entity.Stocks;
import java.io.*;
import java.math.BigDecimal;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.Random;
import java.util.logging.*;
import javax.annotation.Resource;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.*;
import javax.transaction.UserTransaction;
import model.PurchaseService;

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();
            PurchaseService ps = new PurchaseService(em);
            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);  

            double stockPriceReal = getStock(stockid);
            out.print(stockPriceReal);
            HttpSession session = request.getSession();
            session.setAttribute("purchased",purchased);
            session.setAttribute("stockPriceReal",stockPriceReal);
            out.print(stockPriceReal);

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


      } catch (Exception ex) {
  ex.printStackTrace();
} 
            finally{
                out.close();
            }


    }

public int GeneratePid(){  
            Query query = em.createNamedQuery("Purchase.findAll");
            List<Purchase> purchaseList = query.getResultList();
            String lastId = "";
               int realId = 0;
                    if(!purchaseList.isEmpty()){
                        lastId = purchaseList.get(purchaseList.size()-1).getPurchaseid();
                    String subString = lastId.substring(1,5);
                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,7);
                int realId = Integer.parseInt(subString) +1;
            return realId;   

}

    private double getStock(String stockid) {
       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());
         }
     }  return stockPriceReal;
}

}

PurchaseCart.jsp

<%@page import="javax.persistence.Query"%>
<%@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>
     <body>

      <%!List<Double> stockArray = new ArrayList<Double>() ;%>

     <%!List<Object> list1 = new ArrayList<Object>();%>

     <% Object o = session.getAttribute("purchased"); 
     list1.add(o); 
     int size = list1.size(); 
     double stockPrice = (Double)session.getAttribute("stockPriceReal");

      if(stockArray.size() == 0)
      { 
         stockArray.add(stockArray.size(),stockPrice);
      } 
      else {

stockArray.add(stockArray.size(), stockPrice);  }  

     %>

   <table border="1">
            <thead>
                <tr>
                    <th>No.</th>
                    <th>Purchase Details ID</th>
                    <th>Quantity</th>
                    <th>Stock ID</th>
                    <th>Price</th>
                </tr>
            </thead>
            <tbody>

                    <% for (int g = 0; g < size; g ++) { %>
                <tr> 
                    <%! String finalSubString1 = "";
                    int subInt = 0;
                    int subIntP = 0; %>
                   <% String toString = list1.get(g).toString(); 
                    String subString1 = toString.substring(2,7);

                    subInt = Integer.parseInt(subString1)+ g;
                  if(subInt<10)
                 finalSubString1 = "PD0000"+String.valueOf(subInt);
            else if(subInt<100)
                 finalSubString1 = "PD000"+String.valueOf(subInt);       
            else if(subInt<1000)
                 finalSubString1 = "PD00"+String.valueOf(subInt);
            else if(subInt<10000)
                 finalSubString1 = "PD0"+String.valueOf(subInt);
            else if(subInt<100000)
                 finalSubString1 = "PD"+String.valueOf(subInt);

                    String subString2 = toString.substring(7,9);
                    String subString3 = toString.substring(9,15);
                    String subString4 = toString.substring(15,19);
                   %>
                    <td><%= g +1 %></td>
                    <td><%= finalSubString1 %></td>
                    <td><%= subString2 %></td>
                    <td><%= subString3 %></td>
                    <td><%= stockArray.get(g).toString() %></td>



                </tr>

                <% } %>
            </tbody>
        </table> 
            <% session.setAttribute("stockArray",stockArray);
                session.setAttribute("list1",list1);%>

                <form action = "../PurchaseUpdateM" > 
                <input type ="submit" name ="update" class="button" value="Update" >
            </form>
                <form action ="../PurchaseCheckOut" method="POST" > 
               <input type = submit name="checkout" class ="button" value="Check Out">
          </form>
                <form action="MemberAccess/PurchaseM.jsp" >
                    <input type ="submit" name="continue" class="button" value="Continue Shopping">
                </form>
     </body>

</html>

PurchaseCheckOut.java

package controller;

import entity.Login;
import entity.LoginPK;
import entity.Members;
import entity.Purchasedetails;
import model.PurchaseService;
import entity.Purchase;
import entity.Stocks;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.Resource;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.transaction.Transactional;
import javax.transaction.UserTransaction;

public class PurchaseCheckOut extends HttpServlet {

        @PersistenceContext
    EntityManager em;
    @Resource
    UserTransaction utx;
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        int count = 0;
        PrintWriter out = response.getWriter();
        response.setContentType("text/html;charset=UTF-8");
            try {

                Stocks stock = new Stocks();

            HttpSession session = request.getSession();
           List<Object> list2 = (List<Object>) session.getAttribute("list1");
            PurchaseService ps = new PurchaseService(em);
            for(int i = 0; i < list2.size();i++){

                list2.get(i).toString();
                String toString = list2.get(i).toString(); 
                    String subString1 = toString.substring(2,7);
                   int subInt = 0;
                   String finalSubString1 ="";
                    subInt = Integer.parseInt(subString1)+ i;
                  if(subInt<10)
                 finalSubString1 = "PD0000"+String.valueOf(subInt);
            else if(subInt<100)
                 finalSubString1 = "PD000"+String.valueOf(subInt);       
            else if(subInt<1000)
                 finalSubString1 = "PD00"+String.valueOf(subInt);
            else if(subInt<10000)
                 finalSubString1 = "PD0"+String.valueOf(subInt);
            else if(subInt<100000)
                 finalSubString1 = "PD"+String.valueOf(subInt);

                    String subString2 = toString.substring(7,9);
                    String subString3 = toString.substring(9,15);
                    String subString4 = toString.substring(15,20);
                    stock.setStockid(subString3);
            String loginId = "abc";
            String memberId = "M0001";
            String password = "gay";
            LoginPK logPK = new LoginPK(loginId,memberId);
            Login log = new Login(logPK, password);
            session.setAttribute("log", log);
            Login j =  (Login) session.getAttribute("log");
          //  out.print(j.getLoginPK().getMemberId());
            String mem = j.getLoginPK().getMemberId();
            Members member = new Members();
            member.setMemberId(mem);
                    Date date = new Date();
                    DateFormat strformat = new SimpleDateFormat ("yyyy/MM/dd",Locale.ENGLISH);
                    String date2 = strformat.format(date);
                    Date date3 = strformat.parse(date2);
                    Purchase purchase= new Purchase(subString4,date3,member);
                     Purchasedetails pd = new Purchasedetails(finalSubString1,Integer.parseInt(subString2),stock,purchase);
                    out.println(purchase.getPurchaseid());
                    out.println(stock.getStockid());
                if(count == 0){    
            utx.begin();

            ps.addPurchase(purchase);
            utx.commit(); 
            count ++;
                }

           utx.begin();
           ps.addPurchasedetails(pd);  
           utx.commit();

            }
            session.removeAttribute("stockArray");
            session.removeAttribute("list1");
            session.removeAttribute("purchased");
            session.removeAttribute("stockPriceReal");
            response.sendRedirect("/MemberAccess/PurchaseAddConfirm.jsp");
        } catch (Exception ex) {
            Logger.getLogger(PurchaseCheckOut.class.getName()).log(Level.SEVERE, null, ex);
        } 
    }
}

编辑:

 <servlet-name>PurchaseM</servlet-name>
    <jsp-file>/MemberAccess/PurchaseM.jsp</jsp-file>
</servlet>
<servlet-mapping>
    <servlet-name>PurchaseM</servlet-name>
    <url-pattern>/MemberAccess/PurchaseM.jsp</url-pattern>
</servlet-mapping>

0 个答案:

没有答案