File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes getting HTTP method GET is not supported by this URL Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "getting HTTP method GET is not supported by this URL" Watch "getting HTTP method GET is not supported by this URL" New topic
Author

getting HTTP method GET is not supported by this URL

prasad chowdary
Greenhorn

Joined: Feb 18, 2012
Posts: 18
htmlform

<body>
<form name="customerform" action="/Customer/cs">
customerid:<input type="text" name="cid"/>
customerName:<input type="text" name="cname"/>
<input type="submit" value="store"/>
</form>
</body>

Servlet

import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;
import java.io.*;

public class CustomerServlet extends HttpServlet{

public void Service(HttpServletRequest req,HttpServletResponse res){
try{
System.out.println("1");
DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
Connection conn = DriverManager.getConnection("jdbcracle:thin:@localhost:1521:xe","prasad","abc");
Statement stmt = conn.createStatement();
String id = req.getParameter("cid");
String name = req.getParameter("cname");
int result=stmt.executeUpdate("insert into customer values('"+id+"','"+name+"')");
PrintWriter out = res.getWriter();
out.write(id);
out.write(name);
conn.close();
}catch(Exception e){}
}
}


web.xml


<web-app>
<servlet>
<servlet-name>csservlet</servlet-name>
<servlet-class>CustomerServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>csservlet</servlet-name>
<url-pattern>/cs</url-pattern>
</servlet-mapping>
</web-app>
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42276
    
  64
Correct: That's what the HttpServlet.doGet method will do if you do not override it. It looks like you're trying to override the service method instead (but you're not, because you misspelt it) - there are a couple of reasons why you should not do that: firstly, service does more than just dispatch to the doXYZ methods, and secondly, that will cause GET, HEAD and POST requests to be handled the same - which violates the HTTP specification.

Also note that "catch (Exception e) { }" is not a good idea - ignoring exceptions is unwise. You should at the least print the exception message to the log, so that you'll know if something goes wrong.


Ping & DNS - my free Android networking tools app
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12803
    
    5
Also, code like this:


will cause you lots of problems.

The reason being that you do not check for null values from the getParameter() calls. You may be completely sure that there will always be a usable value there but it always pays to check.

Furthermore:

your servlet and ALL classes used in a servlet/jsp envrionment MUST be in a package as we have explained many many many times before.

Bill
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42276
    
  64
Also be sure to submit state changes via POST only.

And use PreparedStatements - as it is, your code is wide open to SQL injection attacks.
Marshall Blythe
Ranch Hand

Joined: Feb 26, 2013
Posts: 32
William Brogden wrote:Also, code like this:

will cause you lots of problems.

The reason being that you do not check for null values from the getParameter() calls. You may be completely sure that there will always be a usable value there but it always pays to check.


This code also exposes a potential SQL injection vulnerability.

edit- Doh! Ulf beat me to it!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: getting HTTP method GET is not supported by this URL