我正在尝试通过http://www.ajaxprojects.com/ajax/tutorialdetails.php?itemid=438完成本教程
似乎Servlet试图将数据发布到http://localhost:8080/WeatherServlet/WeatherServlet ...(WeatherServlet是Servlet的名称-duh)。
我有以下index.jsp页(显示正常)
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$("#getWeatherReport").click(function(){
$cityName = document.getElementById("cityName").value;
$.post("WeatherServlet", {cityName:$cityName}, function(xml) {
$("#weatherReport").html(
$("report", xml).text()
);
});
});
});
</script>
</head>
<body>
<form name="form1" type="get" method="post">
Enter City :
<input type="text" name="cityName" id="cityName" size="30" />
<input type="button" name="getWeatherReport" id="getWeatherReport"
value="Get Weather" />
</form>
<div id="weatherReport" class="outputTextArea">
</div>
</body>
</html>
以下WeatherReport.java页面
package org.ajax.tutorial;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class WeatherReport
*/
public class WeatherReport extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public WeatherReport() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
* response)
*/
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
* response)
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String city = request.getParameter("cityName");
String report = getWeather(city);
response.setContentType("text/xml");
PrintWriter out = response.getWriter();
out.println("<weather><report>" + report + "</report></weather>");
out.flush();
out.close();
}
private String getWeather(String city) {
String report;
if (city.toLowerCase().equals("trivandrum"))
report = "Currently it is not raining in Trivandrum. Average temperature is 20";
else if (city.toLowerCase().equals("chennai"))
report = "It’s a rainy season in Chennai now. Better get a umbrella before going out.";
else if (city.toLowerCase().equals("bangalore"))
report = "It’s mostly cloudy in Bangalore. Good weather for a cricket match.";
else
report = "The City you have entered is not present in our system. May be it has been destroyed "
+ "in last World War or not yet built by the mankind";
return report;
}
}
以下web.xml页面
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" >
<servlet>
<description>Weather Data provider</description>
<display-name>Weather Data provider</display-name>
<servlet-name>WeatherServlet</servlet-name>
<servlet-class>ajaxify.WeatherServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>WeatherServlet</servlet-name>
<url-pattern>/WeatherServlet</url-pattern>
</servlet-mapping>
</web-app>
我正在创建一个WAR归档文件,并使用Eclipse的内置工具进行部署(不是蚂蚁-仍然怀疑这很重要)。
最佳答案
您将要发布到jQuery中的“ WeatherServlet”,这是一个相对URL-原始URL是http://server.com/WeatherServlet/index.jsp,因此也难怪它生成的URL是http://server.com/WeatherServlet/WeatherServlet
更改
$.post("WeatherServlet", {cityName:$cityName}, function(xml) {
至
$.post("index.jsp", {cityName:$cityName}, function(xml) {
或任何您想要发布的jsp。
关于java - jQuery将Servlet名称两次添加到URL,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/773736/