我想從一個onClick事件在JavaScript中發送的servletRequest一個「不可用請求的資源」 .. 這是代碼:發送從JavaScript到Java Servlet請求給
function switchLanguage(languageRequest) {
var xmlhttp;
if (window.XMLHttpRequest) {
xmlhttp = new XMLHttpRequest();
} else {
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.open("GET", "changeLanguage", true);
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4) {
alert("Zumindest das klappt");
alert(xmlhttp.responseText);
}
};
xmlhttp.send(null);
}
的JavaCode樣子這個:
package com.bmw.webevn2.www;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class ServletController extends HttpServlet {
private static final long serialVersionUID = 1L;
@Override
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
System.out.println("Es kommt zumindes an!");
response.setContentType("text/html");
// response.setCharacterEncoding("UTF-8");
String message = "Das schicken klappt";
response.getWriter().write(message);
}
public void doPost(HttpServletRequest req, HttpServletResponse res) throws IOException {
doPost(req, res);
}
}
當我嘗試發送它時,它給了我404「請求的資源()不可用。」 我錯過了什麼嗎?
我的web.xml看起來像這樣(的一部分):
<servlet>
<servlet-name>LanguageServlet</servlet-name>
<servlet-class>com.bmw.webevn2.www.ServletController</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>LanguageServlet</servlet-name>
<url-pattern>/changeLanguage</url-pattern>
</servlet-mapping>
編輯 從那裏發送看起來像這樣的鏈接:
<a href="#" onclick="switchLanguage('language')" target="_self">English</a>
當我嘗試訪問它通過http://localhost:10020/webevn/app/changeLanguage
它現在的作品,但我想用一個鏈接...
LanguageServlet的servlet聲明在哪裏?您將顯示ServletController的代碼,LoginServlet的聲明和LanguageServlet的映射。 LanguageServlet在哪裏? – Brandon