【问题标题】:Is it a mandate to write web.xml file for a Servlet program to run?是否需要为 Servlet 程序编写 web.xml 文件才能运行?
【发布时间】:2015-04-10 09:02:25
【问题描述】:

web.xml file 是否必须实际存在于文件中才能运行 Servlet 程序?

听说在 Tomcat 7.0 中,您不需要将 web.xml file 明确写为 @WebServlet @ Servlet 类中存在的 987654324@ 在内部自动构建 web.xml file..

如果是这样,是什么提示下面的程序输出以下错误信息?

HTTP Status 404 - /Show_Items

type 状态报告

message /Show_Items

description 请求的资源不可用。

有人能解释一下吗?

Servlet 文件

package com.foo.randomcode;


/*This file is connected to HTML file called Order_Form.html*/

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * Servlet implementation class ShowItems_ArrayList
 */
@WebServlet("/Show_Items")
public class Show_Items extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public Show_Items() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        HttpSession session = request.getSession();

        ArrayList previousItems = (ArrayList) session.getAttribute("previousItems");

        //session.getAttribute always return an Object, hence it is typecasted to match
        // receiving data type. 

        if(previousItems == null){
            previousItems = new ArrayList();

            session.setAttribute("previousItems", previousItems);

            //session.setAttribute(String name, String value)
            //The name should match with the associated value.

        }

        String newItem = request.getParameter("newItem");
        response.setContentType("text/html");

        PrintWriter out = response.getWriter();
        String title = "Items purchased";
        out.println("<html> \n" +
                    "<head><title>" +title+ "</head></title>" +
                    "<body bgcolor=\"#fdf5e6> \n" + 
                    "<h2>" +title+ "</h2>"

                    );

        synchronized (previousItems) {
            if(newItem !=null){
                previousItems.add(newItem);
            }

            if(previousItems.size()==0){
                    out.println("<i>No items</i>");
                }

            else {
                out.println("<ul>");
                for(int i=0; i<=previousItems.size(); i++){
                    out.println("<li>" +previousItems.get(i));
                }
                out.println("<ul>");
            }
        }

        out.println("</body></html>");
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
    }

}

Order_Formhtml

<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>

<h3>Order Form</h3>

    <form action="com.foo.randomcode.Show_Items">
        <label> New Item to Order: <input type="text" name="newItem" placeholder="yacht" /> <br><br> 
            <input type="submit" value="Order & Show all Purchases">
        </label>
</form>

</body>
</html>

错误堆栈跟踪:更改 &lt;form action="Show_Items"&gt;

127.0.0.1 - - [10/Apr/2015:16:41:01 +0530] "GET / HTTP/1.1" 200 11418

0:0:0:0:0:0:0:1 - - [10/Apr/2015:16:41:02 +0530] "GET /Runtime_Testing/Order_Form.html HTTP/1.1" 200 370

0:0:0:0:0:0:0:1 - - [10/Apr/2015:16:41:06 +0530] "GET /Runtime_Testing/Show_Items?newItem=hello HTTP/1.1" 500第1585章

新的错误信息:

HTTP 状态 500 - 索引:1,大小:1

type 异常报告

message 索引:1,大小:1

description 服务器遇到内部错误,导致它无法完成此请求。

exception

java.lang.IndexOutOfBoundsException: Index: 1, Size: 1 java.util.ArrayList.rangeCheck(ArrayList.java:635) java.util.ArrayList.get(ArrayList.java:411) com.foo.randomcode.Show_Items.doGet(Show_Items.java:68) javax.servlet.http.HttpServlet.service(HttpServlet.java:620) javax.servlet.http.HttpServlet.service(HttpServlet.java:727) org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)

最终输出

URL: http://localhost:8080/Runtime_Testing/Show_Items?newItem=computer

//这是程序运行后屏幕上可见的内容

&lt;body bgcolor="#fdf5e6&gt; &lt;h2&gt;Items purchased&lt;/h2&gt; &lt;ul&gt; &lt;li&gt; computer &lt;ul&gt; &lt;/body&gt;&lt;/html&gt;

【问题讨论】:

  • 将表单操作更改为action="/Show_Items"
  • /Show_Items@WebServlet("/Show_Items") 中代表什么?你了解 url 映射到 Servlet。
  • action 属性 form 应该指向映射到 servlet 的 url。
  • @braj - 映射到 Servlet 的 url 是我认为我无法完全理解的一个主题...您能详细说明一下吗?
  • @Kishore - 这就是我想澄清的。是否需要编写 web.xml 文件?如果是,那么传递给form' element and what should be the path of ` 的action 属性的参数应该是什么? 等待回复。

标签: java xml servlets


【解决方案1】:

在java EE edition 6(servlet 3.0 规范)之后,您无需编写web.xml 文件。您可以使用@WebServlet 注释servlet。在以前的版本中。

你需要做的

 <form action="Show_Items">

它被映射到@WebServlet("/Show_Items")

action 元素应该具有 url 模式的值,而不是 servlet 类本身。 @WebServlet 注释将 url 模式映射到 servlet 类。这就像在 web.xml 中指定 &lt;servlet-mapping&gt; 元素一样。

对于 IndexOutOfBoundsException,将 for 循环更改为

for(int i=0; i<previousItems.size(); i++)

在您之前的条件下,i&lt;=previousItems.size() 返回大小为1,即元素总数仅为 1。但您正在尝试访问previousItems.get(i),其中i=1 的值是列表中的第二个元素不简单的存在。请记住,列表是基于零索引的。

看了你的servlet代码,看来你应该跟着MVC architecture with servlets and jsp有一定的advantages of using jsp over servlets for showing output.

【讨论】:

  • @kharkar - 那么上面的程序有什么问题?
  • @kharkar - HTTP 状态 404 error 仍然存在,尽管应用了更改。
  • @kharkar - 是的。 如果在 Servlet 中编写一个简单的“Hello World”程序,它可以毫无问题地运行。但是,当我编写 JSP/HTML 文件并尝试链接它时,这就是问题所在。 您是否尝试过运行此程序?
  • 我没有在这台机器上设置服务器。我正在考虑您首先直接访问html页面。然后点击提交按钮就可以了。然后你面临这个问题。请检查您是否在控制台上获得任何堆栈跟踪。另外,请让我知道您执行的每个操作在浏览器中的 url
  • @kharkar - 运行 HTML 文件后的 URL:localhost:8080/Runtime_Testing/Order_Form.html
猜你喜欢
  • 2020-03-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-01-25
  • 2021-06-21
相关资源
最近更新 更多