【问题标题】:jQuery is adding servlet name twice to URLjQuery 将两次 servlet 名称添加到 URL
【发布时间】:2009-04-21 17:39:32
【问题描述】:

我正在尝试在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 - 无论如何都怀疑这很重要)。

【问题讨论】:

    标签: java jquery ajax servlets


    【解决方案1】:

    您在 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。

    【讨论】:

      【解决方案2】:

      我认为问题在于您正在访问页面

      http://localhost:8080/WeatherServelet/
      

      然后你发一个帖子到: $.post("WeatherServlet", ...

      这是一个相对路径,所以你最终发布到

      http://localhost:8080/WeatherServelet/WeatherServelet
      

      您应该尝试发布到绝对路径:

      $.post("/WeatherServlet", ...
      

      【讨论】:

        猜你喜欢
        • 2013-09-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-03-03
        • 2016-09-03
        • 2014-01-22
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多