我制作了一个简单的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并告诉我问题所在吗?
答案 0 :(得分:0)
因此,当您在HTML页面上提交并提交时,它将转到servlet并获得一个空白页面,对吧?好吧,那是因为你的servlet在doGet
方法中几乎没有任何东西。请注意,您的表单action
为SimpleServletPath
而不是XmlServlet
。但等等,它变得更糟!在SimpleServlet java文件中,您将带有注释的路径映射到SimpleServletPath
。但是,您还将第二个servlet XmlServlet
映射到与web.xml相同的路径(!)....所以它一团糟。 (由于双重映射,您实际上可能会得到一个错误页面。)
因此,要解决此问题,请将每个servlet映射到其自己的URL,而不是同一URL。然后确保您从HTML表单中实际提交的任何servlet实际上都会在响应中打印一些内容。决定是否要将servlet映射到带有注释的URLS或使用web.xml而不是混合使用这两种方法也是一个好主意。