JAVA Web开发过程中遇见的各种Exception 总结(待续)

来源:互联网 发布:sql安装包点哪个安装 编辑:程序博客网 时间:2024/06/08 18:57

1org.hibernate.PropertyAccessException: IllegalArgumentException occurred while calling setter of

原因:对应beanset方法数据类型和hibernate配置文件中定义的类型是否一致。

2This is usually caused by using Struts tags without the associated filter.

原因:web.xml里面没有添加以下内容

<filter>

           <filter-name>struts</filter-name>

           <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>

   </filter>

 

   <filter-mapping>

           <filter-name>struts</filter-name>

           <url-pattern>/*</url-pattern>

   </filter-mapping>

 

 

3java.lang.NullPointerException

原因:空指针,所指的对象不存在。用System.out.println()调试,看哪里出现问题。

 

4java.lang.IllegalStateException

原因JSP文件或struts action(servlet应用中没发现此问题)中采用了,如下代码:

 public void print2Screen(HttpServletResponse resp,String encodeString,String[] htmlCommands) throws IOException{
        resp.setCharacterEncoding(encodeString);
        ServletOutputStream httpOutput= resp.getOutputStream()
;
        for(String temp:htmlCommands)
            httpOutput.write(temp.getBytes())
;
    }

·         深层原理:

1.Servlet规范说明,不能既调用 response.getOutputStream(),又调用response.getWriter(),无论先调用哪一个,在调用第二个时候应会抛出 IllegalStateException.

2.servlet代码中有out.write(””),这个和JSP中缺省调用的response.getOutputStream()产生冲突.

因为在jsp中,out变量是通过response.getWriter得到的,在程序中既用了 response.getOutputStream,又用了out变量,故出现以上错误。

解决:

法一:JSP文件中,加入下面两句

<%
out.clear();
out = pageContext.pushBody();
%>

此法的缺陷:
很多开发项目并不是JSP前端,freemarker,velocity等造成问题的"response.getOutputStream()"并未被写在JSP,而是写在servlet/action

法二: action,不要return 回具体的result文件,而是return null

//return SUCCESS;
 return null;


5org.hibernate.exception.SQLGrammarException: Could not execute JDBC batch update

原因:

1、估计是你的列名里面有关键字的原因吧,命名列的时候不要单独使用date,ID...这种关键字

2应该是由于myeclipse自动将数据库的表生成映射文件时多增加了一个属性造成的,  XXX.hbm.xml的映射文件中的class节点,看看是不是有个 catalog 属性,如果有,删除即可。

3、与数据库字段不匹配。


6、org.hibernate.MappingException: entity class not found:

解决:

<hibernate-mapping package="需要包名">

 

7org.hibernate.exception.GenericJDBCException: Could not execute JDBC batch update

原因:数据库里面 某个字段的 varchar(??) 取值太长了~

8、com.mysql.jdbc.exceptions.MySQLNonTransientConnectionException: No operation

原因:Statement被关闭了,或者Connection被关闭了。


9javax.mail.MessagingException: Unknown SMTP host:

原因:邮件服务器不正确,或者说,输入的收件人和发件人的邮箱地址错了。


10java.lang.IllegalStateException: getOutputStream() has already been called f

原因和解决:

具体的原因就是
tomcatjsp编译成servlet之后在函数_jspService(HttpServletRequest request, HttpServletResponse response)的最后
有一段这样的代码
finally {
if (_jspxFactory != null) _jspxFactory.releasePageContext(_jspx_page_context);
}
这里是在释放在jsp中使用的对象,会调用response.getWriter(),因为这个方法是和
response.getOutputStream()
相冲突的!所以会出现以上这个异常。
然后当然是要提出解决的办法:
在使用完输出流以后调用以下两行代码即可:
out.clear();
out = pageContext.pushBody();

文件代码如下:

OutputStream o=response.getOutputStream();
//
输出文件用的字节数组,每次发送500个字节到输出流
byte b[]=new byte[500];
//
下载的文件
File fileLoad=new File("f:/2000","book.zip");
//
客户使用的保存文件的对话框
response.setHeader("Content-disposition","attachment;filename="+"book.zip");
//
通知客户文件的MOME类型
response.setContentType("application/x-tar");
//
通知客户文件的长度
long fileLength=fileLoad.length();

String length=String.valueOf(fileLength);

response.setHeader("Content_Length",length);
out.clear();
out = pageContext.pushBody();


//
读取文件book.zip,并发送给客户下载
FileInputStream in=new FileInputStream(fileLoad);
int n=0;
while((n=in.read(b))!=-1)
{
o.write(b,0,n);
}

out.clear();
out = pageContext.pushBody();

//
输出图象到页面
ImageIO.write(image, "JPEG", response.getOutputStream());

产生这样的异常原因:web容器生成的servlet代码中有out.write(""),这个和JSP中调用的response.getOutputStream()产生冲突.Servlet规范说明,不能既调用response.getOutputStream(),又调用response.getWriter(),无论先调用哪一个,在调用第二个时候应会抛出IllegalStateException,因为在jsp中,out变量实际上是通过response.getWriter得到的,你的程序中既用了response.getOutputStream,又用了out变量,故出现以上错误。

 

 

11org.hibernate.HibernateException: Not able to obtain connection

差不多等于没有连接数据库了。要保证HibernateSession工厂能正常工作。这个一般我们的做法是Session工厂设置成静态的。

然后需要进行操作就打开一个会话。所以在设计的时候注意一下。这个Session工厂对象。最好是放在

static{}块里。然后用个抽象类去实现它,然后其他的类都实现这个抽象类就搞定了!

它出现的原因是,因为Session已经关闭了,而用户还在干某些事,比如query.list()等操作。

所以建议还是用一些模版,比如HibernateTemplate,由SPRING提供的。

还有一个造成这个问题的就是HIBERNATE版本太低,建议使用3.0以上的.


12org.hibernate.PropertyAccessException: exception setting property value with CGLIB (set hibernate.cglib.use_reflection_optimizer=false for more info) setter

其实挺好理解的当HIBERNATE获取到空时返回NULL,但是如果你配置的映射文件中type long或者其他基本数据类型时,就会出现错误。解决的方法有2

第一:设定数据库,如果是number类型或者其他映射后是基本数据的类型时,为其设定默认值。


 怎一累字了得呢?...(待续,不断积累)

0 0
原创粉丝点击