L
Larry Coon
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.
- - - 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.