Servlet question --- getting 505 error....sometimes

Discussion in 'Java' started by Larry Coon, Dec 2, 2005.

  1. Larry Coon

    Larry Coon Guest

    Here's an SSCCE. First a simple servlet (Tomcat 5.5):

    - - - begin TestServlet.java - - -

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

    public class TestServlet extends HttpServlet {
    protected void doGet(HttpServletRequest req, HttpServletResponse res)
    throws ServletException, IOException {
    System.out.println("In doGet()");
    }
    }

    - - - end TestServlet.java - - -

    This servlet just logs "In doGet()" to Tomcat's stdout_xxx.log file
    whenever it's called. Using a web browser to invoke the servlet, here
    are a couple of URLs that work just fine:

    http://localhost:8080/larry/test?cmd=abc

    http://localhost:8080/larry/test?cmd=abc 123

    Both of the above URLs work fine from my browser. In both cases, the
    message is logged to Tomcat's log file, and no errors are produced.

    Now here's a Swing app to call the servlet:

    - - - begin TestApplication.java - - -

    import java.awt.*;
    import java.awt.event.*;
    import java.net.*;
    import javax.swing.*;

    public class TestApplication extends JFrame {
    private JTextField cmdField;

    public TestApplication() {
    super("Test Servlet Call");

    Container container = getContentPane();

    cmdField = new JTextField();
    cmdField.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
    processCommand(cmdField.getText());
    }
    });
    container.add(cmdField, BorderLayout.NORTH);

    setDefaultCloseOperation(EXIT_ON_CLOSE);
    setSize(400, 400);
    setVisible(true);
    }

    private void processCommand(String cmd) {
    try {
    URL url = new URL(cmd);
    URLConnection urlConnection = url.openConnection();

    urlConnection.getInputStream();
    } catch (Exception x) {
    x.printStackTrace();
    }
    }

    public static void main(String[] args) {
    new TestApplication();
    }
    }

    - - - end TestApplication.java - - -

    This application invokes the same servlet via the same URLs as the web
    browser. When I use this URL:

    http://localhost:8080/larry/test?cmd=abc

    Everything works fine -- the message appears in Tomcat's log. However,
    when I use this URL:

    http://localhost:8080/larry/test?cmd=abc 123

    (i.e., if I add a space) I get an IOException that the web server
    returned
    a 505 error (and the "In doGet()" message does not appear in the log)

    Can anyone tell me why the call to the same servlet returns a 505 error
    if
    called by the Swing app, but works fine if called by a browser?

    Note: In the previous version of the SCCEE, the servlet actually got the
    content of the cmd parameter and echoed that to the log. When called
    from
    the browser, it echoed the full content of the parameter, including the
    space, to the log. So I know the servlet was seeing and processing the
    URL correctly, even with the space, when I used a browser.
     
    Larry Coon, Dec 2, 2005
    #1
    1. Advertising

  2. Larry Coon

    Chris Smith Guest

    Larry Coon <> wrote:
    > This application invokes the same servlet via the same URLs as the web
    > browser. When I use this URL:
    >
    > http://localhost:8080/larry/test?cmd=abc
    >
    > Everything works fine -- the message appears in Tomcat's log. However,
    > when I use this URL:
    >
    > http://localhost:8080/larry/test?cmd=abc 123
    >
    > (i.e., if I add a space) I get an IOException that the web server
    > returned a 505 error (and the "In doGet()" message does not appear in
    > the log)


    A valid URL is required to escape certain characters using %xx, where
    'x' is a hexadecimal digit. Your web browser knows this, and is
    performing the conversion for you. Java's URL class doesn't do this
    properly, and therefore causes an error. This is a bug in the class
    java.net.URL.

    Unfortunately, because some older programs might rely on the broken
    behavior, Sun has refused to fix the bug. Instead, they have created a
    new class called java.net.URI (Java 1.4 and later). The correct way to
    create a URL object is now:

    URL url = new URI("...").toURL();

    --
    www.designacourse.com
    The Easiest Way To Train Anyone... Anywhere.

    Chris Smith - Lead Software Developer/Technical Trainer
    MindIQ Corporation
     
    Chris Smith, Dec 3, 2005
    #2
    1. Advertising

  3. Larry Coon

    Roedy Green Guest

    On Fri, 02 Dec 2005 15:45:59 -0800, Larry Coon <>
    wrote, quoted or indirectly quoted someone who said :

    >
    > http://localhost:8080/larry/test?cmd=abc 123
    >
    >(i.e., if I add a space) I get an IOException that the web server
    >returned
    >a 505 error (and the "In doGet()" message does not appear in the log)
    >
    >Can anyone tell me why the call to the same servlet returns a 505 error
    >if
    >called by the Swing app, but works fine if called by a browser?


    use a packet sniffer. You will see the message the browser sends is
    not quite what you type.

    See http://mindprod.com/jgloss/sniffer.html
    --
    Canadian Mind Products, Roedy Green.
    http://mindprod.com Java custom programming, consulting and coaching.
     
    Roedy Green, Dec 3, 2005
    #3
  4. Larry Coon

    Larry Coon Guest

    Chris Smith wrote:

    > A valid URL is required to escape certain characters using %xx, where
    > 'x' is a hexadecimal digit. Your web browser knows this, and is
    > performing the conversion for you.


    Thanks Chris (and Roedy). What threw me here is that when I
    log the message on the server side, it includes the space when
    called by the browser. So when I do this from the browser:

    http://localhost:8080/larry/test?cmd=abc 123

    then "abc 123" is logged. However, if I do -this- from the
    browser:

    http://localhost:8080/larry/test?cmd=abc 123

    Then "abc%20123" is logged. I had suspected that the browser
    was converting the space to %20, but that wouldn't explain how
    the %20 was being logged as a space if and only if the browser
    had converted it (any ideas why?).

    > Java's URL class doesn't do this
    > properly, and therefore causes an error. This is a bug in the class
    > java.net.URL.
    >
    > Unfortunately, because some older programs might rely on the broken
    > behavior, Sun has refused to fix the bug. Instead, they have created a
    > new class called java.net.URI (Java 1.4 and later). The correct way to
    > create a URL object is now:
    >
    > URL url = new URI("...").toURL();


    Didn't know this....thanks!


    Larry Coon
    University of California
     
    Larry Coon, Dec 4, 2005
    #4
  5. Larry Coon

    Larry Coon Guest

    Re: Servlet question --- another application vs browser question

    I have another case where the servlet's response is different
    depending on whether I'm using a browser or a Java app.

    My servlet is returning an XML document, which contains a DOCTYPE
    SYSTEM. If I imvoke the servlet from a browser, the DOCTYPE SYSTEM
    appears in the response.

    I also wrote an application that uses a Transformer to transform
    the XML document to a String, then I display the String in a
    JTextArea. Something like (I'll provide an SSCCE if needed, but
    for now, just an excerpt):

    // The TransformerFactory transformer and DocumentBuilder builder
    // are already instantiated.
    transformer.setOutputProperty("indent", "yes");
    URL url = new URI(cmdField.getText()).toURL();
    URLConnection urlConnection = url.openConnection();

    Document document = builder.parse(urlConnection.getInputStream());
    Source xmlSource = new DOMSource(document);
    StringWriter writer = new StringWriter();

    transformer.transform(xmlSource, new StreamResult(writer));

    textArea.setText(writer.toString());

    - - - end sample code - - -

    When the transformed document is displayed in the textArea, everything
    is there EXCEPT for the DOCTYPE SYSTEM (which, again, is there when I
    use a browser).

    I looked for arguments to setOutputProperty() that tell the transformer
    to include the DOCTYPE SYSTEM it finds in the document, but I didn't
    see anything that would work. The DOCTYPE_SYSTEM output property is
    used to set it to a specific URL.

    How do I get the DOCTYPE SYSTEM to appear?
     
    Larry Coon, Dec 5, 2005
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. John Saunders
    Replies:
    0
    Views:
    469
    John Saunders
    Aug 28, 2003
  2. circuit_breaker
    Replies:
    2
    Views:
    2,013
    Jack Jia
    Apr 4, 2004
  3. SPG
    Replies:
    2
    Views:
    16,952
  4. Nehmo Sergheyev
    Replies:
    1
    Views:
    507
    Andrew Urquhart
    May 9, 2004
  5. Marcin Vorbrodt

    ::std sometimes needed, sometimes not

    Marcin Vorbrodt, Sep 16, 2003, in forum: C++
    Replies:
    24
    Views:
    764
    Jerry Coffin
    Sep 17, 2003
Loading...

Share This Page