0

I would like to use a java servlet in javascript. For the moment I use this code in javascript:

var req = new XMLHttpRequest();
req.open("GET", "http://localhost:8080/FPvisualizer/test.java" + "?action=test", true);
req.send(null);
req.onreadystatechange = function() {processRequest()}; 

function processRequest() {
    if (req.readyState == 4) {
        if (req.status == 200) {
            document.getElementById("target").innerHTML = req.responseText;
        }
    }
}

which communicates with this java servlet:

import java.io.File;
import java.util.Collections;
import java.util.List;
import java.util.Map;

public class LoadOntology2 extends HttpServlet{
    public void doGet (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    String action = request.getParameter("action");

    response.setContentType("text/xml");
    response.setHeader("Cache-Control", "no-cache");
    response.getWriter().write("<message>valid</message>");
}

}

the req.responseText contains the whole contents of the servlet file (i.e. all the code of that file is displayed on the webpage). Does anybody know what I am doing wrong here?

1 Answer 1

3

You are requesting the Java source file itself. You haven't compiled it and installed it on a server configured to execute it for the URL you are using.

I don't have any experience with setting up Java Servlets, but the tutorial at Oracle looks like a good starting point. In particular, the part where it says you need an application server.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for the advice, I found some good information here too

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.