书上的一个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.handle
JspException(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&&do