Issue
Learning some Java Web development here and trying to start my first servlet. But I am getting this error: HTTP method GET is not supported by this URL
description The specified HTTP method is not allowed for the requested resource (HTTP method GET is not supported by this URL).
I am using the 'get' method in my html form and, as you can see I have the doget method implemented. But I'm not sure why I'm getting this error. Could it have something to do with my web.xml
?
I tried using the POST method by changing the html method to 'post' and by using the doPost method but I get the equivalent error for it, too. I only did this to test and I don't want to use post.
web.xml:
<web-app
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/web-app_2_4.xsd"
version="2.4">
<servlet>
<servlet-name>AppleFox</servlet-name>
<servlet-class>com.AppleFox.web.ProcessQuery</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>AppleFox</servlet-name>
<url-pattern>/ProcessQuery.do</url-pattern>
</servlet-mapping>
</web-app>
Servlet code:
package com.AppleFox.web;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class ProcessQuery
*/
public class ProcessQuery<HttpServletRequest> extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String query = ((ServletRequest) request).getParameter("query");
out.println("Sorry we could find any results for " + query + ".");
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
return;
}
}
I'm also using the 'get' method in my html form. Don't know why I am getting this error.
Solution
Just change the following line and rearrange the imports and everything should work fine:
Change:
public class ProcessQuery<HttpServletRequest> extends HttpServlet {
to
public class ProcessQuery extends HttpServlet {
change the imports to:
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
Answered By - csupnig