my dog learned polymorphism*
The moose likes Servlets and the fly likes Problem with sendRedirect Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Problem with sendRedirect" Watch "Problem with sendRedirect" New topic

Problem with sendRedirect

Nikhil Sandilya

Joined: Oct 15, 2002
Posts: 4
I have a text file, which contains some records, each record is separated by a line.
My Java code reads that file line by line and generates an XML String for each line. I want to send this XML String as a Querystring to another URL.
This URL, when it gets the request ( The XML String) sends me a response in the form of an XML file.
I am using sendRedirect method to send the string to this URL.
The problem is i can't send multiple records, the code is a follows
import java.util.*;
import java.text.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.StringTokenizer;
public class XMLRequest extends HttpServlet {

public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException
String action = request.getParameter("action");
if (action.equals("SendXML")) {

String line, XML_string, res;

BufferedReader in = new BufferedReader( new FileReader("C:/2.txt") );
line = in.readLine();
while ( line != null ) // continue until end of file
String[ ] arrayList = readTokens(line, ",");

XML_string = "<?xml%20version=\"1.0\"%20encoding=\"UTF-8\"?>";
XML_string += "<Person>";
XML_string += "<FIRST_NAME>"+removeQuotes( arrayList[0])+"</FIRST_NAME>";
XML_string += "<LAST_NAME>"+ removeQuotes(arrayList[1])+"</LAST_NAME>" ;
XML_string += "<ADDRESS>"+removeQuotes( arrayList[2])+"</ADDRESS> ";
XML_string += "<CITY>"+ removeQuotes(arrayList[3] )+"</CITY>";
XML_string += "<STATE>"+ removeQuotes(arrayList[4])+"</STATE>" ;
XML_string += "<ZIP>"+ removeQuotes(arrayList[5] )+"</ZIP>";
XML_string += "</Person>";
response.sendRedirect("" + XML_string);
line = in.readLine();
catch ( IOException iox )

public static String removeQuotes(String text) {
if ( text.length() == 2)
return " ";
int start = 1;
int end = text.length()-1;
return text.substring(start, end);
public static String[ ] readTokens(String text, String token) {
StringTokenizer parser = new StringTokenizer(text, token);
int numTokens=parser.countTokens( );
String[ ] list = new String[numTokens];
for (int i=0; i < numTokens; i++) {
list[i] = parser.nextToken( );
return list;

When the while loop runs second time, i get illegal state exception at sendRedirect method.
Any tips, why this is happenning or is there any other way of sending the string to the URL.
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
You can't run sendRedirect twice against the same response. sendRedirect commits the response.
If you want the response to come back to the servlet then why not just open a URL object to "" + XML_string instead of trying to use redirect.
The sendRedirect tells the browser that made the request to the servlet to go to that website. I don't think that is what you want to do.

Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog

Joined: Aug 16, 2002
Posts: 8
Why you are not using RequestDispatcher.include (It will include the response from the called JSP/Servlet).
Nikhil Sandilya

Joined: Oct 15, 2002
Posts: 4
Hi All,
Thanks very much for your response.
Instead of using sendRedirct, i am using URL Object
URL url = new URL("" + XML_string);
URLConnection urlc = url.openConnection();
This method works for multiple records, but the response(XML file) doesn't get displayed on the Browser.
Any tips!!
Mike Curwen
Ranch Hand

Joined: Feb 20, 2001
Posts: 3695

But you're looping right? So how many XML files (responses) are you expecting?

A browser is expecting exactly one response to any request. This was basically the root of your problem intially. Once the response is 'commited', that's it.. no further response is possible.

As it is, it looks like you are generating a response for EACH record in your XML. You will need to buffer the response from your second servlet, and once all responses are received, then you can output the results as a single response.

The include directive would work nicely for this, but that would only work within a single webapplication (I think). If you are polling a third party servlet then you need to go the route of URLConnection and buffering all the responses.

One last bit.. if your response is in XML form, then note that you will need to create a fake, enclosing root element. Multiple repsonses will give you multiple root elements, which most browsers (and any validating parsers) will refuse to parse.
[ October 15, 2002: Message edited by: Mike Curwen ]
Nikhil Sandilya

Joined: Oct 15, 2002
Posts: 4
Hi All,
Thanks again for all the tips. They were really very helpful.
Yes, Mike i am looping through and expecting close to 30000 XML request.
I am buffering all the responses in a Vector to display them once i get the final response.
It takes me close 4.5 secs to complete each request. Actually the request goes to some third party application (not sure if it is a servlet), which sends back the XML response.
I am running my servlet on the tomcat server.
Do you think i can reduce the response time by using a different web server.
Thanks again
Best Regards
I agree. Here's the link:
subject: Problem with sendRedirect