Java with jQuery

WeatherServlet.java

 

Package nareshit.jquery.servelet;

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;

import net. sf.json.JSONObject;

public class WeatherServlet extends HttpServlet{

protected void doPost{HttpServletRequest request,HttpServletResponse

response}throws ServletException,IOException{

String city=request.getParameter{“cityname”};

String report=getWeather{city};

setContentType{‘application/json”};

PrintWriter out=response.getWriter{};

 

JSONObject json=new JSCNObject{};

//adding properties to json object

put{“city”,city};

put{‘report”,report};

println{json};

flush{};

close{};

}

private String getWeather{String city}{

String report;”

if {city.toLowerCase().equals{“Hyderabad”}}

report= Currently it is not raining in hyderabad.Average

temperature is 20”;

else if{city.toLowerCase9).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 Banglore.Good weather for a cricket

”;

else

report=”The City you have entered is not present in our sytem.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 id=WebApp_ID”version=”2.4”

xmlns=http://java.sun.com/xml/ns/j2ee

xmlns:xsi=http://www.w3schools.com/xml/schema_schema.asp

xsi:schemaLoaction=”http://java.sun.com/xml/ns/j2ee

http://java.sin.com/xml/ns/j2ee/web-app_2_4.xsd”>

<display-name>test</dispalu-name>

<servlet>

<servlet-name>WeatherServlet</servet-name>

<servlet-class>com.nareshit.jquery.servlet.WeatherServlet</servlet-class>

</servlet>

<servlet-mapping>

<servlet-name>WeatherServlet</servet-name>

<url-pattern>/WeatherServlet</url-pattern>

</servlet-mapping>

<welcome-file-list>

<welcome-file>index,html</welcome-file>

</welcome-file-list>

</web-app>

 

index.html

<!DOCTYPE html PUBLIC “~//w3c//DTD HTML 4.01 Transitional //EN ”  “http://www.w3.org/TR/html14/loose.dtd”>

<html>

<head>

<script type=”text/javascript” src=”script/jquery.js”></script>

<script type=”text/javascript”>

$(document).ready(function() {

$(“#getWeatherReport”).click(function() {

var cityName=$(“#cityName”).val();

$.ajax(

{

url:” . /WeatherServlet ”,

data:{“cityName”: cityName},

dataType:’json’,

success:function(data,status,xhr){

alert(data.report);

},

error: function (xhr,status,error) {

alert(error);

},

type:’POST’

}};

 

}};

}};

</script>

</head>

<body bgcolor=”yellow”>

<center>

<h1> Ajax with web application </h1>

<hr/><hr/>

<form name=”form1” method=”post”>

Enter Ciyt : <input type=”text” name=”cityName” id=”cityName” />

<input type=”button” name=”getWeatherReport” id=”getWeatherReport” value=”Get Weather” />

</form>

<br/>

<div id=”weatherReport” class=”outputTextArea” style=”width:400px;height:100px;boarder:2px solid green”>

</div>

</center>

</body>

</html>

 

Reference Websites 

jQuery.com

Jqueryui.com

http://codylindley.com/jqueryselectors/

http://appendto.com/community/jquery-vsdoc

http://james.padolsey.com/jquery/#v=1.6.2&fn=jquery.fn.hide