1

I'm writing a Servlet in Java, that basically, gets a request with a XML in the Requests body, and then changes a few things in the XML and redirect/foreword the request with The new XML to a different Servlet that's on the same server, but its on a different web app.

How do redirect/foreword the request with The new XML? can i find code example any where?

this is what i have so far:

protected void doPost(HttpServletRequest request, HttpServletResponse response)
     throws ServletException, IOException {
   String body = getBody(request);
   MapXml mapXml = new MapXml(body,
               "C:\\Projects\\XmlMapper\\output.xml","C:\\Projects\\XmlMapper\\output\\");
   String outputXml = mapXml.getOutputXml();
}
public static String getBody(HttpServletRequest request) throws IOException {
    String body = null;
    StringBuilder stringBuilder = new StringBuilder();
    BufferedReader bufferedReader = null;
    try {
        InputStream inputStream = request.getInputStream();
        if (inputStream != null) {
            bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
            char[] charBuffer = new char[128];
            int bytesRead = -1;
            while ((bytesRead = bufferedReader.read(charBuffer)) > 0) {
                stringBuilder.append(charBuffer, 0, bytesRead);
            }
        } else {
            stringBuilder.append("");
        }
    } catch (IOException ex) {
        throw ex;
    } finally {
        if (bufferedReader != null) {
            try {
                bufferedReader.close();
            } catch (IOException ex) {
                throw ex;
            }
        }
    }
    body = stringBuilder.toString();
    return body;
}

And i have no idea how to continue on from here. I'm new to the servlet world.. Thanks!!! Cheers:)

anubhava
  • 761,203
  • 64
  • 569
  • 643
DasDas
  • 571
  • 3
  • 9
  • 32

3 Answers3

1

If both web-apps are on the same server, i.e. Tomcat

in its META-INF/context.xml set <Context crossContext="true" />

getServletContext().getContext("/app").getRequestDispatcher("f.jsp").forward(..);, 

where app is the name of the other application.

Or what you maybe should do is, Use URLConnection to send request to any URL.

URLConnection connection = new URL(url + "?" + query).openConnection();
connection.setRequestProperty("Accept-Charset", charset);
InputStream response = connection.getInputStream();
// ...

About how to set XML in request, you can carry relatively larger amounts of data in POST request. You can also find the max limit of POST data.

just read the bytes that make up the XML file (using FileInputStream) (if you dont have xml in file, use String to create ur xml)and send them in the POST body. Make sure to set the content encoding accordingly.

I am editing this for the 4th time, to add more details. You can use Apache HTTP Client to post XML easily if its difficult for you to use Java's HTTP client.

String xml = "your xml";
PostMethod post = new PostMethod(strURL);     
try {
    StringRequestEntity requestEntity = new StringRequestEntity(xml);
    post.setRequestEntity(requestEntity); ..
    ....
    ...
Waqas Memon
  • 1,247
  • 9
  • 22
  • and where do i set it to use the new xml? – DasDas Aug 21 '13 at 12:00
  • I have not covered that part. What i wanted to tell you is what you wanted to ask. you should set the xml in request, and then forward it.. or you should use URLConnection and set all your parameters in request and submit that request to the URL.... – Waqas Memon Aug 21 '13 at 12:01
  • but that was my question: HOW DO I SET THE XML IN THE REQUEST? – DasDas Aug 21 '13 at 12:04
  • just read the bytes that make up the XML file (using FileInputStream) (if you dont have xml in file, use String to create ur xml)and send them in the POST body. Make sure to set the content encoding accordingly. – Waqas Memon Aug 21 '13 at 12:09
  • how do i set the post body? – DasDas Aug 21 '13 at 12:15
  • I was just avoiding to tell you everything, your research would teach you numerous things. however, use Apache HTTP client and it would be much easier for you to POST anything – Waqas Memon Aug 21 '13 at 12:21
  • I have edited my answer, see it.. also accept the solutions if it satisfies your needs – Waqas Memon Aug 21 '13 at 12:23
  • i think its great! the post is just like the request object? – DasDas Aug 21 '13 at 12:27
  • yes yes... Its your library to send HTTP GET/POST/PUT and all types of requests... please look at it for a while until u get it. in the meanwhile on stackoverflow you should accept answers that help you actually.. – Waqas Memon Aug 21 '13 at 12:30
0

Because the xml will not be a small body for request,so you have to let the client post the new xml for you. or you can do:

  1. share the same database or cache with remote web service, and forward the key of the data in database or cache.

  2. use HttpClient to post the request for your client, with modified xml, and return the response from remote service to your client.

if you can make sure the xml body is small, you can just using GET method, forward the request to remote server

Lookis
  • 65
  • 4
  • this doesn't help me- there's no database involved – DasDas Aug 21 '13 at 12:00
  • How about use HttpClient to post the request for your client? and return the response. – Lookis Aug 21 '13 at 12:01
  • example? i'm not sure what you mean – DasDas Aug 21 '13 at 12:03
  • HttpClient is a set of tool provided by apache [link](http://hc.apache.org), which do send request like a browser via java code. demo is here [link](http://wiki.apache.org/HttpComponents/QuickStart) – Lookis Aug 21 '13 at 12:08
0

Let's rule out some possibilities first:

  1. You cannot do response.sendRedirect("/otherapp/servlet.do") since it doesn't let you send POST data to another webapp.
  2. You cannot use session since you're sending data across to a different webapp.
  3. You cannot obviously pass full XML in a query string using GET.

Once those possibilities are ruled out only possible way I can think of is this:

  1. Return to the calling page with modified XML and URL of other webapps's servelt in response
  2. Let calling page immediately POST the modified XML to other webapps's servelt using simple Javascript
anubhava
  • 761,203
  • 64
  • 569
  • 643
  • did the question mention Different Webapp? i guess its just different Servlet/JSP, on same server. – Waqas Memon Aug 21 '13 at 11:27
  • This is what OP wrote: `The new XML to a different Servlet that's on the same server,` **`but its on a different web app.`** – anubhava Aug 21 '13 at 11:28
  • You can easily find the code snippet by searching on Google. You need basic Javascript knowledge for that. – anubhava Aug 21 '13 at 12:18