-1

this is my simple Java servlet which generate XML code retreiving it from a simple database:

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.*;
import java.io.*

@WebServlet("/XML")
public class XML extends HttpServlet {
   private static final long serialVersionUID = 1L;

    public XML() {
        super();
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}

/**
 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
 */
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    Connection connection;
    Statement statement;

    response.setContentType("application/xml");
    PrintWriter printwriter = response.getWriter();

    printwriter.println("<?xml version=\"1.0\"?>");
    printwriter.println("<document>");

    try {
        Class.forName("com.mysql.jdbc.Driver").newInstance();
        connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/AppDB", "***", "****");

        statement = connection.createStatement();

        String SQL = "SELECT first_name, last_name FROM contacts";
        ResultSet resultset = statement.executeQuery(SQL);

        while(resultset.next()) {
            printwriter.println("<first> " +  resultset.getString("first_name") + " </first>");
            printwriter.println("<last> " +  resultset.getString("last_name") + " </last>");
        }
        resultset.close();
        statement.close();
        connection.close();
    }
    catch (Exception e) {
    }
    printwriter.println("</document>");
    printwriter.close();
}

}

Now, I want to parse it on client-side for displaying data but I don't know how to do that. I have tried to use a JSP file with the SAX parser but when the server give me a error when I put the following Servlet URL:

Document doc = docBuilder.parse("http://localhost:8080/Example/XML");

Could anyone please help me giving me some advice to solve the problem? And could anyone know a possible alternative way to do the same thing? What about JQuery or Ajax? (Even if I have just read a few thing about it, but I don't know them very well)

marks
  • 31
  • 9
  • 2
    Looks like XML is a directory not an .xml file, pass the full path to the xml file you are trying to parse. – SomeDude Jan 12 '16 at 22:50
  • 1
    you have written the xml generation in the doPost() method. If you want to get the xml response just by passing the URL try the implementation in doGet() method – Prabhakaran Jan 12 '16 at 22:59
  • @Prabhakaran Thanks a lot! Perfect solution! – marks Jan 19 '16 at 19:02

1 Answers1

0

Choose between POST or GET

GET => use docBuilder.parse like that, and code doGet in your app.

You can just redirect to doPost

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
doPost (request,response);
}

POST => dont change you servlet, but change your client.

1. docBuilder with URL works also like that, but with GET by default:

Document doc = docBuilder.parse(new URL("http://localhost:8080/Example/XML").openStream());

see that: How to read XML response from a URL in java?

2. So change to POST

use

URL url = new URL("http://localhost:8080/Example/XML");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("POST");
InputStream is= connection.getInputStream();
Document doc = docBuilder.parse(is);

but beware: if you want to pass datas, you have to put in before:

OutputStream out = connection.getOutputStream();
out.write(parameters and data here);
out.close(); 

see that: How can I send POST data through url.openStream()?

How to read XML response from a URL in java?

Sending HTTP POST Request In Java

Community
  • 1
  • 1