• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

getting HTTP method GET is not supported by this URL

 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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("jdbc:oracle: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>
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
reply
    Bookmark Topic Watch Topic
  • New Topic