I'm quite new to XML and JSP so here goes. I want to pass an XML message to a jsp via the URL
Created May 7, 2012
Luigi Viggiano JSP and Servlet can -of course- receive the xml encoded in the URL, formatting it as following:
But I don't like so much this: the legth of the GET can become too long, and is not recommended to use long GET requests (there's already a discussion of maximum lenght for GET in the jguru FAQs). So in this way you can send very little xml, in a ugly way...
http://server/page.jsp?<?xml...
or
http://server/page.jsp?docbody=<?xml...
and take the xml.But I don't like so much this: the legth of the GET can become too long, and is not recommended to use long GET requests (there's already a discussion of maximum lenght for GET in the jguru FAQs). So in this way you can send very little xml, in a ugly way...
As jsp are compiled to servlet, you have to know that servlets have input and output streams to communicate with the client. Here I suggest you a simple code about a servlet taking an xml as a stream.
It is composed by a servlet taking an xml book and responding with an html representation of the xml received, and a client (a console application) that sends a book to the servlet displaying the response in the console.
It's very basic, but it can give you an idea of how to proceed.
This is the client:
import java.net.*;
import javax.xml.parsers.*;
import org.w3c.dom.*;
import org.xml.sax.*;
import java.io.*;
public class ServletClient {
public static void main(String[] args) throws Exception{
//open the connection with the servlet
URL url = new URL("http://localhost:8080/servlet/ServletListener");
URLConnection conn = url.openConnection();
//set-up the connection
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setUseCaches(false);
DataOutputStream out = new DataOutputStream( conn.getOutputStream() );
String book =
"<book>"
+"<title>2001 a space odessy</title>"
+"<authors>"
+"<author>Arthur C. Clarke</author>"
+"<author>Stanley Kubrick</author>"
+"</authors>"
+"<price>$12.95</price>"
+"<isbn>0451452739</isbn>"
+"<publisher>N A L</publisher>"
+"<publishdate>July 1993</publishdate>"
+"</book>
";
//sends the xml to the servlet
out.writeBytes(book);
out.flush();
out.close();
//print the response
BufferedReader in =
new BufferedReader(new InputStreamReader(conn.getInputStream())) ;
String s;
while ((s = in.readLine()) != null) {
System.out.println(s);
}
}
}
This is the servlet:
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import javax.xml.parsers.*;
import org.w3c.dom.*;
import org.xml.sax.*;
public class ServletListener extends HttpServlet {
private static final String CONTENT_TYPE = "text/html";
DocumentBuilder docBuilder;
public void init(ServletConfig config) throws ServletException {
super.init(config);
//Set-up the parser
try {
DocumentBuilderFactory docBuilderFact = DocumentBuilderFactory.newInstance();
docBuilder = docBuilderFact.newDocumentBuilder();
} catch (ParserConfigurationException pcEx) {
throw new ServletException("please set-up jaxp!", pcEx);
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//set-up in, out and content-type
InputStream in = request.getInputStream();
PrintWriter out = response.getWriter();
response.setContentType(CONTENT_TYPE);
//parse the request
Document doc = null ;
try {
doc = docBuilder.parse(in);
} catch (SAXException saxEx) {
throw new ServletException("wrong xml!", saxEx);
}
//get needed data from the xml
NodeList titles = doc.getElementsByTagName("title");
NodeList authors = doc.getElementsByTagName("author");
String title = titles.item(0).getFirstChild().getNodeValue();
StringBuffer author = new StringBuffer();
for (int i = 0; i < authors.getLength(); i++) {
if (i != 0)
author.append(',');
author.append(authors.item(i).getFirstChild().getNodeValue());
}
//send the response to the client
out.print("<html><body>");
out.print("<h1>"+title+"</h1>");
out.print("<h2>"+author.toString()+"</h2>");
out.print("</body></html>");
}
}