书上的一个JSP的例子,但是,我运行一下却发生了异常,希望高手指教!
一下是书上的代码:
<%@ page contentType="text/html;charset=gbk" language="java" %><html> <head> <meta http-equiv="content-type" content="text/html;charset=gbk" /> <title>动态响应contentType属性</title> </head> <body> <center> <p>动态响应contentType属性的案例</p> <hr><br> 请选择你的保存的格式: <form action="SetContentType.jsp" name="myForm" method="post"> <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" name="save" value="保存"> </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>
</select>
String docType = request.getParameter("format");
你确定这样能取到值?告诉你是取不到的。
[解决办法]
是这样的,如果你把上面的代码写在一个页面的话,会出现(NullPointerException
)即空指针异常。理论上讲,你的这段代码:
<% 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); %>
[解决办法]
楼上已经给出答案了,不过要提醒一句
通常用equals时把常量放在左边
如:
if("a".equals(type)){
//....
}
我的异常网推荐解决方案:An exception occurred processing JSP page,http://www.myexception.cn/j2se/33144.html