书上的一个JSP的例子,但是,我运行一下却发生了异常,希望高手指教!一下是书上的代码:Java code%@ page con
书上的一个JSP的例子,但是,我运行一下却发生了异常,希望高手指教!
一下是书上的代码:
Java code<%@ 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>
在浏览器中运行时,异常提示如下:
org.apache.jasper.JasperException: An exception occurred processing JSP page /SetContentType.jsp at line 24
21: </center>
22: <%
23: String docType = request.getParameter("format");
24: if(docType.equals("text")) {
25: docType = "text/html";
26: } else if(docType.equals("word")) {
27: docType = "application/msword";
Stacktrace:
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:521)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:430)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
root cause
java.lang.NullPointerException
org.apache.jsp.SetContentType_jsp._jspService(SetContentType_jsp.java:77)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:388)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
首先,谢谢所有的回答者!
[解决办法]java.lang.NullPointerException NullPointerException.
Because docType is null
You can write you code as below at line 24 .
if (null!=docType&&docType.equals("text"))
[解决办法] <select name="format" id="format" >
<option value="text" >文本文件</option>
<option value="word">word文件</option>
<option value="excel">Excel文件</option>
</select>
String docType = request.getParameter("format");
你确定这样能取到值?告诉你是取不到的。
[解决办法]
是这样的,如果你把上面的代码写在一个页面的话,会出现(NullPointerException
)即空指针异常。理论上讲,你的这段代码:
HTML code <% 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