Tomcat无法找到我的servlet

时间:2014-05-29 05:59:21

标签: java jsp tomcat servlets tomcat7

我正在使用IDEA 13。我正在尝试编写一个简单的JAVA应用程序,其中有一个表单 有一些选项,它的动作是一个servlet.Here是我的文件结构

webapps
 |___ROOT
     |___Beer
           |___form.html
           |___index.jsp
           |___WEB-INF
                 |___web.xml
                 |___classes
                       |____com
                             |___example
                                   |___model
                                   |     |___BeerExpert.class
                                   |
                                   |___web
                                        |___BeerSelect.class



Beer是我的app的名称。表单显示正确,但是当我单击提交按钮时,tomcat无法找到servlet 再次显示空白页面(表格未显示)" HTTP状态404 - /Beer/SelectBeer.do"


我的web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee   http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
     version="3.1">
<servlet>
    <servlet-name>Ch3 Beer</servlet-name>
    <servlet-class>com.example.web.BeerSelect</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>Ch3 Beer</servlet-name>
    <url-pattern>/SelectBeer.do</url-pattern>
</servlet-mapping>
</web-app>


我的servlet

package com.example.web;
import com.example.model.*;

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Iterator;
import java.util.List;

public class BeerSelect extends HttpServlet {
    public void  doPost(HttpServletRequest request,HttpServletResponse response)
    throws IOException , ServletException {

        String c=request.getParameter("color");
        BeerExpert be=new BeerExpert();
        List result=be.getBrands(c);
        request.setAttribute("styles",result);
        RequestDispatcher view=request.getRequestDispatcher("result.jsp");
        view.forward(request,response);


    }
}


我的表格

    <!DOCTYPE html>
    <html>
    <head lang="en">
        <meta charset="UTF-8">
        <title></title>
    </head>
    <body>
    <h1 align="center">Beer Selection</h1>
    <form method="POST" action="SelectBeer.do">

     Select Beer characteristics<br>
    Color:
    <select name="color" size="1">
    <option value="light">light</option>
    <option value="dark">dark</option>
    <option value="amber">amber</option>
    <option value="brown">brown</option>
    </select>
    <br><br>
    <center>
     <input type="submit">
   </center>
   </form>
   </body>
   </html>


我的jsp文件(结果由它显示)

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page import="java.util.*" %>
<html>
<head>
    <title></title>
</head>
<body>
<h1 align="center">Beer Recommendation JSP</h1>
<%
    List styles=(List)request.getAttribute("styles");
    Iterator it=styles.iterator();
    while(it.hasNext()){
    out.print("<br>try: " +it.next());
    }
%>
</body>
</html>


我的模特班

     /**
 * Created by sasha.s on 5/28/2014.
 */
import java.util.*;
public class BeerExpert {
    public List getBrands(String color){
        List brands=new ArrayList();
        if(color.equals("amber")){
            brands.add("Jack Amber");
            brands.add("Red Moose");
        }
        else{
            brands.add("Jail Pale Ale");
            brands.add("Gout Stout");
        }
        return (brands);
    }
}


为什么tomcat无法找到我的servlet?

2 个答案:

答案 0 :(得分:3)

将您的应用程序从webapps / ROOT目录移动到webapps目录。

webapps/
   ROOT/
       ...
   Beer/
       index.jsp
       ...
   app2/
       ...

答案 1 :(得分:0)

也覆盖doGet方法,

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

        doPost(request,response)
  }