将输入放入JSTL会话变量以便稍后在屏幕上显示

时间:2014-04-01 22:52:05

标签: java jsp session servlets jstl

我一直在网上查找将我的输入放入会话的答案,以便我可以在收据页面上输出,但所有这些都是不同的。有人可以选择我的首选方式吗?

我将在下面发布我的servlet和表单

表格

    <%@ 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">
        <c:set var="customer" items="${sessionScope.customers}">


        First Name:<br>
        <input type="text" name="firstName"/><br>
        <c:out value="${customer.getFirstName() }"></c:out>
        Last Name:<br>
        <input type="text" name="lastName" <c:out value="customer.getLastName()"></c:out>/><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">
    </c:set>
        </form>
</body>
</html>

的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", "/customerManagement" })
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);

        }

    }
}

0 个答案:

没有答案