web.xml file
是否必须实际存在于文件中才能运行Servlet程序?
听说在Tomcat 7.0中,您不需要明确地将 web.xml file
写为{{ Servlet类中存在的1}} @WebServlet
会自动在内部构建 web.xml annotation
。
如果是这种情况,是什么提示下面的程序输出以下错误信息?
file
HTTP Status 404 - /Show_Items
状态报告
type
/ Show_Items
message
请求的资源不可用。有人会对此有所了解吗?
Servlet文件
description
Order_Form 即可。的 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
}
}
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>
后
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 < / p>
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
<form action="Show_Items">
异常报告
type
索引:1,大小:1
message
服务器遇到内部错误,无法完成此请求。
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
答案 0 :(得分:1)
在java EE第6版(具有servlet 3.0规范)之后,您无需编写web.xml
文件。您可以使用@ WebServlet注释servlet。在之前的版本中。
你需要做
<form action="Show_Items">
它映射到@WebServlet("/Show_Items")
action
元素应该具有url模式的值,而不是servlet类本身。 @WebServlet
注释将url模式映射到servlet类。这就像在<servlet-mapping>
中指定web.xml
元素一样。
对于IndexOutOfBoundsException,将for循环更改为
for(int i=0; i<previousItems.size(); i++)
在您之前的条件i<=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.