Home arrow static arrow Java Programming [Archive] - How to acheive this???? J2EE gurus
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - How to acheive this???? J2EE gurus
This topic has 14 replies on 1 page.

Posts:100
Registered: 1/7/02
How to acheive this???? J2EE gurus  
Jul 21, 2004 4:55 AM



 
Hi,

I have servletA in one serverA and servletB in ServerB.When a request is made to servletA it should call the ServletB to get the response.

Basically they should be chained.

I understand that to forwarding a request to a servlet which is in a different context we have to use redirect instead of forward.if we user redirect the server issses a new request.

How can i acheive this???

Thanks in advance.

 

Posts:29
Registered: 99-07-02
Re: How to acheive this???? J2EE gurus  
Jul 21, 2004 5:27 AM (reply 1 of 14)



 
I'm sure there's a better way to do this, but one idea might be to use the URL class to open a connection to the servlet on the other server.

Though, I'm not sure I understand why redirecting is a bad thing, especially since you seem to want to pass off processing to the second servlet. Why do you not want to simply redirect?
 

Posts:100
Registered: 1/7/02
Re: How to acheive this???? J2EE gurus  
Jul 21, 2004 5:39 AM (reply 2 of 14)



 
Thanks for your response.

Let me rephrase my question.

Is servlet chaining possible if servlets reside on different servers??

Can i achieve this using filters???

thanks
 

Posts:29
Registered: 99-07-02
Re: How to acheive this???? J2EE gurus  
Jul 21, 2004 5:45 AM (reply 3 of 14)



 

Is servlet chaining possible if servlets reside on
different servers??

As far as I know, I don't think so.

Can i achieve this using filters???

I don't think so.

I would perhaps look into URL and URLConnection to see if these can handle what you want to do.

Hope this helps...
 

Posts:4,496
Registered: 19/06/02
Re: How to acheive this???? J2EE gurus  
Jul 21, 2004 5:45 AM (reply 4 of 14)



 
Is servlet chaining possible if servlets reside on
different servers??

Sure. It's not something you'd usually do though.

Can i achieve this using filters???

Don't think so since filters are configured in the web.xml file.

You might want to look at RequestDispatcher.
 

Posts:29
Registered: 99-07-02
Re: How to acheive this???? J2EE gurus  
Jul 21, 2004 5:47 AM (reply 5 of 14)



 
Thanks, YoGee. Looks like he knows more than me. I'd follow his advice before mine...
 

Posts:4,496
Registered: 19/06/02
Re: How to acheive this???? J2EE gurus  
Jul 21, 2004 5:49 AM (reply 6 of 14)



 
You might want to look at RequestDispatcher.

Actually don't do that it won't work .... look at HttpURLConnection.
 

Posts:4,496
Registered: 19/06/02
Re: How to acheive this???? J2EE gurus  
Jul 21, 2004 5:50 AM (reply 7 of 14)



 
Thanks, YoGee. Looks like he knows more than me. I'd
follow his advice before mine...

LOL. Not sure about that ...
 

Posts:100
Registered: 1/7/02
Re: How to acheive this???? J2EE gurus  
Jul 21, 2004 7:28 AM (reply 8 of 14)



 
Thanks for ur responses.

I cannot use HttpURLConnection as my client is a PDA and it expects Reponse object back for that request.

Any more ideas please.

Thanks
 

Posts:4,496
Registered: 19/06/02
Re: How to acheive this???? J2EE gurus  
Jul 21, 2004 7:57 AM (reply 9 of 14)



 
javax.microedition.io.HttpConnection ?
 

Posts:4,496
Registered: 19/06/02
Re: How to acheive this???? J2EE gurus  
Jul 21, 2004 7:59 AM (reply 10 of 14)



 
I cannot use HttpURLConnection as my client is a PDA
and it expects Reponse object back for that request.

What a minute ... what's that got to do with anything? You're going from a servlet on one server to a servlet on another server. Who cares what your client is?
 

Posts:100
Registered: 1/7/02
Re: How to acheive this???? J2EE gurus  
Jul 21, 2004 8:44 AM (reply 11 of 14)



 
The Client is imp bcos it is the one which receives the response.

I would like to one thing.

if PDA makes a request to ServletA which is on ServerA and the ServletA makes an HttpURLConnection
and contacts ServletB which is on ServletB, how can i inlcude the response sent by servletB into ServletA response????

ServletB is writing everything into response object.

Thanks

 

Posts:29
Registered: 99-07-02
Re: How to acheive this???? J2EE gurus  
Jul 21, 2004 10:14 AM (reply 12 of 14)



 
Off the top of my head, servletA would have to read from the input stream that the URLConnection provides you to get the response information from ServletB (which will have written its response to its output stream). In other words, the URLConnection in ServletA hooks up its InputStream to ServletB's OutputStream.

I agree with YoGee that the client shouldn't really matter in how the server does its processing (it only matters in what content the server sends in its response).

As a sidenote, the API contains some helpful information as well about creating and using these connections (you can find the API linked from java.sun.com).

I hope this makes sense.
 

Posts:3,258
Registered: 00-08-28
Re: How to acheive this???? J2EE gurus  
Jul 21, 2004 3:35 PM (reply 13 of 14)



 
The Client is imp bcos it is the one which receives
the response.

I would like to one thing.

if PDA makes a request to ServletA which is on
ServerA and the ServletA makes an HttpURLConnection
and contacts ServletB which is on ServletB, how can i
inlcude the response sent by servletB into ServletA
response????

ServletB is writing everything into response object.

Thanks


You could use a method like this. Though you might have to modify ot as per your requirements

private void returnSuccessfullNotifyHttpResponse(HttpServletRequest request, HttpServletResponse response, String pushId)                                         throws ServletException, IOException {       String responseString =null;    PrintWriter out=null;    String replyTime = null;     Calendar  date = Calendar.getInstance(TimeZone.getTimeZone("GMT"));    String year,month,day,hour,minute,second;     year=Integer.toString(date.get(Calendar.YEAR));    month=Integer.toString(date.get(Calendar.MONTH) + 1 );    if (month.length() == 1)      month = "0" + month;    day=Integer.toString(date.get(Calendar.DAY_OF_MONTH));    if (day.length() == 1)      day = "0" + day;    hour=Integer.toString(date.get(Calendar.HOUR_OF_DAY));    if (hour.length() == 1)      hour = "0" + hour;    minute=Integer.toString(date.get(Calendar.MINUTE));    if (minute.length() == 1)      minute = "0" + minute;    second=Integer.toString(date.get(Calendar.SECOND));    if (second.length() == 1)      second = "0" + second;     replyTime = year+ "-" + month + "-" + day+ "T" + hour+ ":" + minute + ":" + second + "Z";     responseString=            XML_HEADER +               PAP_STAG + CRLF +                 PUSH_RESPONSE_STAG +                   PUSH_ID + pushId + CLOSE_QUOTE + SPACE +                  SENDER_ADDRESS + "http://" + request.getServerName() + ":"+  request.getServerPort() + request.getServletPath() + CLOSE_QUOTE + SPACE +                  SENDER_NAME + SPACE +                  REPLY_TIME + replyTime + CLOSE_QUOTE + CLOSE_TAG + CRLF +                  RESPONSE_RESULT_STAG +                    CODE + "1001" + CLOSE_QUOTE +                    DESC + "The request accepted for processing" + CLOSE_QUOTE + CLOSE_TAG +                  RESPONSE_RESULT_ETAG + CRLF +                PUSH_RESPONSE_ETAG + CRLF +              PAP_ETAG;     response.setContentType("application/xml");    //response.setHeader("Connection", "keep-alive");    response.setStatus(response.SC_ACCEPTED);    response.setContentLength(responseString.length());     out = new PrintWriter (response.getOutputStream());     out.print(responseString);    out.close();   }
 

Posts:3,258
Registered: 00-08-28
Re: How to acheive this???? J2EE gurus  
Jul 21, 2004 3:36 PM (reply 14 of 14)



 
The Client is imp bcos it is the one which receives
the response.

I would like to one thing.

if PDA makes a request to ServletA which is on
ServerA and the ServletA makes an HttpURLConnection
and contacts ServletB which is on ServletB, how can
i
inlcude the response sent by servletB into ServletA
response????

ServletB is writing everything into response object.

Thanks


You could use a method like this. Though you might
have to modify ot as per your requirements

private voidreturnSuccessfullNotifyHttpResponse(HttpServletRequestrequest, HttpServletResponse response, String pushId)throws ServletException, IOException {   String responseString =null;PrintWriter out=null;String replyTime = null; Calendar  date =Calendar.getInstance(TimeZone.getTimeZone("GMT"));String year,month,day,hour,minute,second; year=Integer.toString(date.get(Calendar.YEAR));month=Integer.toString(date.get(Calendar.MONTH) + 1 );if (month.length() == 1)month = "0" + month;day=Integer.toString(date.get(Calendar.DAY_OF_MONTH));if (day.length() == 1)day = "0" + day;hour=Integer.toString(date.get(Calendar.HOUR_OF_DAY));if (hour.length() == 1)hour = "0" + hour;minute=Integer.toString(date.get(Calendar.MINUTE));if (minute.length() == 1)minute = "0" + minute;second=Integer.toString(date.get(Calendar.SECOND));if (second.length() == 1)second = "0" + second; replyTime = year+ "-" + month + "-" + day+ "T" + hour+":" + minute + ":" + second + "Z"; responseString=XML_HEADER +PAP_STAG + CRLF +PUSH_RESPONSE_STAG +PUSH_ID + pushId + CLOSE_QUOTE + SPACE +SENDER_ADDRESS + "http://" + request.getServerName() +":"+  request.getServerPort() +request.getServletPath() + CLOSE_QUOTE + SPACE +SENDER_NAME + SPACE +REPLY_TIME + replyTime + CLOSE_QUOTE + CLOSE_TAG +CRLF +RESPONSE_RESULT_STAG +CODE + "1001" + CLOSE_QUOTE +DESC + "The request accepted for processing" +CLOSE_QUOTE + CLOSE_TAG +RESPONSE_RESULT_ETAG + CRLF +PUSH_RESPONSE_ETAG + CRLF +PAP_ETAG; response.setContentType("application/xml");//response.setHeader("Connection", "keep-alive");response.setStatus(response.SC_ACCEPTED);response.setContentLength(responseString.length()); out = new PrintWriter (response.getOutputStream()); out.print(responseString);out.close();}

and forgotto mention that you would call this method in the service method of servlet B
 
This topic has 14 replies on 1 page.