Java中表单提交时出现404错误

时间:2016-06-23 09:21:22

标签: html eclipse tomcat servlets http-status-code-404

我正在开发简单的Web应用程序。我在Eclipse Mars中创建了Dynamic Web Project,我使用的是Java 1.8和Tomcat v8.0.36。 我创建了一个简单的表单:

<!DOCTYPE html>
<html>
<head>
<title>Coffee Advice Page</title>
</head>
<body>
<form action=”SelectCoffee.do”>
Select Coffee characteristics<p>
Color:
<select name=”color” size=”1”>
<option value=”light”> light </option>
<option value=”amber”> amber </option>
<option value=”brown”> brown </option>
<option value=”dark”> dark </option>
</select>
</br></br>

<input type="submit" value="Submit">

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

一个Servlet:

package com.example.web;

import java.io.IOException;
import java.io.PrintWriter;

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

@SuppressWarnings("serial")
public class CoffeeSelectionServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        System.out.println("Inside doGet()");
        PrintWriter printWriter = response.getWriter();
        printWriter.println("doGet() is working fine!");

    }

}

Web.xml中:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://xmlns.jcp.org/xml/ns/javaee"
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
    id="WebApp_ID" version="3.1">
    <display-name>CoffeeAdvice</display-name>
    <welcome-file-list>
        <welcome-file>index.html</welcome-file>
        <!-- <welcome-file>index.htm</welcome-file>
        <welcome-file>index.jsp</welcome-file>
        <welcome-file>default.html</welcome-file>
        <welcome-file>default.htm</welcome-file>
        <welcome-file>default.jsp</welcome-file> -->
    </welcome-file-list>

    <servlet>
        <servlet-name>Servlet1</servlet-name>
        <servlet-class>com.example.web.CoffeeSelectionServlet</servlet-class>
    </servlet>

    <servlet-mapping>
        <servlet-name>Servlet1</servlet-name>
        <url-pattern>/SelectCoffee.do</url-pattern>
    </servlet-mapping>

</web-app>

但是当我启动Tomcat服务器并提交此表单时,查询字符串出现404错误: http://localhost:8080/CoffeeAdvice/%C3%A2%E2%82%AC%C2%9DSelectCoffee.do%C3%A2%E2%82%AC%C2%9D?%E2%80%9Dcolor%E2%80%9D=%E2%80%9Dlight%E2%80%9D

这不是我的意图。 如果我直接从浏览器栏发出GET请求:

http://localhost:8080/CoffeeAdvice/SelectCoffee.do?color=light

它绝对正常!

请让我知道为什么这个查询字符串会像这样生成失真以及我必须更改的内容。

非常感谢任何帮助!

1 个答案:

答案 0 :(得分:0)

您在表单操作中使用的双引号是不正确的

我没有取消你现在的网址

http://localhost:8080/CoffeeAdvice/â¢â¬ÂSelectCoffee.doÃâ¬Â?âcolorâ=âlightâ”

修正双重报价。