I'm quite new to XML and JSP so here goes. I want to pass an XML message to a jsp via the URL

Luigi Viggiano

JSP and Servlet can -of course- receive the xml encoded in the URL, formatting it as following:



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

        DataOutputStream out = new DataOutputStream( conn.getOutputStream() );
        String book =
            +"<title>2001 a space odessy</title>"
              +"<author>Arthur C. Clarke</author>"
              +"<author>Stanley Kubrick</author>"
            +"<publisher>N A L</publisher>"
            +"<publishdate>July 1993</publishdate>"


        //sends the xml to the servlet

        //print the response
        BufferedReader in =
            new BufferedReader(new InputStreamReader(conn.getInputStream())) ;
        String s;
        while ((s = in.readLine()) != null) {

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 {

        //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();

        //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)

        //send the response to the client