JasperTack
JasperTack

Reputation: 4447

use of servlet in javascript

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?

Upvotes: 0

Views: 307

Answers (1)

Quentin
Quentin

Reputation: 943922

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.

Upvotes: 3

Related Questions