一下是书上的代码:<%@ 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 2421: </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)首先,谢谢所有的回答者!
<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 2421: </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)首先,谢谢所有的回答者!
解决方案 »
- 关于UTF-8下的CSV文件被EXCEL打开时乱码的问题
- 谁使用过spring 的roo做过开发的 分享分享下经验
- 怎么解决日语乱码的问题 急用解决
- sns算法
- 跪求jsp在线文本编辑器
- 怎样从jsp页面取得java写的函数里面的数据
- 使用MSXML2.DOMDocument装载由Servlet生成的XML字符串时遇到不可解释的问题?????
- 从数据库中读取视频后,如何在JSP页面上放映
- 配Tomcat集群时如何同步session ? 成功配置过Apache2 + Tomcat 5请进!
- 帮助我系统的致命点:我开发了一套“文件管理系统”,(jsp语言,resin服务器)但
- 请问如何用java代码实现 ,重启tomcat6啊
- 求助:登陆跳转问题
String docType = request.getParameter("format");
这行代码没有获取到预期的值,所以docType的值为空,所以在下面调用docType.equals("text")方法时报了空指针的异常。楼主可以考虑换个方法获取format的值。
<option value="text" >文本文件</option>
<option value="word">word文件</option>
<option value="excel">Excel文件</option>
</select>String docType = request.getParameter("format");
你确定这样能取到值?告诉你是取不到的。
因为第一次进来docType为空 你又没做相应的判断 所以会抛异常的
java代码最好写上一句response.setCharacterEncoding("gbk");
不然会乱码的
)即空指针异常。理论上讲,你的这段代码: <%
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);
%>应该是在SetContentType.jsp页面中的。你在代码恰加一句空指针的判断即可。
即
if(docType!=null)
{
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);}
如:
if("a".equals(type)){
//....
}