使链接转到servlet以重定向到另一个页面

时间:2014-03-31 20:16:38

标签: java jsp java-ee servlets

首先让我开始说我的servlet以一种我刚刚好的形式加载选项列表。问题是当我从index.jsp开始,就像我想要的那样,列表不加载。所以基本上,我想点击index.jsp上的一个链接,带我到servlet,然后根据点击的链接将我重定向到正确的页面。也许我一直在看这个太长,只需要新鲜的眼睛,但我不知道为什么它不会工作。

我已经包含了我的Index.jsp和servlet

的index.jsp

<%@ 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>Insert title here</title>
</head>
<body>



    <form method="get" action="customerServlet">


        <a href="customerServlet?addCustomer">Add Customer</a>
    <br/>
    <a href="customerServlet?addPet">Add Pet</a>
    </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" })
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);
            request.getRequestDispatcher("/customerManagement.jsp").forward(request, response);
        //}

    }







    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 sendEditCustomerForm(HttpServletRequest request, 
            HttpServletResponse response) throws IOException, ServletException {

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



    public void doGet(HttpServletRequest request, 
            HttpServletResponse response)
            throws ServletException, IOException {
         String uri = request.getRequestURI();
         if (uri.endsWith("/addCustomer")) {
             addCustomer(response, request);
         } else if (uri.endsWith("/addPet")) {
             addPet(response, request);
         }

    }


    public void doPost(HttpServletRequest request, 
            HttpServletResponse response)
            throws ServletException, IOException {
         processRequest(request, response);

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

    }
}

2 个答案:

答案 0 :(得分:2)

使用一个参数并检查值比手动解析URL更容易:

<a href="customerServlet?action=addCustomer">Add Customer</a>
<br/><a href="customerServlet?action=addPet">Add Pet</a>

在你的servlet中:

String action = request.getParameter("action");
if("addCustomer".equals(action)) { ... }
else if("addPet".equals(action)) { ... }

答案 1 :(得分:0)

如果您正在使用&#34;表单&#34;,这可能是将params发送到servlet的解决方案。

 <form action="ServletName" method="POST">
     <input type="text" name="paramName">
     <input type="submit" value="Add">
    </form>

在Servlet中:

String costumerName = request.getParameter("paramName");

如果你只是使用像href这样的链接,你应该发送如下的参数:

<a href="ServletName?ID=12345">

在servlet中,同样。

String costumerName = request.getParameter("ID");