*
The moose likes Servlets and the fly likes how to acces a remote servlet from a java client????? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "how to acces a remote servlet from a java client?????" Watch "how to acces a remote servlet from a java client?????" New topic
Author

how to acces a remote servlet from a java client?????

sajjad ahmad
Ranch Hand

Joined: Jan 23, 2003
Posts: 78
hi !
let me show you my full code
**********************My servlet code*************************************
import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class HelloWorldExample extends HttpServlet {

public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException
{
ResourceBundle rb =
ResourceBundle.getBundle("LocalStrings",request.getLocale());
response.setContentType("text/html");
}
}
**********************My servlet code*************************************

*********************My Client Code*****************************************
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.*;
import java.io.*;
public class CheckingServlet {
public CheckingServlet() {
}
public static void main (String args[]){
try {
String request = "test";
java.net.URL netUrl = new java.net.URL("http://localhost:8080/servlets-examples/servlet/HelloWorldExample ");
HttpURLConnection connection = (HttpURLConnection) netUrl.openConnection();
connection.setRequestMethod( "GET" );
connection.setRequestProperty("Content-Length", "" + Integer.toString(request.getBytes().length));
connection.setUseCaches(false);
connection.setDoInput(true);
connection.setDoOutput(true);
DataOutputStream out = new DataOutputStream( connection.getOutputStream() );
out.writeBytes( request );
out.flush();
out.close();
BufferedReader reader = new BufferedReader( new InputStreamReader(connection.getInputStream()));
String response = reader.readLine();
while( null != response )
{
System.out.println( response );
response = reader.readLine();
}
}
catch (MalformedURLException ex) {
ex.printStackTrace();
}
catch (IOException ex) {
ex.printStackTrace();
}
}
}
*********************My Client Code*****************************************
For simplicity i have omited all that swing code from my client.....

My servlet is runing properly on server ...when i enter following url in browser i get appropriate result on browser
"http://localhost:8080/servlets-examples/servlet/HelloWorldExample"
but when i try to run my client "CheckingServlet" i get following exception

java.io.IOException: Server returned HTTP response code: 405 for URL: http://localhost:8080/servlets-examples/servlet/HelloWorldExample
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.ja va:789)
at CheckingServlet.main(CheckingServlet.java:22)

I cann't understand this exception ....Can anybody please help me to get rid of this problem.....
Also i want your suggestions on following idea as well
I have a swing client which has to connect to a server remotly ...on server end we should have a controller servlet which handles all the request from similar swing clients from many locations...now i want thy cleint to send a full fledge java object as a request parameter to the servlet and also accept a full flege java object from servlet as a response to it's request is it ok to do....
or we have anyother alternative of doing that
Jeffrey Spaulding
Ranch Hand

Joined: Jan 15, 2004
Posts: 149
Hi

hadn't too much time to check, but you have an extra blank at the end of your URL.

To connect a swing client with a controller servlet we use XML-RPC

Try http://www.xmlrpc.com/

And it works amazingly well

J.
sajjad ahmad
Ranch Hand

Joined: Jan 23, 2003
Posts: 78
ok i have removed that space but getting the same exception again .....anyother suggestions
Roger Chung-Wee
Ranch Hand

Joined: Sep 29, 2002
Posts: 1683
I have a swing client which has to connect to a server remotly ...on server end we should have a controller servlet which handles all the request from similar swing clients from many locations...now i want thy cleint to send a full fledge java object as a request parameter to the servlet and also accept a full flege java object from servlet as a response to it's request is it ok to do....
or we have anyother alternative of doing that

There are various ways of getting a remote client to talk to the server, like RMI, Web Services and JMS. What container are you using? I'm more familiar with WebLogic Server these days (haven't used Tomcat for since 2002), and I'd probably go for RMI-IIOP. If I'm using Web Services, I'd use a JAX-RPC generated stub.

In both cases, use serialized data transfer objects for parameter and result data.

Do note that these are not the only solutions, it's just that I happen to have some familiarity with them.


SCJP 1.4, SCWCD 1.3, SCBCD 1.3
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12821
    
    5
The reason is simple:

closes the socket connection for BOTH output and input. Just flush the output stream - don't do a close until the response has been read.
Bill
sajjad ahmad
Ranch Hand

Joined: Jan 23, 2003
Posts: 78
HI!
i have done that now i am not using out.close(); i am using following code in client
String request = "test";
java.net.URL netUrl = new java.net.URL("http://localhost:8080/servlets-examples/servlet/HelloWorldExample");
HttpURLConnection connection = (HttpURLConnection) netUrl.openConnection();
connection.setRequestMethod( "POST" );
connection.setRequestProperty("Content-Length", "" + Integer.toString(request.getBytes().length));
connection.setUseCaches(false);
connection.setDoInput(true);
connection.setDoOutput(true);
DataOutputStream out = new DataOutputStream( connection.getOutputStream() );
out.writeBytes( request );
out.flush();
connection.getInputStream();
BufferedReader reader = new BufferedReader( new InputStreamReader(connection.getInputStream()));
String response = reader.readLine();
while( null != response )
{
System.out.println( response );
response = reader.readLine();
}

but still getting the same exception
sajjad ahmad
Ranch Hand

Joined: Jan 23, 2003
Posts: 78
Hi!
i have just changed the servlet class and made a new servlet as


**************************servlet code*************************************
import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
import util.HTMLFilter;
public class RequestParamExample extends HttpServlet {
ResourceBundle rb = ResourceBundle.getBundle("LocalStrings");
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException
{
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<html>");
out.println("<body>");
out.println("<head>");

String title = rb.getString("requestparams.title");
out.println("<title>" + title + "</title>");
out.println("</head>");
out.println("<body bgcolor=\"white\">");

out.println("<a href=\"../reqparams.html\">");
out.println("<img src=\"../images/code.gif\" height=24 " +
"width=24 align=right border=0 alt=\"view code\"></a>");
out.println("<a href=\"../index.html\">");
out.println("<img src=\"../images/return.gif\" height=24 " +
"width=24 align=right border=0 alt=\"return\"></a>");

out.println("<h3>" + title + "</h3>");
String firstName = request.getParameter("firstname");
String lastName = request.getParameter("lastname");
out.println(rb.getString("requestparams.params-in-req") + "<br>");
if (firstName != null || lastName != null) {
out.println(rb.getString("requestparams.firstname"));
out.println(" = " + HTMLFilter.filter(firstName) + "<br>");
out.println(rb.getString("requestparams.lastname"));
out.println(" = " + HTMLFilter.filter(lastName));
} else {
out.println(rb.getString("requestparams.no-params"));
}
out.println("<P>");
out.print("<form action=\"");
out.print("RequestParamExample\" ");
out.println("method=POST>");
out.println(rb.getString("requestparams.firstname"));
out.println("<input type=text size=20 name=firstname>");
out.println("<br>");
out.println(rb.getString("requestparams.lastname"));
out.println("<input type=text size=20 name=lastname>");
out.println("<br>");
out.println("<input type=submit>");
out.println("</form>");

out.println("</body>");
out.println("</html>");
out.println(new java.util.Date());
}

public void doPost(HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException
{
doGet(request, response);
}
}
**************************servlet code*************************************
Client code for accessign that servlet is same adn now my clietn is runing ok and i can see that daat set to out in doGet method of servlet on client end .....
Although i could not understand the reason why the last servlet was not working but atleast i have successully run my code....
thanks for all those people who responded to this issue
sajjad ahmed paracha
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: how to acces a remote servlet from a java client?????