My web.xml is like
<web-app version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>sample</display-name>
<servlet>
<servlet-name>Sampleclass</servlet-name>
<servlet-class>sample.SampleClass</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Sampleclass</servlet-name>
<url-pattern>/SampleClass</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>/page/form.jsp</welcome-file>
</welcome-file-list>
</web-app>
and form.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<h1>A simple web application</h1>
<form method="POST" name="Sampleclass" action="SampleClass">
<label for="name">Enter your name </label>
<input type="text" id="name" name="name"/><br><br>
<input type="submit" value="Submit Form"/>
<input type="reset" value="Reset Form"/>
</form>
</body>
</html>
and SampleClass.java is
public class SampleClass extends HttpServlet {
public void init(ServletConfig config) throws ServletException {
super.init(config);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String name = request.getParameter("name");
String age = request.getParameter("age");
PrintWriter out = response.getWriter();
out.write("<html>Hello Your name is "+name +", and your age is "+age+"</html>");
}
public void destroy() {
}
}
but I am getting error when I entered to submit button of form.jsp
and error is
type Status report
message HTTP method POST is not supported by this URL
description The specified HTTP method is not allowed for the requested resource (HTTP method POST is not supported by this URL).
I am not understanding that what is the problem exactly ? Please help..
Your
web.xmland<form>are all perfectly fine. The servlet must be mapped on/servleturland the form action must point toservleturl.The error message also proves that the servlet is perfectly found:
You would otherwise have gotten a
404 Page Not Found(servlet not found) or maybe worse a500 Internal Server Error(servlet failed to execute).The error you got basically means that there’s no
doPost()method. However, your code example do contain it. This can have only one cause: you are not running the servlet class version you think you are running. The one currently deployed does not have thedoPost()method. Clean up everything, recompile/rebuild everything, redeploy the webapp, restart the server and try again.