简体   繁体   English

将Servlet映射到web.xml

[英]Mapping Servlet on web.xml

I was doing some changes to my web.xml when I started to get a 404 error. 当我开始出现404错误时,我正在对web.xml进行一些更改。 My servlet just prints out json data. 我的servlet仅打印出json数据。 Or my tomcat server just does not respond when I try to run the code. 否则,当我尝试运行代码时,tomcat服务器只是不响应。 What seems to be the problem? 似乎是什么问题? I have tried different servlet mapping and still does not seem to be working. 我尝试了不同的servlet映射,但似乎仍然无法正常工作。 Thank you for taking the time to review my code it is very appreciated! 感谢您抽出宝贵的时间来查看我的代码,非常感谢!

    <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>

The start to my servlet that just prints out the JSON data. 我的servlet的开始,仅打印出JSON数据。

   @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();
        }

    }

}

Verify that you has set your class correctly, example: 验证您是否正确设置了课程,例如:

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

I found this tutorial where you can see a complete example for JPS and Servlet and how to modify your web.xml file. 我找到了本教程 ,在这里您可以看到JPS和Servlet的完整示例以及如何修改web.xml文件。 All the code is available, so I think it can help you: http://www.ibm.com/developerworks/java/library/j-hangman-app/index.html 所有代码都可用,因此我认为它可以为您提供帮助: http : //www.ibm.com/developerworks/java/library/j-hangman-app/index.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM