java - jQuery 将 servlet 名称添加到 URL 中两次

标签 java jquery ajax servlets

我正在尝试完成 http://www.ajaxprojects.com/ajax/tutorialdetails.php?itemid=438 上的教程

servlet 似乎正在尝试将数据 POST 到 http://localhost:8080/WeatherServlet/WeatherServlet ...(WeatherServlet 是 servlet 的名称 - 呃)。

我有以下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 的内置工具进行部署(不是 ant - 无论如何我怀疑这很重要)。

最佳答案

您正在 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/

相关文章:

java - 我不断从方法中得到零

javascript - 编写自己的 Javascript 插件来创建音频播放器

javascript - 当我使用公共(public) URL 时,请求的资源上不存在 'Access-Control-Allow-Origin' header

javascript - 在 Release模式下运行 Android Cordova 应用程序时,Ajax 调用失败

java - 使用 Ignite c++ 客户端访问 Ignite java 缓存

java - 为什么说我没弄错?

java - 遍历 ArrayList 并将值放入 HashMap 与仅搜索 ArrayList 的时间复杂度

javascript - 可以使用 jQuery 在 2 个 FontAwesome 5 图标(常规和实心)之间切换

javascript - 如何在搜索栏下添加来自数据库的自动完成建议?

jquery - AJAX 动态调整此 HTML 的大小导致文本不可读。我怎么 "refresh"呢?