Java with jQuery

20 September, 2018


Related Blogs   Package nareshit.jquery.servelet; import; import; 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= xmlns:xsi= xsi:schemaLoaction=””> <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 ”  “”> <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(; }, 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

About Author


Author Bio

TekSlate is the best online training provider in delivering world-class IT skills to individuals and corporates from all parts of the globe. We are proven experts in accumulating every need of an IT skills upgrade aspirant and have delivered excellent services. We aim to bring you all the essentials to learn and master new technologies in the market with our articles, blogs, and videos. Build your career success with us, enhancing most in-demand skills .