找不到Tomcat 7 404 intelliJ资源

时间:2013-10-18 12:08:08

标签: java jsp tomcat intellij-idea web.xml

我有一个从intelliJ运行的简单tomcat服务器,我正在尝试配置一个servlet,允许用户选择不同颜色的啤酒,但是我总是收到一个表单提交后找不到的404资源。我的代码是一个问题,还是一个inteliJ配置问题?

的web.xml

    <?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
          http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
           version="3.0">
    <servlet>
        <servlet-name>
            beerSelect
        </servlet-name>
        <servlet-class>
            com.example.web.controller.beerSelect
        </servlet-class>
    </servlet>

    <servlet-mapping>
        <servlet-name>beerSelect</servlet-name>
        <url-pattern>/beerSelect.do(</url-pattern>
    </servlet-mapping>
</web-app>

form.html

<!DOCTYPE html>
<html>
<head>
    <title>Beverages</title>
    <link rel="stylesheet" type="text/css" href="/style/main.css">
</head>
<body>
<h1 class="title">Beer selection form</h1>
<form method="post" action="beerSelect.do">
    Select your beer type:
    <select name="color" size="1">
        <option value="light">Light</option>
        <option value="amber">Amber</option>
        <option value="mild">Mild</option>
        <option value="dark">Dark</option>
    </select>
    <input type="SUBMIT" value="Submit">
</form>
</body>
</html>

beerSelect.java

package com.example.web.controller;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
/**
 * Created with IntelliJ IDEA.
 * User: Chris
 * Date: 18/10/13
 * Time: 10:42
 * To change this template use File | Settings | File Templates.
 */

public class beerSelect extends HttpServlet{
    PrintWriter responseWriter;
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException{
        response.setContentType("text/html");
        responseWriter = response.getWriter();
        responseWriter.print("<p>Beer Selection Advice<p>");
        responseWriter.print("<br>Beer Color:" + request.getParameter("color"));
    }
}

1 个答案:

答案 0 :(得分:3)

首先看看web.xml

中是否发现了一个错误
 <url-pattern>/beerSelect.do(</url-pattern>
                            ↑

删除(开圆括号

修改

由于doPost方法仅处理ServletException

,您将获得未处理的错误IOException
public void doPost(HttpServletRequest request, HttpServletResponse response) 
                                                 throws IOException{  

更改为

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

同时将@Override注释添加到doPost方法,请检查此answer

 @Override creates a compile-time check that a method is being overridden. 
 This is very useful to make sure you do not have a silly signature issue 
 when trying to override. 

见下文

@Override
public void doPost(HttpServletRequest request, HttpServletResponse response) 
                                                 throws IOException, ServletException{