<%@ 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="test30.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">
</form>
</center>
<%
  String docType=request.getParameter("format");
  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>
我怎么都编译不出来,希望大家帮帮我,谢谢了!
刚才只听一个同事说什么变量值没有初始化,也没细说。我是按照书本上写的,难道书上错了。

解决方案 »

  1.   

    此回复为自动发出,仅用于显示而已,并无任何其他特殊作用
    楼主截止到2008-06-19 09:00:21的汇总数据:
    注册日期:2008-6-6
    上次登录:2008-6-19
    发帖数:1
    结贴数:0
    结贴率: 0.00%
    如何结贴请参考这里:http://topic.csdn.net/u/20080501/09/ef7ba1b3-6466-49f6-9d92-36fe6d471dd1.html
      

  2.   

    String docType 没初始化
    如果你直接进这个页面request.getParameter("format"); 取不到值
      

  3.   

    String docType=request.getParameter("format"); 
    在这句后面加上判断
    if(docType!=null){
      后面的所有代码
    }
      

  4.   

    <%@ page contentType="text/html;charset=gb2312"%> 
    <%@ 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="test30.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"> 
    </form> 
    </center> 
    <% 
      String docType=request.getParameter("format"); 
      docType = (docType == null ? "" : docType);
      if(docType.equals("text")){ 
        docType="text/html"; 
      } 
      else if(docType.equals("word")){ 
        docType="application/msword"; 
      } 
      else if(docType.equals("excel")){ 
        docType="application/x-msexcel"; 
      }
      if(!docType.equals("")){ 
        response.setContentType(docType); 
      }
    %> 
    </body> 
    </html> 
      

  5.   

    谢谢大家了,看样子我买了一本盗版书。唉!不过有时我见"request.getParameter()“方法获取参数时候并没有初始化也可得到相应的值,我在怎样的情况下要初始化哪?(是不是我需要选择才能获取值的时候)
      

  6.   

    当你第一次加载这个页面的时候,用request.getParameter()来获取该页面的值,都会为null
    这跟<select>,<input>还是其他的form元素都没有关系
      

  7.   

    习惯写成:
    if("text".equals(docType)){ 
        docType="text/html"; 
    }...
    ......