【发布时间】: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_Form。html
<!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>
错误堆栈跟踪:更改 <form action="Show_Items">
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: 1java.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
//这是程序运行后屏幕上可见的内容
<body bgcolor="#fdf5e6> <h2>Items purchased</h2> <ul> <li> computer <ul> </body></html>
【问题讨论】:
-
将表单操作更改为
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属性的参数应该是什么? 等待回复。