userBean代码:
package com.User.Bean;
import com.db.DataSqlserver.*;
import java.sql.*;
import java.text.*;public class postContent {
private String Name,homepage,email,radio,body;
private java.util.Date date;
private DataSqlserver conn;
//set方法
public void setName(String Name){
this.Name=Name;
}
public void setHomepage(String homepage){
this.homepage=homepage;
}
public void setEmail(String email){
this.email=email;
}
public void setRadio(String radio){
this.radio=radio;
}
public void setBody(String body){
this.body=body;
}
public void setDate(java.util.Date date){
this.date=date;
}
//get方法
public String getName(){
return this.Name;
}
public String getHomepage(){
return this.homepage;
}
public String getEmail(){
return this.email;
}
public String getRadio(){
return this.body;
}
public java.util.Date getDate(){
return this.date;
}
public String getDateString(){
SimpleDateFormat f=new SimpleDateFormat("yyyy-MM-dd");
return f.format(this.getDate()).toString();
}
public postContent(){}
public boolean addMessage() throws Exception
{
String name=this.Name;
String homepage=this.homepage;
String email=this.email;
String radio=this.radio;
String pusdata="1999-01-01";
String body=this.body;
String sql="insert into content(name,homepage,email,radio,pusdata,body) values('"+name+"','"+ homepage +"','"+ email +"','"+ radio +"','"+ pusdata +"','"+ body +"')";
try
{
ResultSet rs=conn.executeQuery1(sql);
rs.close();
conn.close();
}
catch(Exception e)
{
e.printStackTrace();
throw e;
}
return true;
}}
jsp页面
<%@ page contentType="text/html;charset=gb2312" language="java" %>
<html>
<head>
<meta http-equiv="Content-Language" content="zh-cn">
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<meta name="GENERATOR" content="Microsoft FrontPage 4.0">
<meta name="ProgId" content="FrontPage.Editor.Document">
<title>迷你留言板</title><meta http-equiv=refresh content=3;url=index.jsp>
<STYLE type=text/css>TD {
FONT-SIZE: 9pt; LINE-HEIGHT: 12pt
}
A:link {
COLOR: #0080ff; TEXT-DECORATION: none
}
A:visited {
COLOR: #0080ff; TEXT-DECORATION: none
}
A:hover {
COLOR: #ee9c00; TEXT-DECORATION: underline
}
</STYLE></head>
<body bgcolor="#FFFFFF" background="images/bg.jpg">
<p> </p>
<p> </p>
<p> </p>
<jsp:useBean id="postCont" class="com.User.Bean.postContent" scope="request" />
<jsp:setProperty name="postCont" property="*" /><%
boolean flag=postCont.addMessage();
if(flag==true)
{
%>
<table cellpadding="0" cellspacing="1" width="52%" align="center" height="145" style="border: 1pt dotted #666633" bordercolor="#999966">
<tr>
<td width="100%" bgcolor="#666633" height="123">
<div align="center"><font color="#FF0000"><font size="2" color="#CCCCCC">您的留言已经提交成功!谢谢您的支持请稍后…………</font></font><font size="2" color="#CCCCCC">…<b>…</b></font></div>
</td>
</tr>
</table>
<%
}
else{
%>
<table cellpadding="0" cellspacing="1" width="52%" align="center" height="145" style="border: 1pt dotted #666633" bordercolor="#999966">
<tr>
<td width="100%" bgcolor="#666633" height="123">
<div align="center"><font color="#FF0000"><font size="2" color="#CCCCCC">留言未成功…………</font></font><font size="2" color="#CCCCCC">…<b>…</b></font></div>
</td>
</tr>
</table>
<%
}
%></body>
</html>
报错:2008-7-18 23:24:14 org.apache.catalina.core.StandardWrapperValve invoke
严重: Servlet.service() for servlet jsp threw exception
java.lang.NullPointerException
at com.User.Bean.postContent.addMessage(postContent.java:67)
at org.apache.jsp.save_jsp._jspService(save_jsp.java:92)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:374)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:337)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:266)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:286)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:844)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
at java.lang.Thread.run(Thread.java:595)
package com.User.Bean;
import com.db.DataSqlserver.*;
import java.sql.*;
import java.text.*;public class postContent {
private String Name,homepage,email,radio,body;
private java.util.Date date;
private DataSqlserver conn;
//set方法
public void setName(String Name){
this.Name=Name;
}
public void setHomepage(String homepage){
this.homepage=homepage;
}
public void setEmail(String email){
this.email=email;
}
public void setRadio(String radio){
this.radio=radio;
}
public void setBody(String body){
this.body=body;
}
public void setDate(java.util.Date date){
this.date=date;
}
//get方法
public String getName(){
return this.Name;
}
public String getHomepage(){
return this.homepage;
}
public String getEmail(){
return this.email;
}
public String getRadio(){
return this.body;
}
public java.util.Date getDate(){
return this.date;
}
public String getDateString(){
SimpleDateFormat f=new SimpleDateFormat("yyyy-MM-dd");
return f.format(this.getDate()).toString();
}
public postContent(){}
public boolean addMessage() throws Exception
{
String name=this.Name;
String homepage=this.homepage;
String email=this.email;
String radio=this.radio;
String pusdata="1999-01-01";
String body=this.body;
String sql="insert into content(name,homepage,email,radio,pusdata,body) values('"+name+"','"+ homepage +"','"+ email +"','"+ radio +"','"+ pusdata +"','"+ body +"')";
try
{
ResultSet rs=conn.executeQuery1(sql);
rs.close();
conn.close();
}
catch(Exception e)
{
e.printStackTrace();
throw e;
}
return true;
}}
jsp页面
<%@ page contentType="text/html;charset=gb2312" language="java" %>
<html>
<head>
<meta http-equiv="Content-Language" content="zh-cn">
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<meta name="GENERATOR" content="Microsoft FrontPage 4.0">
<meta name="ProgId" content="FrontPage.Editor.Document">
<title>迷你留言板</title><meta http-equiv=refresh content=3;url=index.jsp>
<STYLE type=text/css>TD {
FONT-SIZE: 9pt; LINE-HEIGHT: 12pt
}
A:link {
COLOR: #0080ff; TEXT-DECORATION: none
}
A:visited {
COLOR: #0080ff; TEXT-DECORATION: none
}
A:hover {
COLOR: #ee9c00; TEXT-DECORATION: underline
}
</STYLE></head>
<body bgcolor="#FFFFFF" background="images/bg.jpg">
<p> </p>
<p> </p>
<p> </p>
<jsp:useBean id="postCont" class="com.User.Bean.postContent" scope="request" />
<jsp:setProperty name="postCont" property="*" /><%
boolean flag=postCont.addMessage();
if(flag==true)
{
%>
<table cellpadding="0" cellspacing="1" width="52%" align="center" height="145" style="border: 1pt dotted #666633" bordercolor="#999966">
<tr>
<td width="100%" bgcolor="#666633" height="123">
<div align="center"><font color="#FF0000"><font size="2" color="#CCCCCC">您的留言已经提交成功!谢谢您的支持请稍后…………</font></font><font size="2" color="#CCCCCC">…<b>…</b></font></div>
</td>
</tr>
</table>
<%
}
else{
%>
<table cellpadding="0" cellspacing="1" width="52%" align="center" height="145" style="border: 1pt dotted #666633" bordercolor="#999966">
<tr>
<td width="100%" bgcolor="#666633" height="123">
<div align="center"><font color="#FF0000"><font size="2" color="#CCCCCC">留言未成功…………</font></font><font size="2" color="#CCCCCC">…<b>…</b></font></div>
</td>
</tr>
</table>
<%
}
%></body>
</html>
报错:2008-7-18 23:24:14 org.apache.catalina.core.StandardWrapperValve invoke
严重: Servlet.service() for servlet jsp threw exception
java.lang.NullPointerException
at com.User.Bean.postContent.addMessage(postContent.java:67)
at org.apache.jsp.save_jsp._jspService(save_jsp.java:92)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:374)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:337)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:266)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:286)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:844)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
at java.lang.Thread.run(Thread.java:595)
楼主【IPvECMS】截止到2008-07-18 23:31:54的历史汇总数据(不包括此帖):
发帖的总数量:5 发帖的总分数:160 每贴平均分数:32
回帖的总数量:3 得分贴总数量:0 回帖的得分率:0%
结贴的总数量:3 结贴的总分数:120
无满意结贴数:1 无满意结贴分:20
未结的帖子数:2 未结的总分数:40
结贴的百分比:60.00 % 结分的百分比:75.00 %
无满意结贴率:33.33 % 无满意结分率:16.67 %
楼主加油
没给连接引用指明对象是那个。
at com.User.Bean.postContent.addMessage(postContent.java:67)
可以看出是postContent.java的67行报的空指针。
很容易就容易看出:conn为Null。
以为conn为Null,所以后面的JSP也就报错了呀!!!
不要忘记给分我哦!!!
package com.db.DataSqlserver;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.sql.SQLException;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.sql.DataSource; public class DataSqlserver {
private Connection m_conn=null;
private Statement m_stmt=null;
private boolean isAutoCommit;
public DataSqlserver() throws Exception
{
try
{
Context initCtx=new InitialContext();
Context envCtx=(Context)initCtx.lookup("java:comp/env");
DataSource ds=(DataSource)envCtx.lookup("jdbc/SqlServer");
m_conn=ds.getConnection();
initCtx.close();
envCtx.close();
}
catch(Exception ex)
{
ex.printStackTrace();
System.out.println("数据库连接错误!");
throw ex;
}
}
public void close() throws SQLException
{
try
{
if(m_stmt !=null) m_stmt.close();
if(m_conn !=null) m_conn.close();
}
catch(SQLException ex)
{
ex.printStackTrace();
System.out.println("数据库关闭错误!");
throw ex;
}
}
public ResultSet executeQuery1(String sql) throws SQLException
{
try
{
m_stmt=m_conn.createStatement();
return m_stmt.executeQuery(sql);
}
catch(SQLException ex)
{
ex.printStackTrace();
System.out.println("执行SQL:'"+sql+"' 错误! '");
System.out.println(ex.toString());
System.out.println(ex.getMessage());
throw ex;
}
}
public ResultSet executeQuery2(String sql) throws SQLException
{
try
{
m_stmt=m_conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
return m_stmt.executeQuery(sql);
}
catch(SQLException ex)
{
ex.printStackTrace();
System.out.println("执行SQL:'"+sql+"' 错误!'");
System.out.println(ex.toString());
System.out.println(ex.getMessage());
throw ex;
}
}
public int executeUpdate(String sql) throws SQLException
{
try
{
Statement stmt=m_conn.createStatement();
int rt=stmt.executeUpdate(sql);
stmt.close();
return rt;
}
catch(SQLException ex)
{
ex.printStackTrace();
System.out.println("执行SQL:'"+sql+"' 错误'");
System.out.println(ex.toString());
System.out.println(ex.getMessage());
throw ex;
}
}
// 开始新事务
public void beginTrans() throws SQLException
{
try
{
isAutoCommit=m_conn.getAutoCommit(); // 获得当前自动提交状态
m_conn.setAutoCommit(false);// 禁止自动提交
}
catch(SQLException ex)
{
ex.printStackTrace();
System.out.println("开始新事务错误!");
throw ex;
}
} // 保存任何更改并结束当前事务。它也可能启动新事务
public void commitTrans() throws SQLException
{
try
{
m_conn.commit();
m_conn.setAutoCommit(isAutoCommit);
}
catch(SQLException ex)
{
ex.printStackTrace();
System.out.println("更改当前事务错误!");
throw ex;
}
}
// 取消当前事务中所作的任何更改并结束事务。它也可能启动新事务。
public void rollbackTrans() throws SQLException
{
try
{
m_conn.rollback();
m_conn.setAutoCommit(isAutoCommit);
}
catch(SQLException ex)
{
ex.printStackTrace();
System.out.println("取消当前事务错误!");
throw ex;
}
}
}
<resource-ref>
<description>SQL Server Datasource</description>
<res-ref-name>jdbc/SqlServer</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>context.xml<?xml version="1.0" encoding="UTF-8"?>
<Context docBase="javabook" path="/javabook" reloadable="true">
<WatchedResource>WEB-INF/web.xml</WatchedResource>
<Resource name="jdbc/SqlServer" auth="Container" type="javax.sql.DataSource"
maxActive="100" maxIdle="30" maxWait="10000"
username="sa" password="sa" driverClassName="com.microsoft.jdbc.sqlserver.SQLServerDriver"
url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=javabook" />
</Context>
之后再看看还有什么问题
package com.User.Bean;
import com.db.DataSqlserver.*;
import java.sql.*;
import java.text.*;public class postContent {
private String Name,homepage,email,radio,body;
private java.util.Date date;
private DataSqlserver conn;
//set方法
public void setName(String Name){
this.Name=Name;
}
public void setHomepage(String homepage){
this.homepage=homepage;
}
public void setEmail(String email){
this.email=email;
}
public void setRadio(String radio){
this.radio=radio;
}
public void setBody(String body){
this.body=body;
}
public void setDate(java.util.Date date){
this.date=date;
}
//get方法
public String getName(){
return this.Name;
}
public String getHomepage(){
return this.homepage;
}
public String getEmail(){
return this.email;
}
public String getRadio(){
return this.body;
}
public java.util.Date getDate(){
return this.date;
}
public String getDateString(){
SimpleDateFormat f=new SimpleDateFormat("yyyy-MM-dd");
return f.format(this.getDate()).toString();
}
public postContent(){}
public boolean addMessage() throws Exception
{
String name=this.Name;
String homepage=this.homepage;
String email=this.email;
String radio=this.radio;
String pusdata="1999-01-01";
String body=this.body;
String sql="insert into content(name,homepage,email,radio,pusdata,body) values('"+name+"','"+ homepage +"','"+ email +"','"+ radio +"','"+ pusdata +"','"+ body +"')";
try
{
ResultSet rs=conn.executeQuery1(sql);
rs.close();
conn.close();
}
catch(Exception e)
{
e.printStackTrace();
throw e;
}
return true;
}}从上面这个javabean中没有看到private DataSqlserver conn;变量的实例化过程,结合你的数据连接类使用的是JNDI,连接对象m_conn的初始化过程在类DataSqlserver 的构造函数中所以你只需在 public boolean addMessage() throws Exception方法内加上conn = new DataSqlserver()就可以了,你试试。