说明:setContentType(String type)方法,指的是响应设置内容类型头。参数type可取text/html,text/plain,application/x-msexcel,application/msword等值。
示例目的:动态设置contentType属性,以不同方式打开文件list.txt。
choice.jsp
<html>
<head>
<title>以提供的不同方式打开文件示例</title>
</head>
<body>
<font size=4>
<p>请选择打开文本文件list.txt的方式!
<p><jsp:include page="list.txt"/> //使用动态标签<jsp:include>添加静态文件。
<form action="contentType.jsp" method="post" name=form>
<input type="submit" value="word" name="word">
<input type="submit" value="excel" name="excel">
</form>
</font>
</body>
</html>
contentType.jsp
"<%@page contentType="text/html;charset=gb2312" %>"
<html>
<head>
<title>以不同方式打开文件示例</title>
</head>
<body>
<%
String str1=request.getParameter("word");
String str2=request.getParameter("excel");
if(str1==null)
{
str1="";
}
if(str2==null)
{
str2="";
}
if(str1.equals("word"))
{//设置Word类型。
response.setContentType("application/msword;charset=gb2312");
}
if(str2.equals("excel"))
{//设置excel类型。
response.setContentType("application/x-msexcel;charset=gb2312");
}
%>
<jsp;include page="list.txt"/>
</body>
</html>