我的WebContent / jsps文件夹中的JSP文件中有一个HTML表单。我在src文件夹的默认包中有一个servlet类servlet.java。在我的web.xml中,它被映射为/ servlet。
我在HTML表单的action属性中尝试了几个URL:
<form action="/NewServlet">
<form action="/NewServlet.java">
<form action="/src/NewServlet.java">
<form action="../NewServlet.java">
我还更新了web.xml
:
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>NewServlet</servlet-name>
<servlet-class>NewServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>NewServlet</servlet-name>
<url-pattern>/NewServlet</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
但这些都没有奏效。他们都继续返回HTTP 404错误,如下所示: 请求的资源不可用。我正在使用netbeans。
为什么不起作用?
这是我的servlet代码:
public class NewServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
NameHandler myClass = new NameHandler();
String head="<html><body>";
String end="</body></html>";
String button = request.getParameter("button1");
String t=request.getParameter("textp");
if ("SUBMIT".equals(button)) {
if( myClass.isfound(t))
{
out.println(head+" <h1>"+t+"</h1>"+end);
}
}
request.getRequestDispatcher("/WEB-INF/response.jsp").forward(request, response);
}
}
这是我的HTML代码:
<!DOCTYPE html>
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<h1 style="background-image:url(320px-Draft01_wkp.png);font-size:300%;color: white;">GetaDictionary.com</h1>
<form action="${pageContext.request.contextPath}/NewServlet" method="post">
<input type="text" name="name" id="textp"/>
<input type="submit" name="button1" id="Button1" value="SEARCH" />
</form>
</body>
</html>