在web.xml中映射Servlet

时间:2014-05-01 19:22:12

标签: servlets web.xml

当我开始收到404错误时,我正在对我的web.xml进行一些更改。我的servlet只打印出json数据。或者当我尝试运行代码时,我的tomcat服务器没有响应。什么似乎是问题?我尝试过不同的servlet映射,但似乎仍然没有工作。感谢您花时间查看我的代码,非常感谢!

    <servlet>
        <servlet-name>inventory</servlet-name>
        <servlet-class>InventoryServlet</servlet-class> 
    </servlet>
    <servlet-mapping>
    <servlet-name>inventory</servlet-name>
    <url-pattern>InventoryServlet</url-pattern>
    </servlet-mapping>

</web-app>

刚刚打印出JSON数据的servlet的开始。

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

        @Override
        protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            ArrayList < LavaLamp > lamps = new ArrayList < LavaLamp > ();

        Manufacturer lavaLampDotCom = new Manufacturer();
        lavaLampDotCom.setManName("lavalamp.com");

        LavaLamp blulava = new LavaLamp();
        blulava.setLavaName("Blulava");
        blulava.setType("Original");
        blulava.setPrice(17.99);
        blulava.setHeight(12);
        blulava.setWidth(3.5);
        blulava.setWaxColor("Blue");
        blulava.setLiquidColor("Red");
        blulava.setId(1);
        blulava.setCostBook(new SimpleCostBook(blulava.getPrice()));
        blulava.setManufacturer(lavaLampDotCom);
        blulava.setManId(1);
        blulava.setSku(1111);
        lamps.add(blulava);

        Manufacturer volcanicLavaDotNet = new Manufacturer();
        volcanicLavaDotNet.setManName("volcaniclava.net");

        LavaLamp bloodlava = new LavaLamp();
        bloodlava.setLavaName("Blood Lava");
        bloodlava.setType("Original");
        bloodlava.setWaxColor("Red");
        bloodlava.setLiquidColor("Green");
        bloodlava.setPrice(15.99);
        bloodlava.setHeight(12);
        bloodlava.setWidth(3.5);
        bloodlava.setId(2);
        bloodlava.setCostBook(new SimpleCostBook(bloodlava.getPrice()));
        bloodlava.setManufacturer(volcanicLavaDotNet);
        bloodlava.setManId(4);
        bloodlava.setSku(2222);
        lamps.add(bloodlava);


        Manufacturer lavaHouseDotCom = new Manufacturer();
        lavaHouseDotCom.setManName("lavahouse.com");

        LavaLamp purppower = new LavaLamp();
        purppower.setLavaName("Purp Power");
        purppower.setType("Large");
        purppower.setWaxColor("Purple");
        purppower.setLiquidColor("Yellow");
        purppower.setPrice(22.99);
        purppower.setHeight(16);
        purppower.setWidth(4.5);
        purppower.setId(3);
        purppower.setCostBook(new SimpleCostBook(purppower.getPrice()));
        purppower.setManufacturer(lavaHouseDotCom);
        purppower.setManId(3);
        purppower.setSku(3333);
        lamps.add(purppower);

        Manufacturer lavaCountryDotOrg = new Manufacturer();
        lavaCountryDotOrg.setManName("lavacountry.org");

        LavaLamp mellowyellow = new LavaLamp();
        mellowyellow.setLavaName("Mellow Yellow");
        mellowyellow.setType("Large");
        mellowyellow.setWaxColor("Yellow");
        mellowyellow.setLiquidColor("Black");
        mellowyellow.setPrice(29.99);
        mellowyellow.setHeight(16);
        mellowyellow.setWidth(4.5);
        mellowyellow.setId(4);
        mellowyellow.setCostBook(new SimpleCostBook(mellowyellow.getPrice()));
        mellowyellow.setManufacturer(lavaCountryDotOrg);
        mellowyellow.setManId(5);
        mellowyellow.setSku(4444);
        lamps.add(mellowyellow);

        Manufacturer jamiesBasementDotSwag = new Manufacturer();
        jamiesBasementDotSwag.setManName("jamiesbasement.swag");

        LavaLamp rainbowmadness = new LavaLamp();
        rainbowmadness.setLavaName("Rainbow Madness");
        rainbowmadness.setType("XXL");
        rainbowmadness.setWaxColor("Rainbow");
        rainbowmadness.setLiquidColor("Clear");
        rainbowmadness.setPrice(49.99);
        rainbowmadness.setHeight(50);
        rainbowmadness.setWidth(12.5);
        rainbowmadness.setId(5);
        rainbowmadness.setCostBook(new SimpleCostBook(rainbowmadness.getPrice()));
        rainbowmadness.setManufacturer(jamiesBasementDotSwag);
        rainbowmadness.setManId(2);
        rainbowmadness.setSku(5555);
        lamps.add(rainbowmadness);

        Manufacturer lavaLampCom = new Manufacturer();
        lavaLampCom.setManName("lavalamp.com");

        LavaLamp greenwilly = new LavaLamp();
        greenwilly.setLavaName("Green Willy");
        greenwilly.setType("Large");
        greenwilly.setWaxColor("Green");
        greenwilly.setLiquidColor("Clear");
        greenwilly.setPrice(27.99);
        greenwilly.setHeight(16);
        greenwilly.setWidth(3.5);
        greenwilly.setId(6);
        greenwilly.setCostBook(new SimpleCostBook(greenwilly.getPrice()));
        greenwilly.setManufacturer(lavaLampCom);
        greenwilly.setManId(1);
        greenwilly.setSku(6666);
        lamps.add(greenwilly);
        try {

        PrintWriter out = response.getWriter();
        out.write(new Gson().toJson(lamps));

        } catch (IOException e) {
            e.printStackTrace();
        }

    }

}

1 个答案:

答案 0 :(得分:0)

确认您已正确设置课程,例如:

<servlet>
    <servlet-name>MyServletClass</servlet-name>
    <servlet-class>myPackage.mySubPackage.myClass</servlet-class>
</servlet>

我找到了这个tutorial,您可以在其中看到JPS和Servlet的完整示例以及如何修改您的web.xml文件。所有代码都可用,所以我认为它可以帮助您:http://www.ibm.com/developerworks/java/library/j-hangman-app/index.html