为什么我的servlet不会在eclipse中运行?

时间:2016-04-03 19:02:53

标签: java html eclipse servlets

我正在完成本教程:http://theopentutorials.com/post/uncategorized/get-all-parameters-in-html-form-using-getparametermap/

我创建了我的java文件,如下所示:

package com.theopentutorials.servlets;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.Map.Entry;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class RetrievingAllParams extends HttpServlet {
    private static final long serialVersionUID = 1L;
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Map<String, String[]> map = request.getParameterMap();
        PrintWriter out = response.getWriter();
        response.setContentType("text/html");

        out.print("<html><body>");
        out.print("<h1> Your Order...</h1>");
        out.println("<table border=\"1\" cellpadding = \"5\"" + 
                " cellspacing = \"5\">");
        out.println("<tr> <th>Parameter Name</th>" + 
            "<th>Parameter Value</th></tr>");
        Set set = map.entrySet();
        Iterator it = set.iterator();
        while (it.hasNext()) {
            Map.Entry<String, String[]> entry = 
            (Entry<String, String[]>) it.next();
            String paramName = entry.getKey();
            out.print("<tr><td>" + paramName + "</td><td>");
            String[] paramValues = entry.getValue();
            if (paramValues.length == 1) {
                String paramValue = paramValues[0];
                if (paramValue.length() == 0)
                    out.println("<b>No Value</b>");
                else
                    out.println(paramValue);
            } else {
                out.println("<ul>");
                for (int i = 0; i < paramValues.length; i++) {
                    out.println("<li>" + paramValues[i] + "</li>");
                }
                out.println("</ul>");
            }
            out.print("</td></tr>");
        }
        out.println("</table></body></html>");
    }
}

这是html文件:

 <html>
 <head>
     <title>Retrieving All Parameters</title>
</head>
<body>
    <h4>Order your Pizza Here</h4>
    <form action="allparams.do" method="post">
    <b>Name</b> <input type="text" name="name"><br><br>
    <b>Select the Crust:</b> 
    <select name = "crust">
        <option value="pan">Pan</option>
        <option value="thin">Thin Crust</option>
        <option value="deep">Deep Crust</option>
        <option value="cheese">Cheese Burst</option>
    </select> <br><br>
    <b>Toppings: </b><br>
    <input type="checkbox" name="toppings" value="peas">Peas<br>
    <input type="checkbox" name="toppings" value="paneer">Paneer<br>
    <input type="checkbox" name="toppings" value="redpeppers">Red Peppers<br>
    <input type="checkbox" name="toppings" value="pineapple">Pineapple<br>
    <input type="checkbox" name="toppings" value="onion">Onion<br>
    <input type="checkbox" name="toppings" value="tomato">Tomato<br><br>

    <b>Select 1 FREE Appetizer</b>
    <input type="radio" name="appetizer" value="Garlic Bread">Garlic Bread
    <input type="radio" name="appetizer" value="Cheese Garlic Bread">Cheese Garlic Bread
    <input type="radio" name="appetizer" value="Veg Soup">Veg Soup
    <input type="radio" name="appetizer" value="Veg Sandwich">Veg Sandwich<br><br>

    <b>Address</b><br>
    <textarea name="address" rows=3 cols=40></textarea><br><br>
    <b>Credit Card:</b><br>
    <input type="radio" name="cardType" value="Visa">Visa
    <input type="radio" name="cardType" value="MasterCard">MasterCard
    <input type="radio" name="cardType" value="Amex">American Express
    <br><br>
    <b>Credit Card Number:</b> <input type="password" name="cardNum">
    <b>Repeat Credit Card Number: </b><input type="password" name="cardNum"><br><br>
    <input type="submit" name="submit" value="Order Pizza">
    </form>
</body>

web.xml文件如下所示:

<?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns="http://java.sun.com/xml/ns/javaee"
        xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"              
        xsi:schemaLocation=
        "http://java.sun.com/xml/ns/javaee      
         http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
        id="WebApp_ID" version="2.5">
        <servlet>
            <servlet-name>AllParamsMap</servlet-name>
            <servletclass>com.theopentutorials.servlets.
            RetrievingAllParams</servlet-class>
        </servlet>
        <servlet-mapping>
            <servlet-name>AllParamsMap</servlet-name>
            <url-pattern>/allparams.do</url-pattern>
        </servlet-mapping>
    </web-app>

我正在使用此调用服务器:http://localhost:8080/FormServlet/allparams.html

以下是我在网络浏览器中收到的回复:enter image description here

我的java类文件位于匹配的包enter image description here

我做错了什么?感谢您的帮助!

0 个答案:

没有答案