There are many ways to add weather display to the page. This article describes how to use js for easy implementation. The specific code is as follows. Do not miss out on interested users.
The Code is as follows:
<% @ Page language = "java" pageEncoding = "UTF-8" %>
Script
Function load (cid)
{
Var xmlhttp;
If (window. XMLHttpRequest)
{// Code for IE7 +, Firefox, Chrome, Opera, Safari
Xmlhttp = new XMLHttpRequest ();
}
Else
{// Code for IE6, IE5
Xmlhttp = new ActiveXObject ("Microsoft. XMLHTTP ");
}
Xmlhttp. open ("GET", "date. jsp? Cid = "+ cid, false );
Xmlhttp. send ();
Var obj = eval ("(" + xmlhttp. responseText + ")");
// Var obj = JSON. parse (xmlhttp. responseText); // IE8 or above
Document. getElementById ("test"). innerHTML = obj. weatherinfo. city + ":" + obj. weatherinfo. weather1 + "" + obj. weatherinfo. temp1;
}
Script
Weather conditions
Shenzhen weather
Chenzhou weather
Date. jsp
The Code is as follows:
<% @ Page language = "java" import = "java.net. *, java. io. *" pageEncoding = "UTF-8" %>
<%
String cid = request. getParameter ("cid ");
URL url = new URL ("http://m.weather.com.cn/data/" + cid + ". html ");
HttpURLConnection httpConn = (HttpURLConnection) url. openConnection ();
HttpConn. connect ();
InputStream cin = httpConn. getInputStream ();
BufferedReader reader = new BufferedReader (new InputStreamReader (cin, "UTF-8 "));
StringBuffer sb = new StringBuffer ();
String rl = null;
While (rl = reader. readLine ())! = Null)
Sb. append (rl );
Out. println (sb );
%>