我已经尝试了一切来使这个servlet正常工作,并且在下载了几个较旧的JRE和几个旧版本的Apache tomcat之后,它仍然无法正常工作。现在我正在使用Kepler for Java EE和JRE v1.7以及Tomcat v7。下面是我的java源代码和html - 我怀疑问题出在doGet方法本身,因为我认为我的文件路径命名正确,但我仍然返回一个空白页面。虽然问题可能在于形式动作 - 我对eclipse完全不熟悉。
<!DOCTYPE html>
<html>
<head> <meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<form action="/HelloWorld/HelloServlet" method="get">
<input type="submit" value="go to hello servlet"/>
</form>
</body>
</html>`
HelloServaled.java
package com.tutorials.servlets;
import java.io.IOException;
import java.io.PrintWriter;
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 HelloServlet
*/
@WebServlet("/HelloServlet")
public class HelloServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public HelloServlet() {
super();
// TODO Auto-generated constructor stub
}
/* @see Servlet#destroy()
*/
public void destroy() {
// TODO Auto-generated method stub
}
/**
* @see HttpServlet#service(HttpServletRequest request, > HttpServletResponse response)
*/
protected void service(HttpServletRequest request, HttpServletResponse > response) throws ServletException, IOException {
// TODO Auto-generated method stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, > HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse > response) throws ServletException, IOException {
PrintWriter out = response.getWriter();
out.println("I am in go get mehtod" );
// TODO Auto-generated method stub
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, > HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse > response) throws ServletException, IOException {
// TODO Auto-generated method stub
}
/**
* @see HttpServlet#doPut(HttpServletRequest, HttpServletResponse)
*/
protected void doPut(HttpServletRequest request, HttpServletResponse > response) throws ServletException, IOException {
// TODO Auto-generated method stub
}
}
答案 0 :(得分:0)
您的主要问题在于您的表单action
属性与您的servlet映射不同。
您的注释:(@WebServlet("/HelloServlet")
)告诉应用服务器监听对&#34; / HelloServlet&#34;的请求,例如:http://eample.com/HelloServlet
它是什么。
另一方面,您的表单将其操作设置为/HelloWorld/HelloServlet
,这意味着帖子请求转到http://example.com/HelloWorld/HelloServlet
因此,当您的浏览器在那里发布时,应用程序会处理它应该如何处理它,因为您的servlet没有在那里听任何东西。