ablazso
asked on
How to send XML reply back to remote caller from a server based JSP?
I have on server A server based JSP program which does the following:
It is invoked by a client call to receive an XML data stream from a remote client. It inserts some additional tagged data into the XML data, then via https connection, it sends this modified XML data to an other server, B, for processing. The data is processed then the reply will come back to server A and then, unaltered, it will somehow go back to the calling client upon which the server program will terminate.
All this is fine when there are no problems, however, if the IP number of the remote client is INVALID, the server A program should not continue, it should immediately send back an XML formatted error message then terminate. Also, if the server A program cannot connect to server B it should similarly send an XML message back to the client.
My problem is that I don't know how to do either. Can any one show me how to format then send and XML reply back to the calling client?
Please find my short JSP program below (I change the target server name to protect the innocent!)
<%--
/************************* ********** ********** ********** ********** ********
* CardService.jsp - *
************************** ********** ********** ********** ********** *******/
--%>
<%@ taglib uri="http://jakarta.apache.org/taglibs/io-1.0" prefix="io" %>
<%@ taglib prefix="xtags" uri="http://jakarta.apache.org/taglibs/xtags-1.0" %>
<%@ taglib uri="http://jsptags.com/tags/navigation/pager" prefix="pg" %>
<%@ taglib uri="http://jakarta.apache.org/taglibs/i18n-1.0" prefix="i18n" %>
<%@ page import="java.text.*" %>
<%@ page import="java.util.*" %>
<%@ page import="java.io.*" %>
<%@ page import="org.apache.commons .lang.Char SetUtils" %>
<%@ page import="net.easytel.*" %>
<% System.out.println(" ");
System.out.println("====== ========== == CardService ===================");
String ipnumber = request.getRemoteAddr();
System.out.println("=== Remote user's IP Number = [" + ipnumber + "]");
String distributor = null;
DistributorAccessDAO dao = new DistributorAccessDAO();
dao.doSelect(ipnumber);
distributor = dao.getDistributor();
System.out.println("Distri butor: " + distributor );
if (distributor.equals("INVAL ID") {
!!!!!!!!!!! this is where the XML reply then terminate should go to!!!!!!!!!!!!!!!!!
System.out.print("=== [" + ipnumber + "] Not found!")
}
if (distributor.equals("DB-ER ROR") {
!!!!!!!!!!! this is where the XML reply then terminate should go to!!!!!!!!!!!!!!!!!
System.out.print("=== Data base error!")
}
InputStreamReader isr = new InputStreamReader(request. getInputSt ream());
BufferedReader b = new BufferedReader(isr);
String s;
StringBuffer inbuffer = new StringBuffer();
while ((s = b.readLine()) != null){
System.out.println(s);
inbuffer.append(s);
}
int ipoint = inbuffer.indexOf("<CBBRequ est>") + 12;
int iname1 = inbuffer.indexOf("<Name>") + 6;
int iname2 = inbuffer.indexOf("</Name>" );
String inName = inbuffer.substring(iname1, iname2);
System.out.println("=== command recieved=[" + inName + "]");
String xxName = "GetCardList";
if (inName.equals(xxName)) {
System.out.println("=== This is a 'GetCardList' command - 'cad' tag is inserted!");
int ipoint2 = inbuffer.indexOf("</Name>" ) + 7;
String cad = "<ClientAssignedData>" + distributor + "</ClientAssignedData>";
inbuffer.insert(ipoint2,ca d);
}
else {
System.out.println("=== This is not a 'GetCarList' command - 'cad' tag is not inserted!");
}
inbuffer.insert(ipoint,"<A uthenticat ion><SignO nID>Easyte l</SignOnI D><Passwor d>Password </Password ></Authent ication>") ;
System.out.println("=== Modified inbuffer:");
System.out.println(inbuffe r);
b.close();
b = null;
System.out.println("Done - XML data delivered to CBB!");
%>
<io:http url="https://XYZ.yellowbank.com/XYZService.asp" action="POST" input="true" output="true">
<io:header name="Content-Type" value="text/xml"/>
<io:body>
<%=inbuffer%>
</io:body>
</io:http>
!!!!!!!!!!! who do I catch an exception here????
!!!!!!!!!!! this is where the XML reply then terminate should go to!!!!!!!!!!!!!!!!!
It is invoked by a client call to receive an XML data stream from a remote client. It inserts some additional tagged data into the XML data, then via https connection, it sends this modified XML data to an other server, B, for processing. The data is processed then the reply will come back to server A and then, unaltered, it will somehow go back to the calling client upon which the server program will terminate.
All this is fine when there are no problems, however, if the IP number of the remote client is INVALID, the server A program should not continue, it should immediately send back an XML formatted error message then terminate. Also, if the server A program cannot connect to server B it should similarly send an XML message back to the client.
My problem is that I don't know how to do either. Can any one show me how to format then send and XML reply back to the calling client?
Please find my short JSP program below (I change the target server name to protect the innocent!)
<%--
/*************************
* CardService.jsp - *
**************************
--%>
<%@ taglib uri="http://jakarta.apache.org/taglibs/io-1.0" prefix="io" %>
<%@ taglib prefix="xtags" uri="http://jakarta.apache.org/taglibs/xtags-1.0" %>
<%@ taglib uri="http://jsptags.com/tags/navigation/pager" prefix="pg" %>
<%@ taglib uri="http://jakarta.apache.org/taglibs/i18n-1.0" prefix="i18n" %>
<%@ page import="java.text.*" %>
<%@ page import="java.util.*" %>
<%@ page import="java.io.*" %>
<%@ page import="org.apache.commons
<%@ page import="net.easytel.*" %>
<% System.out.println(" ");
System.out.println("======
String ipnumber = request.getRemoteAddr();
System.out.println("=== Remote user's IP Number = [" + ipnumber + "]");
String distributor = null;
DistributorAccessDAO dao = new DistributorAccessDAO();
dao.doSelect(ipnumber);
distributor = dao.getDistributor();
System.out.println("Distri
if (distributor.equals("INVAL
!!!!!!!!!!! this is where the XML reply then terminate should go to!!!!!!!!!!!!!!!!!
System.out.print("=== [" + ipnumber + "] Not found!")
}
if (distributor.equals("DB-ER
!!!!!!!!!!! this is where the XML reply then terminate should go to!!!!!!!!!!!!!!!!!
System.out.print("=== Data base error!")
}
InputStreamReader isr = new InputStreamReader(request.
BufferedReader b = new BufferedReader(isr);
String s;
StringBuffer inbuffer = new StringBuffer();
while ((s = b.readLine()) != null){
System.out.println(s);
inbuffer.append(s);
}
int ipoint = inbuffer.indexOf("<CBBRequ
int iname1 = inbuffer.indexOf("<Name>")
int iname2 = inbuffer.indexOf("</Name>"
String inName = inbuffer.substring(iname1,
System.out.println("=== command recieved=[" + inName + "]");
String xxName = "GetCardList";
if (inName.equals(xxName)) {
System.out.println("=== This is a 'GetCardList' command - 'cad' tag is inserted!");
int ipoint2 = inbuffer.indexOf("</Name>"
String cad = "<ClientAssignedData>" + distributor + "</ClientAssignedData>";
inbuffer.insert(ipoint2,ca
}
else {
System.out.println("=== This is not a 'GetCarList' command - 'cad' tag is not inserted!");
}
inbuffer.insert(ipoint,"<A
System.out.println("=== Modified inbuffer:");
System.out.println(inbuffe
b.close();
b = null;
System.out.println("Done - XML data delivered to CBB!");
%>
<io:http url="https://XYZ.yellowbank.com/XYZService.asp" action="POST" input="true" output="true">
<io:header name="Content-Type" value="text/xml"/>
<io:body>
<%=inbuffer%>
</io:body>
</io:http>
!!!!!!!!!!! who do I catch an exception here????
!!!!!!!!!!! this is where the XML reply then terminate should go to!!!!!!!!!!!!!!!!!
you can see you jsp get kind of messy after you made above changes. consider servlet if you don't like it.
let me know if you have further enquires.
let me know if you have further enquires.
ASKER
Will "} catch(Exception e) { " work without io:body tag fraim correctly?
If I do on the top part of my prgram "out.println" for the XML reply, will "return;" terminate?
How big a problem is to change this to a servlet?
If I do on the top part of my prgram "out.println" for the XML reply, will "return;" terminate?
How big a problem is to change this to a servlet?
yes!
yes, out.println will send your reply to client and return will end the process. that's what you want, isn't it?
you have wrote most of the code in jsp already, the major diff is you'll be using URLConnection in servlet instead of <io:> tags. I can help you on this part. other then that, change to servlet is simple. I assume you have basic knowledge of servlet.
let me know.
yes, out.println will send your reply to client and return will end the process. that's what you want, isn't it?
you have wrote most of the code in jsp already, the major diff is you'll be using URLConnection in servlet instead of <io:> tags. I can help you on this part. other then that, change to servlet is simple. I assume you have basic knowledge of servlet.
let me know.
ASKER
Thank you!!
It would very much like your help with the sevlet conversion.
It would very much like your help with the sevlet conversion.
ASKER
I've made the changes you suggested and both of them worked perfectly!
It seams that the XML response in after the 'catch' must be in raw format, no 'out.println' no quotation, nothing just the text. Am I correct?
Incidentally, my previous comment should have been spelled correctly, like this:
I would very much like your help with the servlet conversion.
It seams that the XML response in after the 'catch' must be in raw format, no 'out.println' no quotation, nothing just the text. Am I correct?
Incidentally, my previous comment should have been spelled correctly, like this:
I would very much like your help with the servlet conversion.
<% } catch( Exception e ) { %>
you error message in xml format
<% } %>
-- or --
<% } catch( Exception e ) {
out.println( "you error message in xml format" );
} %>
they are essentially the same.
you error message in xml format
<% } %>
-- or --
<% } catch( Exception e ) {
out.println( "you error message in xml format" );
} %>
they are essentially the same.
i'll get you servlet equivalent of <io:http> tag.
import java.io.*;
import java.net.*;
...
URL url = new URL( "https://XYZ.yellowbank.com/XYZService.asp" );
URLConnection conn = url.openConnection();
conn.setDoOutput( true );
conn.setDoInput( true );
conn.setRequestProperty( "Content-Type", "text/xml" );
OutputStream urlOut= conn.getOutputStream();
urlOut.write( xmlString.getBytes() );
urlOut.close();
InputStream urlIn = new BufferedInputStream( conn.getInputStream() );
out = response.getOutputStream() ;
int cc = 0;
while( (cc=urlIn.read()) != -1 ) out.write( cc );
urlIn.close();
out.close();
import java.net.*;
...
URL url = new URL( "https://XYZ.yellowbank.com/XYZService.asp" );
URLConnection conn = url.openConnection();
conn.setDoOutput( true );
conn.setDoInput( true );
conn.setRequestProperty( "Content-Type", "text/xml" );
OutputStream urlOut= conn.getOutputStream();
urlOut.write( xmlString.getBytes() );
urlOut.close();
InputStream urlIn = new BufferedInputStream( conn.getInputStream() );
out = response.getOutputStream()
int cc = 0;
while( (cc=urlIn.read()) != -1 ) out.write( cc );
urlIn.close();
out.close();
ASKER
This is just to replaces the <io:http> tag? But what if the connection fails?
Can most of the first part of the JSP program be use in the new servlet?
Can most of the first part of the JSP program be use in the new servlet?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
1. you must set your content type by
<% response.setContentType( "text/xml" ); %>
-or-
<%@ page contentType="text/xml" %>
2. >> !!!!!!!!!!! this is where the XML reply then terminate should go to!!!!!!!!!!!!!!!!!
out.println( "your xml reply string" );
return;
-or-
%>
your xml reply string
<%
return;
3. for the exception:
<% try { %>
<io:http url="https://XYZ.yellowbank.com/XYZService.asp" action="POST" input="true" output="true">
<io:header name="Content-Type" value="text/xml"/>
<io:body>
<%=inbuffer%>
</io:body>
</io:http>
<% } catch( Exception e ) { %>
you error message in xml format
<% } %>