This question already has answers here:
Servlet returns “HTTP Status 404 The requested resource (/servlet) is not available”
                                
                                    (10个答案)
                                
                        
                                3年前关闭。
            
                    
我正在开发简单的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

它绝对正常!

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

任何帮助将非常感激!

最佳答案

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

我未转义您现在的网址

http://localhost:8080/CoffeeAdvice/â“SelectCoffee.doââ€?âcolourâ=âlightâ”

修正双引号。

关于html - 用Java提交表单时出现404错误,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/37987373/

10-12 03:16