• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

getting HTTP method GET is not supported by this URL

 
prasad chowdary
Greenhorn
Posts: 18
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Pie
Posts: 42966
73
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13045
6
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Pie
Posts: 42966
73
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 35
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic