views:

1291

answers:

2

I am trying to complete the tutorial at http://www.ajaxprojects.com/ajax/tutorialdetails.php?itemid=438

It seems that the servlet is trying to POST the data to http://localhost:8080/WeatherServlet/WeatherServlet ... (WeatherServlet is the name of the servlet - duh).

I have the following index.jsp page (which displays fine)

<%@ 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"&gt;
<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>


The following WeatherReport.java page

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;
}

}


The following web.xml page

<?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>


I am creating a WAR archive and deploying using Eclipse's built in tools (not ant - doubt this matters anyway).

+2  A: 

i think the problem is that you are accessing the page at

http://localhost:8080/WeatherServelet/

and then you do a post to: $.post("WeatherServlet", ...

which is a relative path, so you end up posting to

http://localhost:8080/WeatherServelet/WeatherServelet

you should try to post to an absolute path:

$.post("/WeatherServlet", ...
mkoryak
+2  A: 

You're posting to "WeatherServlet" within the jQuery, which is a relative URL - and the original URL is http://server.com/WeatherServlet/index.jsp, so it's no wonder that the URL it builds is http://server.com/WeatherServlet/WeatherServlet

Change

$.post("WeatherServlet", {cityName:$cityName}, function(xml) {

to

$.post("index.jsp", {cityName:$cityName}, function(xml) {

or whatever jsp you want it to post to.

Jon Skeet