<%@ page contentType="text/html;charset=gb2312"%>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=gb2312">
<title>动态响应</title></head>
<body>
<center>
<p>动态响应contentType属性的案例</p>
<hr><br>
请选择你的保存的格式:
<form action="test28.jsp" method="post" name="form3">
<select name="format" id="format">
<option value="text">文本文件</option>
<option value="word">Word文件</option>
<option value="excel">Excel文件</option>
</select>
<br><br>
<input type="submit" value="提交" name="sub">
<input type="reset" value="刷新" name="res">
</form>
</center>
<%
  
  String docType=request.getParameter("format");
  if(docType!=null){
  
  
  if(docType.equals("text")){
  docType="text/html";
  }
   else if("docType".equals("word")){
    docType="application/msword";
}
 else if("docType".equals("excel")){
  docType="application/x-msexcel";
  }
     response.setContentType(docType);
 }
 %>
 </body>
 </html>
我编译后,无论选择哪种保存格式,弹出的都是Dreamweaver的格式,选择"text“根本就没有任何弹出!大家帮我一下,是不是哪里写错了,还是我电脑外部哪个环境设置错了。谢谢了!

解决方案 »

  1.   

    response.setContentType(docType); 
    前面不能有任何输出,所以你的这个应该在一个很干净的文件里使用,或者放在文件的最前面。比如
    <%@ page contentType="text/html;charset=gb2312"%> 
    <% 
      out.clearBuffer(); // 加上这个清除以前的输出
      String docType=request.getParameter("format"); 
      if(docType!=null){ 
      
      
      if(docType.equals("text")){ 
      docType="text/html"; 
      } 
      else if("docType".equals("word")){ 
        docType="application/msword"; 

    else if("docType".equals("excel")){ 
      docType="application/x-msexcel"; 
      } 
        response.setContentType(docType); 

    %>其他的内容
      

  2.   

    else if("docType".equals("word")你这个地方的docType为什么要用引号括起来?
      

  3.   

    else if("docType".equals("word")你这个地方的docType为什么要用引号括起来?
    这样不就是这个if永远进不去了吗?