I have a problem with my servlet
. I have an HTML page where I call get method for a sum of two numbers when I submit my form it gave me an error. But I already declare get method in my servlet
class.
Type: Status Report
Message: HTTP method POST is not supported by this URL
Description The method received in the request-line is known by the origin server but not supported by the target resource.
Servlet code:
public class AddServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// TODO Auto-generated method stub
int a=Integer.parseInt(req.getParameter("num1"));
int b=Integer.parseInt(req.getParameter("num2"));
int sum=a+b;
resp.getWriter().println(sum+ " doGet method");
}
}
html code:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Servlet</title>
</head>
<body>
<form action="add" method="get" >
Enter 1st number<input type="text" name="num1">
Enter 2st number<input type="text" name="num2">
<input type="submit">
</form>
</body>
</html>
deployment descriptor code: 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_4_0.xsd" id="WebApp_ID" version="4.0">
<display-name>ServletPractice1</display-name>
<servlet>
<servlet-name>AddServlet</servlet-name>
<servlet-class>com.meet.servlet.AddServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>AddServlet</servlet-name>
<url-pattern>/add</url-pattern>
</servlet-mapping>
</web-app>
You do not have a valid doGet() method, when you type the servlet’s path in address bar directly, the web container like Tomcat will try to invoke the doGet() method.
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws IOException{
....
}
Or else Override service method like this:
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request, response); }