方法一
<%@ 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%>">
<title>table</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">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
<style type="text/css">
body{
margin: 0,0,0,0;
}
td{
border:1px solid;
border-collapse: collapse;
border-color: blue;
}
</style>
<script type="text/javascript">
function printdiv(printpage){
var headstr = "<html><head><title></title></head><body>";
var footstr = "</body>";
var newstr = document.all.item(printpage).innerHTML;
var oldstr = document.body.innerHTML;
document.body.innerHTML = headstr+newstr+footstr;
window.print();
document.body.innerHTML = oldstr;
return false;
}
</script>
</head>
<body>
<br>
<div id="printdiv">
<table width="80%" border="1px solid" bordercolor="blue" id="mytable" style="border-collapse: collapse;">
<caption>雇员明细表</caption>
<tr>
<th>编号</th>
<th>姓名</th>
<th>性别</th>
<th>年龄</th>
</tr>
<tr>
<td>1</td>
<td>张三</td>
<td>男</td>
<td>28</td>
</tr>
<tr>
<td>2</td>
<td>张三</td>
<td>男</td>
<td>28</td>
</tr>
<tr>
<td>3</td>
<td>张三</td>
<td>男</td>
<td>28</td>
</tr>
<tr>
<td>4</td>
<td>张三</td>
<td>男</td>
<td>28</td>
</tr>
<tr>
<td>5</td>
<td>张三</td>
<td>男</td>
<td>28</td>
</tr>
<tr>
<td>6</td>
<td>张三</td>
<td>男</td>
<td>28</td>
</tr>
</table>
<img alt="点我试试看" src="http://www.google.com.hk/intl/zh-CN/images/logo_cn.png">
</div>
<input type="button" value="打印表格" onclick="printdiv('printdiv');">
</body>
</html>
方法二
<%@ 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%>">
<title>table</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">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
<style type="text/css">
body{
margin: 0,0,0,0;
}
td{
border:1px solid;
border-collapse: collapse;
border-color: blue;
}
</style>
<!-- 此样式用来标记那些不需要被打印的内容 -->
<style type="text/css" media=print>
.noprint{display : none }
</style>
</head>
<body>
<br>
<div id="printdiv">
<table width="80%" border="1px solid" bordercolor="blue" id="mytable" style="border-collapse: collapse;">
<caption>雇员明细表</caption>
<tr>
<th>编号</th>
<th>姓名</th>
<th>性别</th>
<th>年龄</th>
</tr>
<tr>
<td>1</td>
<td>张三</td>
<td>男</td>
<td>28</td>
</tr>
<tr>
<td>2</td>
<td>张三</td>
<td>男</td>
<td>28</td>
</tr>
<tr>
<td>3</td>
<td>张三</td>
<td>男</td>
<td>28</td>
</tr>
<tr>
<td>4</td>
<td>张三</td>
<td>男</td>
<td>28</td>
</tr>
<tr>
<td>5</td>
<td>张三</td>
<td>男</td>
<td>28</td>
</tr>
<tr>
<td>6</td>
<td>张三</td>
<td>男</td>
<td>28</td>
</tr>
</table>
<img alt="点我试试看" src="http://www.google.com.hk/intl/zh-CN/images/logo_cn.png" class="noprint">
</div>
<object id="WebBrowser" width="0" height="0" classid="CLSID:8856F961-340A-11D0-A96B-00C04FD705A2">
</object>
<input type="button" name="Button" value="打印设置" onclick="document.all.WebBrowser.ExecWB(8,1)" class="noprint">
<input type="button" name="Button" value="打印" onclick="document.all.WebBrowser.ExecWB(6,1)" class="noprint">
<input type="button" name="Button" value="打印预览" onclick="document.all.WebBrowser.ExecWB(7,1)" class="noprint">
</body>
</html>