将信息从Servlet发送回JSP

时间:2014-02-22 03:03:32

标签: java jsp servlets

用户点击index.jsp页面上的edit,然后将它们发送到editCustomer.jsp页面。问题是,当我点击更新时,它不会更新索引页面上的客户信息。

如何使用我的代码执行此操作?

的index.jsp

    <%@page import="edu.witc.Assignment02.controller.CustomerServlet"%>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
   <%@ page import="java.util.ArrayList" %>
<!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>Home</title>
</head>
<body>
<ul>
           <% String customerString ="";
           ArrayList<edu.witc.Assignment02.model.Customer> customers = (java.util.ArrayList)request.getAttribute("customers");
           for (edu.witc.Assignment02.model.Customer customer : customers) {
                customerString += "<li>" + customer.getName() +
                           "(" + customer.getCity() + ") (" +
                           "<a href='editCustomer?id=" + customer.getId() +
                           "'>edit</a>)</li>";
           }%>
           <%=customerString %>
           </ul>
           <body>
</html>

EDITCUSTOMER.JSP

    <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
    <%@page import="edu.witc.Assignment02.controller.CustomerServlet"%>
<!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>Edit Customer</title>
</head>
<body>
<h2>Edit Customer</h2> 
<% int customerId = 
                    Integer.parseInt(request.getParameter("id"));%>
 <form method='post' action='customer'>
  <input type='hidden' name ='id' value='<%=customerId%>'/>
  <table>

  <tr><td>Name:</td><td>"
 <input name='name' /> 
 </td></tr>
 <tr><td>City:</td><td>
<input name='city' />
</td></tr>
<tr>
<td colspan='2' style='text-align:right'>
<input type='submit' value='Update'/></td> 
</tr>
<tr><td colspan='2'>
<a href='customer'>Customer List</a>
</td></tr>
  </table>
            </form></body>


</html>

SERVLET

package edu.witc.Assignment02.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import edu.witc.Assignment02.*;
import edu.witc.Assignment02.model.Customer;

/*
 * Not thread-safe. For illustration purpose only
 */
@WebServlet(name = "CustomerServlet", urlPatterns = { 
        "/customer", "/editCustomer", "/updateCustomer"})
public class CustomerServlet extends HttpServlet {
    private static final long serialVersionUID = -20L;

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

    @Override
    public void init() throws ServletException {
        Customer customer1 = new Customer();
        customer1.setId(1);
        customer1.setName("Donald D.");
        customer1.setCity("Miami");
        customers.add(customer1);

        Customer customer2 = new Customer();
        customer2.setId(2);
        customer2.setName("Mickey M.");
        customer2.setCity("Orlando");
        customers.add(customer2);       
    }

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




    }

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

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

        String url = "/editCustomer.jsp";
        request.setAttribute("customers", customers);

        request.getRequestDispatcher(url).forward(request,response);
    }


    @Override
    public void doGet(HttpServletRequest request, 
            HttpServletResponse response)
            throws ServletException, IOException {
        String uri = request.getRequestURI();
        if (uri.endsWith("/customer")) {
            sendCustomerList(response, request);
        } else if (uri.endsWith("/editCustomer")) {
            sendEditCustomerForm(request, response);
        }

    }

    @Override
    public void doPost(HttpServletRequest request, 
            HttpServletResponse response)
            throws ServletException, IOException, ServletException {
        // update customer
        int customerId = 0;
        try {
            customerId = 
                    Integer.parseInt(request.getParameter("id"));
        } catch (NumberFormatException e) {
        }
        Customer customer = getCustomer(customerId);
        if (customer != null) {
            customer.setName(request.getParameter("name"));
            customer.setCity(request.getParameter("city"));
        }
        sendCustomerList(response, request);
    }
}

CUSTOMER.JAVA

package edu.witc.Assignment02.model;

public class Customer {
    private int id;
    private String name;
    private String city;

    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getCity() {
        return city;
    }
    public void setCity(String city) {
        this.city = city;
    }
}

1 个答案:

答案 0 :(得分:0)

我不确定你期望发生什么。您正在提交此表单

<form method='get' action='customer'>
    <table>
        <tr>
            <td>Name:</td>
            <td>
                "
                <input name='name' />
            </td>
        </tr>
        <tr>
            <td>City:</td>
            <td>
                <input name='city' />
            </td>
        </tr>
        <tr>
            <td colspan='2' style='text-align:right'>
                <input type='submit' value='Update' />
            </td>
        </tr>
        <tr>
            <td colspan='2'>
                <a href='customer'>Customer List</a>
            </td>
        </tr>
    </table>
</form>

处理它
if (uri.endsWith("/customer")) {
     sendCustomerList(response, request);

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

上述代码中的任何内容都不会修改customers List中的任何内容。对象不会自行更改。你必须自己做修改。

也许您打算使用POST请求提交表单。在这种情况下,您需要<input>的{​​{1}}字段。


考虑请求 - 响应周期。

您的Servlet容器收到HTTP请求,它会调度您的Servlet来处理它。您的servlet执行一些逻辑并转发到JSP。 JSP呈现一些写入HTTP响应主体的HTML。响应将发送给客户端。在这种情况下,您的客户端是一个浏览器,它读取HTML文本并以图形方式呈现它。然后,您可以与它进行交互,填写输入字段和单击按钮。执行此操作时,浏览器将获取您输入的值并生成一个HTTP请求,并将其发送到您的服务器。依此类推,等等......