Servlet不会重定向到收据页面

时间:2014-04-01 22:21:08

标签: java java-ee servlets

所以我被困在用户点击提交按钮的位置,页面应该转到收据页面并显示信息。

我是盲人还是应该这样做?

所有其他重定向都可以使用我正在使用的方法正常工作但由于某种原因它不会进入收据页面。

的Servlet

package edu.witc.Assignment03.controller;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import javax.servlet.*;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
//import javax.servlet.annotation.WebServlet;
//import javax.servlet.http.HttpServlet;
//import javax.servlet.http.HttpServletRequest;
//import javax.servlet.http.HttpServletResponse;




import javax.servlet.http.HttpSession;

import edu.witc.Assignment03.model.Customer;
import edu.witc.Assignment03.model.Phone;
import edu.witc.Assignment03.model.States;


@WebServlet(description = "servlet to get act as controller between form and models", urlPatterns = { "/customerServlet","/addCustomer","/addPet" })
public class CustomerServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    public CustomerServlet() {
        super();
    }


    private void processRequest(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub

        HttpSession session = request.getSession();


        Phone phone = new Phone();
        States state = new States();


        Collection<Phone> phones = phone.getPhoneCollection();
        Collection<States> states = state.getStateCollection();

            session.setAttribute("phones", phones);
            session.setAttribute("states", states);

        //}

    }







    private List<edu.witc.Assignment03.model.Customer> customers = new ArrayList<Customer>();



 private void addCustomer(HttpServletResponse response, HttpServletRequest request)//redirect to index
           throws IOException, ServletException {
    String url = "/customerManagement.jsp";
    processRequest(request, response);
        request.getRequestDispatcher(url).forward(request,response);
    }

 private void addPet(HttpServletResponse response, HttpServletRequest request)//redirect to index
         throws IOException, ServletException {
    String url = "/pets.jsp";
    request.getRequestDispatcher(url).forward(request,response);
  }




    private Customer getCustomer(int customerId) {
        for (Customer customer : customers) {
            if (customer.getCustomerId() == customerId) {
                return customer;
            }
        }
        return null;
    }



    private void makeCustomerReceipt(HttpServletRequest request, 
            HttpServletResponse response) throws IOException, ServletException {

        String url = "/receipt.jsp";
        request.setAttribute("customers", customers);
        request.getRequestDispatcher(url).forward(request,response);
    }



    public void doGet(HttpServletRequest request, 
            HttpServletResponse response)
            throws ServletException, IOException {
        String action = request.getParameter("action");
        if("addCustomer".equals(action)) { 
            addCustomer(response, request);
            }
        else if("addPet".equals(action)) { 
            addPet(response, request);
        }


    }


    public void doPost(HttpServletRequest request, 
            HttpServletResponse response)
            throws ServletException, IOException {
        // update customer
        int customerId = 0;
        try {
            customerId = 
                    Integer.parseInt(request.getParameter("id"));
        } catch (NumberFormatException e) {
        }
        Customer customer = getCustomer(customerId);
        if (customer != null) {
            customer.setFirstName(request.getParameter("firstName"));
            customer.setLastName(request.getParameter("lastName"));
            customer.setEmail(request.getParameter("email"));
            customer.setPhone(request.getParameter("phone"));
            customer.setAddress(request.getParameter("address"));
            customer.setCity(request.getParameter("city"));
            customer.setZip(request.getParameter("zip"));
            makeCustomerReceipt(request, response);

        }

    }
}

CustomerManagement.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
    <%@ page import="java.util.ArrayList" %>
    <%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> 
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Customer Management</title>
</head>
<body>
    <form action="customerServlet" method="post">

        First Name:<br>
        <input type="text" name="firstName"/><br>

        Last Name:<br>
        <input type="text" name="lastName"/><br>
        Email:<br>
        <input type="text" name="email"/><br>
        Phone Number:<br>
        <input type="text" name="phone"/><br>
        Phone Type:<br>
        <select name="thePhones" id="selectPhones">
            <option selected value="choose">
                Select a Phone
            </option>
            <c:forEach items="${sessionScope.phones}" var="current"  >
                <option>${current.getPhoneName()}</option>              
            </c:forEach>        
        </select><br>
        Street Address:<br>
        <input type="text" name="streetAddress"/><br>
        Apartment Number:<br>
        <input type="text" name="apartmentNumber"/><br>
        City:<br>
        <input type="text" name="city"/><br>
        State:<br>
        <select name="states" id="states">
            <option selected value="Wisconsin">
                Select a State
            </option>
            <c:forEach items="${sessionScope.states}" var="current"  >
                <option>${current.getStates()}</option>             
            </c:forEach>        
        </select>
        <input type="submit" value="submit">

        </form>
</body>
</html>

1 个答案:

答案 0 :(得分:0)

要进行问题排查,请将response.getWriter().write("..."); return;添加到您现在无所事事的两个可能的问题位置:

    public void doPost(HttpServletRequest request, 
            HttpServletResponse response)
            throws ServletException, IOException {
        // update customer
        int customerId = 0;
        try {
            customerId = 
                    Integer.parseInt(request.getParameter("id"));
        } catch (NumberFormatException e) {
           response.getWriter().write("Error: Customer ID could not be parsed to a number");
           return; //to exit the function: we don't want to do anything more if we encounter this error
        }
        Customer customer = getCustomer(customerId);
        if (customer != null) {
            customer.setFirstName(request.getParameter("firstName"));
            customer.setLastName(request.getParameter("lastName"));
            customer.setEmail(request.getParameter("email"));
            customer.setPhone(request.getParameter("phone"));
            customer.setAddress(request.getParameter("address"));
            customer.setCity(request.getParameter("city"));
            customer.setZip(request.getParameter("zip"));
            makeCustomerReceipt(request, response);
            return; //to exit the function: you should always do this after forward or redirect
        }
        else
        {
           response.getWriter().write("Error: customer is null");
           return; //to exit the function
        }
    }
}