简体   繁体   中英

Can't Access Java Servlet Although it Works in NetBeans IDE

I'm pulling my hair out and have done everything I can think of although I know the answer will be simple - so this is my first post to Stackoverflow.

I've built a servlet in NetBeans that works fine when run in the NetBeans IDE. An Html page (index.htm) has some Javascript calls - POST and GET (Ajax calls within Javascript - shown below). A JSON object is sent to the Servlet - it contains parameters to access a MySQL database (user name, schema, etc.). The doGet method of the Servlet then accesses the database and returns a JSON string of data when invoked by the "GET" Ajax call - the data comes through just fine. The is dumped to the screen via an "alert" call which I know is not the right way - was just doing it for debugging purposes.

Output after Servlet has been run

Here is my NetBeans setup: NetBeans Setup

So what I want to do is launch another Html page (say from my desktop) with Javascript calls (just like in the IDE) to invoke the Servlet and get the data. But it's not working. Here is the Javascript code (which works when tested inside the IDE):

    function sendAjax() 
{
    // MySQL Data Request
    var jsonObj = 
    {
        "parameters": 
        {
            "requesting": 
            {
                "application": "mysql",
                "properties": 
                {
                    "password": "tridvola",
                    "db_name": "mysql",
                    "tb_name": "flightdata1",
                    "user": "root"
                }
            },
            "destination": "localhost",
            "format": "json"
        }                
    }


    $.ajax(
    {
        url: "jsonservlet",
        type: 'POST',
        dataType: 'json',
        data: JSON.stringify(jsonObj),
        contentType: 'application/json',
        mimeType: 'application/json',

        success: goodData,
        error: error_m
    });
}

function error_m(data,status,er) 
{
//            alert("error: "+data+" status: "+status+" er:"+er);
//            alert("sendAjax error!!");
}

function goodData(data2) 
{
//    alert("inside goodData!!");
//    var jsonServlet = data;
//    alert("after variable declaration");        
//    var content = $(data2).text();
//    alert(content);
}

function getJsonData()
{
    $.ajax(
    {
        type: "GET",
        url: "jsonservlet",
        dataType: 'text',
        success : recData, 
        error: badData
    });
}

function recData(vars)
{
    var info = vars;
    alert(info);
}

function badData()
{
    alert("error from call to receive data from Servlet!!");
}

And here are the web.xml contents:

  <display-name>Java Servlet JSON</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
  </welcome-file-list>

  <servlet>
    <servlet-name>jsonservlet</servlet-name>            
    <servlet-class>JSONServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>jsonservlet</servlet-name>
    <url-pattern>/jsonservlet</url-pattern>
  </servlet-mapping>
</web-app>

Here are the contents of the Html file:

<!DOCTYPE html>
<!--
To change this license header, choose License Headers in Project Properties.
To change this template file, choose Tools | Templates
and open the template in the editor.
-->
<html>
<head>
<meta charset="utf-8">
<title>Servlet MySQL Tester</title>
<script src="jquery-1.12.0.js"></script>


<script src="jsfunctions.js"></script>

</head>

<body>

<script>
    sendAjax();
</script>

    <h1 style="text-align:left">MySQL Retrieval Web Page<br></h1> 

    <h3 style="text-align:left">=> retrieving MySQL data ...<br></h3> 

<script>
    getJsonData();
</script>

</body> 
</html>

Important Note: To launch the servlet from a browser I have to use "BServlet" - the project name. This is strange because it's the name of the project but doesn't match the servlet name in the web.xml file.

Extra information - Tomcat is set as one of the servers in NetBeans. I've also taken the .war file (after doing a "clean and build") and deposited it in the Tomcat webapps folder but it doesn't change anything. Have also looked at the Catalina directory but already had the servlet folder in there. Also added JAVA_HOME environment variable pointing to the JDK.

So again - I just want to run Javascript code from within an Html file (outside the IDE) - and invoke the Servlet (send the JSON data structure) and receive the MySQL data. I've run the same html file on the desktop and it doesn't invoke the servlet - have tried the project name, the servlet name, etc. - nothing works. I know that I'm going to face-palm myself when somebody shows me what I've overlooked. Any help is very much appreciated.

If you are using Chrome and are trying to do AJAX from a local file to somewhere, you are out of luck, see this ticket .

In general, I'd advice against trying to develop AJAX from local files, there are lots of hairy cases. Use a local Tomcat/Jetty/Glassfish.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM