tags:

views:

122

answers:

1

How can I post XML data from JSP page to server?

A: 

JSP is just a view technology providing a template to write HTML/CSS/JS in and the ability to interact with backend Java code using EL and taglibs like JSTL.

To send some information to the server side in HTML, you'd like to use a form with an input element and a submit button. E.g.

<form action="servlet" method="post">
    <input type="text" name="xml">
    <input type="submit">
</form>

The webbrowser will send the input values as request parameter to the server side. You'd like to create a servlet wherein you just grab the request parameter as follows in the doPost() method:

String xml = request.getParameter("xml");

Instead of a small input field, you can also use a textarea:

    <textarea name="xml"></textarea>

Grabbing the request parameter value in the servlet happens the same way.

If you actually wanted to upload a XML file, then you rather need an <input type="file">:

<form action="servlet" method="post" enctype="multipart/form-data">
    <input type="file" name="xml">
    <input type="submit">
</form>

Obtaining the uploaded file is a whole story apart. The JSP/Servlet API prior to version 3.0 doesn't offer builtin facilities for this. The file (and other kinds of fields) isn't available as request parameter. You'd like to use Apache Commons FileUpload. You can find usage explanation and code examples in this answer.

See also:

BalusC