[英]AJAX call is not reaching Servlet
我正在嘗試制作一個簡單的文本框,該文本框根據用戶輸入的字符來啟動提示性反饋。 我正在嘗試從Servlet提取JSON對象,但是我的AJAX調用卻無法到達Servlet。 使用this.status檢查AJAX請求的狀態時,我收到錯誤代碼404。有人可以建議我一種解決方案:
[![在此處輸入圖片描述] [1]] [1]
這是我的servlet:FetchServ.java
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
Connection con;
ResultSet rs;
java.sql.PreparedStatement pst;
String ch = request.getParameter("q");
String data;
ArrayList<String> list = new ArrayList();
response.setContentType("application/JSON");
PrintWriter out = response.getWriter();
try {
Class.forName("com.mysql.jdbc.Driver");
System.out.println("drivers registered");
con = DriverManager.getConnection(con_url, userID, password);
System.out.println("connection created");
pst = con.prepareStatement("SELECT * FROM candidates where FirstName LIKE ?");
pst.setString(1, ch + "%");
rs = pst.executeQuery();
System.out.println("query executed");
while(rs.next())
{
data = rs.getString("FirstName");
list.add(data);
}
String json = new Gson().toJson(list);
response.setCharacterEncoding("UTF-8");
response.getWriter().write(json);
response.getWriter().append("Served at: ").append(request.getContextPath());
} catch (SQLException | ClassNotFoundException e) {
System.err.println(e.getMessage());
}
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
jsp代碼:
<script>
function suggest(str){
if(str.length == 0){
return;
}else{
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function(){
document.getElementById("sugg").innerHTML = this.status;
if(this.readyState == 4 && this.status == 200){
//var res = JSON.parse(this.responseText);
document.getElementById("sugg").innerHTML = this.responseText;
}
};
try{
xmlhttp.open("GET", "com.test.java/FetchServ", true);
xmlhttp.send();
}catch(e)
{
alert("unable to connect ");
}
}
}
</script>
</head>
<body>
<form>
Search:<input type="text" onkeyup = "suggest(this.value)">
</form>
<p id = "sugg"></p>
</body>
</html>
這就是我得到的結果:[![在此處輸入圖片描述] [2]] [2]
<display-name>ACtxtbox</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>FetchServ</servlet-name>
<servlet-class>com.test.java/FetchServ</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>FetchServ</servlet-name>
<url-pattern>/FetchServ</url-pattern>
</servlet-mapping>
</web-app>
您的Web.xml條目不正確,請檢查您在urlbar中使用的url和使用xmlhttp.open("GET", "fetch", true);
提交的表單xmlhttp.open("GET", "fetch", true);
您可以通過打開developer tools
並導航至“ network tab
來調試作為請求傳遞給服務器的實際網址,如下所示。
您的<servlet-class>ACtxtbox/FetchServ</servlet-class>
也不正確,它應該與<servlet-class>sompackagepath.FetchServ</servlet-class>
包路徑一起使用
編輯
根據您的圖像,我可以看到您聲明了index.jsp
,可以通過提供URL來訪問它: http://localhost:<port_number>/ACtxtbox/index.jsp
。
現在,您應該注意以下幾點:
xmlhttp.open("GET", "fetch", true);
中的此行代碼中將參數指定為fetch
xmlhttp.open("GET", "fetch", true);
因此它現在將您的URL更改為http://localhost:<port_number>/ACtxtbox/fetch
<servlet> <servlet-name>FetchServ</servlet-name> <servlet-class>com.rishal.test.FetchServ</servlet-class> </servlet> <servlet-mapping> <servlet-name>FetchServ</servlet-name> <url-pattern>/fetch</url-pattern> </servlet-mapping>
http://localhost:<port_number>/ACtxtbox/com.test.java/fetch
像http://localhost:<port_number>/ACtxtbox/com.test.java/fetch
? 是否已修改web.xml條目。 請閱讀有關Web應用程序,jsp,servlet和ajax調用的教程和Google。 您還應該注意,必須像在web.xml條目com.rishal.test
給出的那樣在某個package
下創建servlet
類,它的程序包路徑和類位於該程序包路徑內。 package com.rishal.test; public class FetchServ extends HttpServlet { ---------------------- --------------------------- }
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.