一个怪现象,大家来看看~
在编写关于“Response”的代码时,遇到这样的怪事。
直接通过Tomcat运行范例程序时,报错:
HTTP Status 500 -
--------------------------------------------
type Exception report
message
description The server encountered an internal error () that prevented it from fulfilling this request.
exception
org.apache.jasper.JasperException
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:372)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:292)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:236)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
root cause
java.lang.NullPointerException
java.util.Arrays$ArrayList. <init> (Unknown Source)
java.util.Arrays.asList(Unknown Source)
org.apache.jsp.ch4.resposne_jsp._jspService(resposne_jsp.java:50)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:94)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:324)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:292)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:236)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
note The full stack trace of the root cause is available in the Apache Tomcat/5.0.28 logs.
可是在运行过之前写的一个“Request.jsp”以后再运行就完全没有问题了。
但是在“Firefox”中无论如何运行都报错。
而且在NetBeans5.5中这个jsp根本无法通过编译。可是这是范例程序啊,为什么呢?似乎运行时出的问题和在NetBeans中出的问题原因是不一样的。NetBeans中编译时显示:
init:
deps-module-jar:
deps-ear-jar:
deps-jar:
library-inclusion-in-archive:
library-inclusion-in-manifest:
compile:
compile-jsps:
Compiling 1 source file to C:\Documents and Settings\Java\Response\build\generated\classes
C:\Documents and Settings\Java\Response\build\generated\src\org\apache\jsp\response_jsp.java:65: 警告:编码 GBK 的不可映射字符
out.println( "涓婁竴娆¤闂殑鏃堕棿锛?+cookie_response.getValue());
^
C:\Documents and Settings\Java\Response\build\generated\src\org\apache\jsp\response_jsp.java:65: 未结束的字符串字面值
out.println( "涓婁竴娆¤闂殑鏃堕棿锛?+cookie_response.getValue());
^
C:\Documents and Settings\Java\Response\build\generated\src\org\apache\jsp\response_jsp.java:66: 需要 ') '
cookie_response.setValue(new Date().toString());
^
2 错误
1 警告
C:\Documents and Settings\Java\Response\nbproject\build-impl.xml:364: The following error occurred while executing this line:
C:\Documents and Settings\Java\Response\nbproject\build-impl.xml:149: Compile failed; see the compiler error output for details.
生成失败(总时间:3 秒)
源程序如下:
/**
response.jsp
*/
<%@page contentType= "text/html;charset=gb2312 "%>
<%@ page import= "javax.servlet.http.Cookie,java.util.* "%>
<%
String userName= "hellking ";
Cookie[] cookie=request.getCookies();
Cookie cookie_response=null;
List list=Arrays.asList(cookie);
Iterator it=list.iterator();
while(it.hasNext())
{
Cookie temp=(Cookie)it.next();
if(temp.getName().equals(userName+ "_access_time "))
{
cookie_response=temp;
break;
}
}
out.println( "当前的时间: "+new java.util.Date()+ " <br> ");
if(cookie_response!=null)
{
out.println( "上一次访问的时间: "+cookie_response.getValue());
cookie_response.setValue(new Date().toString());
}
else
{
cookie_response=new Cookie(userName+ "_access_time ",new java.util.Date().toString());
}
response.addCookie(cookie_response);
response.setContentType( "text/html ");
response.flushBuffer();
%>
哪位大侠能给个解释?
[解决办法]
out.println( "当前的时间: "+new java.util.Date()+ " <br> ");
if(cookie_response!=null)
{
out.println( "上一次访问的时间: "+cookie_response.getValue());
你先把这上面的中文去掉试试
[解决办法]
我在看《jsp网络编程技术与实例》时也出现了这种情况,请高手帮忙解决。
[解决办法]
Settings\Java\Response\build\generated\src\org\apache\jsp\response_jsp.java:65: 警告:编码 GBK 的不可映射字符
out.println( "涓婁竴娆¤闂殑鏃堕棿锛?+cookie_response.getValue());
可能乱码问题,
[解决办法]
判断下request.getCookies(); 是否为空
[解决办法]
root cause
java.lang.NullPointerException
一看就是空指针异常了,对null对象进行操作,所以会出这个错.
在取对象的时候,在操作之前必须判断是否为空,不为null的时候再操作就好了.
我的异常网推荐解决方案:The server encountered an internal error () that prevented it from fulfilling this request.,http://www.myexception.cn/java-web/317.html