I have a servlet in my server. And I have another servlet in another server, in some other place. I need to communicate with that remote server, like passing values and retrieving values. I used RequestDispatcher, but it isn't working fine. this is the code of my login screen and two servlets. login.html: <html> <body> <form name="login" method="get" action="http://dev4:8080/examples/servlet/checklogin1"> Username <input type="text" name="username"> Password <input type="password" name="password"> <input type="submit" value="submit"> </form> </body> </html> checklogin1(my servlet in my server): import javax.servlet.http.*; import javax.servlet.*; import java.io.*;
If you read the javadocs for RequestDispatcher, you'll find this for getRequestDispatcher(String path) method: "The pathname must begin with a "/" and is interpreted as relative to the current context root." Since the context is defined for a web application _in_ your servlet container, I don't think you can use RequestDispatcher to go outside the container. Look into java.net.URL or java.net.URLConnection instead. Maybe that would work. Also it looks like you're trying to post form variables by printing them to the response's output stream. That doesn't seem likely to work, even if you were submitting them to a local url. Something like this would normally print your variables to the browser. Also - you cannot print the RequestDispatcher to the response's output stream; it'll just output the results of the dispatcher's toString() method to your browser. Take a look at a book or online tutorial on servlets to find out more about what the PrintWriter of the response object does.