【发布时间】:2015-04-15 00:03:12
【问题描述】:
我制作了一个简单的 tomcat servlet,完全遵循 Youtube tutorial。
我创建了一个名为 XmlServlet 的类,它处理简单的 GET 和 POST 请求。
这是我的课:
打包优先:
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class XmlServlet extends HttpServlet {
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String UserName = request.getParameter("UserName");
String FullName = request.getParameter("FullName");
System.out.println("hello u" + UserName);
}
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String UserName = request.getParameter("UserName");
String FullName = request.getParameter("FullName");
String location = request.getParameter("location");
String prof = request.getParameter("prof");
out.println("hello u from dpost" + UserName + "u full name" + FullName
+ "address" + location);
}
}
和 Servlet:
package First;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class SimpleServlet
*/
@WebServlet(description = "My First One", urlPatterns = { "/SimpleServletPath" })
public class SimpleServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public SimpleServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
}
还有我的 web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
id="WebApp_ID" version="3.1">
<!--
<servlet>
<servlet-name>XmlServlet</servlet-name>
<servlet-class>First.Xmlservlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>XmlServlet</servlet-name>
<url-pattern>/SimpleServletPath</url-pattern>
</servlet-mapping>
-->
</web-app>
and i create simple html page to represent this
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<form method="get" action="SimpleServletPath">
user name<input name="UserName" /> full name<input name="FullName" />
<br>
Work <input type="radio" name="prof" value="developer">developer</input>
<input type="radio" name="prof" value="IT">IT</input>
<select name="location" multiple size=3 >
<option value="here">here</option>
<option value="home">home</option>
<option value="school">school</option>
<option value="football">football</option>
<option value="chelsea">Chelsea</option>
</select>
</br>
<input type="Submit" />
</form>
</body>
</html>
我无法从 HTML 页面获取任何数据。一切似乎都很好, 但我收到有关 web.xml 的错误。 有人可以帮我检查一下我的 servlet、类和 web.xml 并告诉我问题出在哪里吗?
【问题讨论】:
-
我想你在没有评论 web.xml () 中关于 servlet 的信息的情况下尝试了这个?
-
假设没有它 我忘记删除它,我把 放在我的代码中,因为我谈论 @CoqPwner 的问题
-
@Ascalonian web.xml 的问题,我的 html 页面无法获取任何数据