<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <servlet> <servlet-name>OfflineTest</servlet-name> <servlet-class>com.offline.OfflineTest</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <!--如果页面不请求, 可以注释这段 <servlet-mapping> <servlet-name>OfflineTest</servlet-name> <url-pattern>/OfflineTest</url-pattern> </servlet-mapping> --> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>
package com.offline;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
/**
* 判断是否连接网络
* @author Coco
*
*/
public class OfflineTest extends HttpServlet {
private static final long serialVersionUID = 1194662230797153372L;
public void init() throws ServletException {
boolean bool = pingLink();
// 取得Application对象
ServletContext application = this.getServletContext();
// 设置Application属性
application.setAttribute("bool", bool);
}
public boolean pingLink(){
boolean bool = true;
Runtime runtime = Runtime.getRuntime(); // 获取当前程序的运行进对象
Process process = null; // 声明处理类对象
String line = null; // 返回行信息
InputStream is = null; // 输入流
InputStreamReader isr = null; // 字节流
BufferedReader br = null;
String ip = "www.baidu.com";
boolean res = false;// 结果
try {
process = runtime.exec("ping " + ip); // PING
is = process.getInputStream(); // 实例化输入流
isr = new InputStreamReader(is);// 把输入流转换成字节流
br = new BufferedReader(isr);// 从字节中读取文本
while ((line = br.readLine()) != null) {
if (line.contains("TTL")) {
res = true;
break;
}
}
is.close();
isr.close();
br.close();
if (res) {
// Log.print("ping www.baidu.com通...已经连接外网");
} else {
bool = false;
// Log.print("ping www.baidu.com不通...无法连接外网");
}
} catch (IOException e) {
// Log.print(e.getMessage());
}
return bool;
}
}
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>" rel="external nofollow" >
<title>判断是否连接网络</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
</head>
<body>
<%=application.getAttribute("bool") %><br/>
或者<%=getServletContext().getAttribute("bool") %><br/>
实际应用中:比如有网络就加载一个页面 ,引用jquery
<div id="share_plus"></div>
<script type="text/javascript">
var network = '<%=application.getAttribute("bool") %>';
if(network == true){
$.get("/common/common_share.html",function(data){
$("#share_plus").html(data);
});
}
</script>
</body>
</html>
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有