我正在运行一个小的servlet程序,但它没有给出预期的输出。Servlet程序运行不正常
Servletfile.java
public class Servletfile extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/ html");
PrintWriter out= response.getWriter();
String abc = request.getParameter("name");
out.print("name="+abc);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
}
的index.html
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<Form action= "welcome" method="get">
Enter Name :<input type = "text" name="name"><br>
<input type = "Submit" value= "login">
</Form>
</body>
</html>
的web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>Practice</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<servlet>
<description></description>
<display-name>Servletfile</display-name>
<servlet-name>Servletfile</servlet-name>
<servlet-class>code.Servletfile</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Servletfile</servlet-name>
<url-pattern>/Servletfile</url-pattern>
</servlet-mapping>
</web-app>
上执行的代码,它显示index.html页面,但如果我输入任何名称作为输入,然后它给404页找不到错误。如果我单独运行servlet程序,它将name = null作为输出。你能给我一些建议吗?
请同时发布你的'web.xml'文件。这对我们能够帮助你至关重要。 – 2015-03-03 07:07:07
它看起来像你的表单没有触及servlet,考虑到你的行为只是简单的“welcome”,但web.xml文件将告诉所有人,这并不奇怪。 – 2015-03-03 07:08:49
我已经添加了web.xml文件,请看看 – Archana 2015-03-03 07:13:21