我的jquery代码找不到servlet

时间:2014-02-26 08:21:32

标签: jquery servlets

我创建了一个包含jquery代码的简单网页。

home.xhtml:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html
    PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:h="http://java.sun.com/jsf/html" xml:lang="en" lang="en">
<head>
<script type="text/javascript" src="/resources/js/jquery.min.js"></script>

<title>my chart</title>

<script type="text/javascript">

    function create() {
        $("#hello").click(function () {
            $.get('${request.contextPath}/Hello/', function (data) {
                $("#a").html(data);
            });
        });
    }
</script>

</head>
<body>

<button id="hello" onclick="create()">click me</button>
<br/>
<label id="a">hi</label>

<form action="Hello" method="POST">
<br/>
Please enter a color <br/>
<input type="text" name="color" size="20px"/>
<input type="submit" value="submit"/>
</form>

</body>
</html>

HelloBean.java:

import org.apache.commons.io.IOUtils;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;

@ManagedBean
@ViewScoped
public class HelloBean extends HttpServlet {

private static void close(Closeable resource) {
    if (resource != null) {
        try {
            resource.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

public static String getDataChart() {
    String dataPath = "C:\\Users\\abasaleh\\Desktop\\bachupChart\\myData.docx";
    String content = "";
    try {
        FileInputStream text = new FileInputStream(new File(dataPath));
        content = IOUtils.toString(text, "UTF-8");
    } catch (IOException e) {
        e.printStackTrace();
    }
    return content;
}

protected static void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    String content = getDataChart();
    response.setContentType("application/json");
    ServletOutputStream out = null;
    try {
        response.reset();
        out = response.getOutputStream();
        if (content != null) {
            out.write(content.getBytes("UTF-8"));
        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        close(out);
    }
}


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

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

}
web.xml中的

<servlet>
    <servlet-name>Hello</servlet-name>
    <servlet-class>HelloBean</servlet-class>
</servlet>

<servlet-mapping>
    <servlet-name>Hello</servlet-name>
    <url-pattern>/Hello</url-pattern>
</servlet-mapping>

当我点击“点击我”按钮时,我收到错误:

HTTP Status 404 - /$%7Brequest.contextPath%7D/Hello/

type: Status report

message: /$%7Brequest.contextPath%7D/Hello/

description: The requested resource (/$%7Brequest.contextPath%7D/Hello/) is not available.
Apache Tomcat/7.0.16

但是当点击“提交”按钮时,它可以找到servlet。

是什么原因?请帮我。谢谢;


我在home.xhtml创建函数:

function create() {
            $.get('${request.contextPath}/Hello/', function (data) {
                $("#a").html(data);
            });
}

我在firefox浏览器中查看firebug。我收到这个错误:

GET http://localhost:9090/$%7Brequest.contextPath%7D/Hello/  404 Not Found

1 个答案:

答案 0 :(得分:0)

您将${request.contextPath}作为字符串文字(而不是变量)传递,因此无效。

至于你的表单,提交按钮会很高兴地发布它,但是代码不会按照你的期望进行 - 你提到的Javascript函数绑定到页面中的不同按钮,而不是提交的按钮。 / p>