0

Background:

I am trying to send data from a class called from an applet to a servlet all on the same server.

public class SendData {
    public void send() {
        URL url = new URL("http://address.edu/folder/package/Servlet.class");
        URLConnection conn = url.openConnection();
        conn.setUseCaches(false);
        conn.setDoInput(true);
        conn.setDoOutput(true);
        ObjectOutputStream objout = new ObjectOutputStream(conn.getOutputStream());
        Object data = new Object();
        objout.writeObject(data);
        objout.flush();
        objout.close();
}}

The servlet looks like:

public class Servlet extends HttpServlet {
    public void doPost(HttpServletRequest req, HttpServletResponse resp) {
        ObjectInputStream objin = new ObjectInputStream(req.getInputStream());
        Object input = objin.readObject();
        objin.close();
        resp.setStatus(HttpServletResponse.SC_OK);
        ...// do other things
}}

Problem:

The error returned is java.io.IOException: Server returned HTTP response code: 405 for URL: http://address.edu/folder/Servlet.class. I am using my school's server, and I am not 100% sure, but I don't think the server allows the doPost method.

Is there any way around this? Could I somehow connect via ftp to the servlet.class if I provide the username and password that grants access? I am a complete beginner with this, so any advice/suggestion would be greatly appreciated!

EDIT: added web.xml

    <web-app xmlns="http://java.sun.com/xml/ns/j2ee" version="2.5"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  <servlet>
    <servlet-name>Servlet</servlet-name>
    <servlet-class>package.Servlet</servlet-class>
  </servlet>

  <servlet-mapping>
    <servlet-name>Servlet</servlet-name>
    <url-pattern>/Servlet</url-pattern>
  </servlet-mapping>
</web-app>
GreenBeans
  • 33
  • 1
  • 4

1 Answers1

0

Assuming that everything is configured correctly on your server (web.xml, URL mapping etc.), you have to specify also to use the POST method on the client.

URLConnection to a http:// URL uses the GET method by default.

If you are sure that this request will allways be a HTTP request, then you can cast to HttpURLConnection, which allows you to set the request method:

  URL url = new URL("http://address.edu/folder/Servlet.class");
  conn = (HttpURLConnection)url.openConnection();
  conn.setRequestMethod("POST");
  ...
Udo Klimaschewski
  • 4,827
  • 1
  • 25
  • 40
  • This returns the same error. Would the configurations you mentioned have an effect on allowing doPost method? If so, where do I start? – GreenBeans Dec 06 '12 at 13:30
  • That is then an entirely different thing. You should then show us your web.xml (Especially the Servlet and ServletMapping parts) and probably related server log entries. – Udo Klimaschewski Dec 06 '12 at 13:53
  • I added web.xml, but not sure how I can get server log entries. – GreenBeans Dec 06 '12 at 15:43
  • Try URL `http://address.edu/folder/Servlet` without the .class at the end. That is how you mapped your Servlet class to an URL in web.xml. (If your web-application is called "folder") – Udo Klimaschewski Dec 06 '12 at 15:50
  • Now this is getting late over here on my side of the planet, but who throws that FileNotFoundException? Client? Server? What statement in your code? (Stacktrace) – Udo Klimaschewski Dec 06 '12 at 18:59