程序如下(.jsp):
<%!
public String saveFormat(String docType) throws Exception {
if(docType.contentEquals("text")) {
docType="text/html";
} else if(docType.equals("wors")) {
docType="application/msword";
} else if(docType.equals("excel")) {
docType="application/x-msexcel";
}
return docType;
}
%><%@ page language="java" import="java.util.*" pageEncoding="GB2312" %><html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=gb2312"/>
<title>动态响应contentType属性</title>
</head>
<body>
<center>
<p><br></p><p><br></p><p>动态响应contentType属性的案例</p>
<hr/><br/>
请选择你的保存格式:
<form action="index.jsp" method="post" name="MyForm">
<select name="format" id="format" >
<option value="text" >文本文件</option>
<option value="word" selected>Word文件</option>
<option value="excel">Excel文件</option>
</select>
<br/><br/>
<input type="submit" name="Submit" value="提交"></input>
</form>
</center>
<%
String docType=request.getParameter("format");
docType=saveFormat(docType);
response.setContentType("docType");
%>
</body>
</html>运行时错误显示如下org.apache.jasper.JasperException: An exception occurred processing JSP page /index.jsp at line 31: <%!
2: public String saveFormat(String docType) throws Exception {
3: if(docType.contentEquals("text")) {
4: docType="text/html";
5: } else if(docType.equals("wors")) {
6: 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.index_jsp.saveFormat(index_jsp.java:13)
org.apache.jsp.index_jsp._jspService(index_jsp.java:92)
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)
<%!
public String saveFormat(String docType) throws Exception {
if(docType.contentEquals("text")) {
docType="text/html";
} else if(docType.equals("wors")) {
docType="application/msword";
} else if(docType.equals("excel")) {
docType="application/x-msexcel";
}
return docType;
}
%><%@ page language="java" import="java.util.*" pageEncoding="GB2312" %><html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=gb2312"/>
<title>动态响应contentType属性</title>
</head>
<body>
<center>
<p><br></p><p><br></p><p>动态响应contentType属性的案例</p>
<hr/><br/>
请选择你的保存格式:
<form action="index.jsp" method="post" name="MyForm">
<select name="format" id="format" >
<option value="text" >文本文件</option>
<option value="word" selected>Word文件</option>
<option value="excel">Excel文件</option>
</select>
<br/><br/>
<input type="submit" name="Submit" value="提交"></input>
</form>
</center>
<%
String docType=request.getParameter("format");
docType=saveFormat(docType);
response.setContentType("docType");
%>
</body>
</html>运行时错误显示如下org.apache.jasper.JasperException: An exception occurred processing JSP page /index.jsp at line 31: <%!
2: public String saveFormat(String docType) throws Exception {
3: if(docType.contentEquals("text")) {
4: docType="text/html";
5: } else if(docType.equals("wors")) {
6: 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.index_jsp.saveFormat(index_jsp.java:13)
org.apache.jsp.index_jsp._jspService(index_jsp.java:92)
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)
<form action="index.jsp" method="post" name="MyForm">
<select name="format" id="format" >
<option value="text" >文本文件</option>
<option value="word" selected>Word文件</option> //肯定至少会选中word
<option value="excel">Excel文件</option>
这个是空指针错误,
org.apache.jasper.JasperException: An exception occurred processing JSP page /index.jsp at line 3
确定是第三行错误
if(docType.contentEquals("text")) {
那只能是docType是空了,你可以断点到这里或者在这之前输出docType看看。
最起码你要这么写
应该是你的docType没拿到