So I have a problem with an ordinary Java servlet that creates and
returns an excelfile for me.
The call starts from the client side where I parse my table of data,
creates an xml-file wich I send via a POST message to the servlet.
The servlet recieves the requests, I extract the information from the
xml-file, creates the excelfile and then noting happens at the
browser. Except the messagebox wich displays OK.
I also gets these messages in the Development Shell.
[ERROR] Unable to find 'ExcelFileCreator.gwt.xml' on your classpath;
could be a typo, or maybe you forgot to include a classpath entry for
source?
[WARN] Use of deprecated hosted mode servlet path mapping
[WARN] The client code is invoking the servlet with a URL that is not
module-relative: /ExcelFileCreator
[WARN] Prepend GWT.getModuleBaseURL() to the URL in client code to
create a module-relative URL: /
se.transrail.GWTTidrExcelLookalike.excellookalike/ExcelFileCreator
[WARN] Using module-relative URLs ensures correct URL-independent
behavior in external servlet containers
Could the message be due to the mapping of the servlet? I have just
created a file ExcelFileCreator.gwt.xml with the content shown below.
using gwt 1.5
What am I doing wrong?
/Simon
// xml format
<root>
<header>
<cell_0>header1</cell_1>
<cell_1>header1</cell_2>
...
<cell_x>header1</cell_x>
</header>
<rows>
<row_1>
<cell_0>header1</cell_1>
<cell_1>header1</cell_2>
...
<cell_x>header1</cell_x>
</row_1>
...
</rows>
</root>
// file: ExcelFileCreator.gwt.xml
<module>
<servlet path='/ExcelFileCreator'
class='se.transrail.GWTTidrExcelLookalike.server.servlet.CreateExcelFile'/
>
</module>
// client side
import com.google.gwt.http.client.*;
import com.google.gwt.xml.client.Document;
import com.google.gwt.xml.client.Element;
import com.google.gwt.xml.client.Text;
import com.google.gwt.xml.client.XMLParser;
import com.gwtext.client.widgets.MessageBox;
import java.util.List;
public class ExportGridToExcel {
// TableDecorator is my own class...
public static void exportGridToExcel(List data, TableDecorator
tableDecorator) {
public static void exportGridToExcel(List data, TableDecorator
tableDecorator) {
Document doc = XMLParser.createDocument();
String[] headers = tableDecorator.getColumnHeader();
Element root = doc.createElement("root");
Element headerRoot = doc.createElement("header");
for (int i = 0; i < headers.length; i++) {
String header = headers[i];
Element cellElement = doc.createElement("cell_" +
String.valueOf(i));
Text headerText = doc.createTextNode(header);
cellElement.appendChild(headerText);
headerRoot.appendChild(cellElement);
}
Element rowRoot = doc.createElement("rows");
for (int i = 0; i < data.size(); i++) {
Object rowData = data.get(i);
Object[] row = tableDecorator.getRowInCells(rowData);
Element rowElement = doc.createElement("row_" +
String.valueOf(i));
for (int j = 0; j < row.length; j++) {
Object cell = row[j];
Element cellElement = doc.createElement("cell_" +
String.valueOf(j));
Text headerText = doc.createTextNode(cell.toString());
cellElement.appendChild(headerText);
rowElement.appendChild(cellElement);
}
rowRoot.appendChild(rowElement);
}
root.appendChild(headerRoot);
root.appendChild(rowRoot);
String sendString = root.toString();
RequestBuilder requestBuilder = new
RequestBuilder(RequestBuilder.POST, "/ExcelFileCreator");
try {
Request response = requestBuilder.sendRequest(sendString,
new PostRequest());
} catch (RequestException e) {
MessageBox.alert("Failed to send the request: " +
e.getMessage());
}
}
static class PostRequest implements RequestCallback {
/**
* Called when a pending [EMAIL PROTECTED]
com.google.gwt.http.client.Request} completes
* normally. Note this method is called even when the status
code of the
* HTTP response is not "OK", 200.
*
* @param request the object that generated this event
* @param response an instance of the
* [EMAIL PROTECTED]
com.google.gwt.http.client.Response}
class
*/
public void onResponseReceived(Request request, Response
response) {
// recives the file here as a request??? I can see some data in
the
request
MessageBox.alert("OK");
}
/**
* Called when a [EMAIL PROTECTED] com.google.gwt.http.client.Request}
does not complete
* normally. A [EMAIL PROTECTED]
com.google.gwt.http.client.RequestTimeoutException
RequestTimeoutException} is
* one example of the type of error that a request may
encounter.
*
* @param request the request object which has experienced
the error condition
* @param exception the error that was encountered
*/
public void onError(Request request, Throwable exception) {
MessageBox.alert("Error:" + exception.getMessage());
}
}
}
// server side...
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import se.transrail.common.utils.ExcelUtils;
import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import java.io.IOException;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.List;
public class CreateExcelFile extends HttpServlet {
public void doPost(HttpServletRequest request, HttpServletResponse
response) {
try {
int length = 1024; //A big number
byte[] buffer = new byte[length];
int offset = 0;
int bytesRead = 0;
ServletInputStream bufferedReader =
request.getInputStream();
String inputXml = "";
StringBuffer stringBuffer = new StringBuffer();
while ((bytesRead = bufferedReader.readLine(buffer,
offset, length)) != -1) {
inputXml += new String(buffer, offset, bytesRead);
}
// here I have a correct string with all the data
// Get Document Builder Factory
DocumentBuilderFactory factory =
DocumentBuilderFactory.newInstance();
// Turn on validation, and turn off namespaces
factory.setValidating(false);
factory.setNamespaceAware(true);
// Obtain a document builder object
DocumentBuilder builder = factory.newDocumentBuilder();
Document document = builder.parse(new InputSource(new
StringReader(inputXml)));
List<String> headerList = new ArrayList<String>();
List<String[]> rowList = new ArrayList<String[]>();
NodeList headerRoot =
document.getElementsByTagName("header").item(0).getChildNodes();
for (int j = 0; j < headerRoot.getLength(); j++) {
Node cell = headerRoot.item(j);
headerList.add(cell.getTextContent());
}
NodeList rowRoot =
document.getElementsByTagName("rows").item(0).getChildNodes();
for (int j = 0; j < rowRoot.getLength(); j++) {
Node row = rowRoot.item(j);
String[] rowData = new String[headerList.size()];
for (int k = 0; k < row.getChildNodes().getLength(); k+
+) {
rowData[k] =
row.getChildNodes().item(k).getTextContent();
}
rowList.add(rowData);
}
// parsing is ok...
response.setContentType("application/vnd.ms-excel");
response.setHeader("Content-Disposition", "attachment;
filename=test.xls");
HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet sheet = ExcelUtils.getSheet(workbook, "test");
ExcelUtils.writeCellValues(sheet, 0,
headerList.toArray());
for (int j = 0; j < rowList.size(); j++) {
ExcelUtils.writeCellValues(sheet, j + 1,
rowList.get(j));
}
// writing to the stream and closing it works fine
workbook.write(response.getOutputStream());
response.getOutputStream().close();
} catch (IOException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} finally {
int a = 0;
}
}
}
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Google Web Toolkit" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/Google-Web-Toolkit?hl=en
-~----------~----~----~----~------~----~------~--~---