javabean问题
userinfo.java文件
package com.bxgl.zaly;
public class userinfo
{
private String username;
private String password;
public String getUsername()
{
return username;
}
public String getPassword()
{
return password;
}
public void setUsername(String username)
{
this.username=username;
}
public void serPassword(String password)
{
this.password=password;
}
}
userregist.java
package com.bxgl.zaly;
import java.sql.*;
public class userregist
{
Connection conn=null;
private userinfo usin;
public userregist()
{
try
{
Class.forName( "sun.jdbc.odbc.JdbcOdbcDriver ");
String url= "jdbc:odbc:driver={Microsoft Access Driver (*.mdb)};DBQ=data\bxgl.mdb ";
conn=DriverManager.getConnection(url);
}
catch (Exception e)
{
e.printStackTrace();
}
}
public void setUsin(userinfo usin)
{
this.usin=usin;
}
public void regist()throws Exception
{
String sql= "insert into user(username,password) values(?,?) ";
try
{
PreparedStatement pstmt=conn.prepareStatement(sql);
pstmt.setString(1,usin.getUsername());
pstmt.setString(2,usin.getPassword());
pstmt.executeUpdate();
}
catch (Exception e)
{
e.printStackTrace();
throw e;
}
}
}
newuser.jsp文件
<%@ page language= "java " contentType= "text/html;charset=gb2312 " import= "com.bxgl.zaly.* "%>
<jsp:useBean id= "usin " class= "com.bxgl.zaly.userinfo " scope= "page ">
<jsp:setProperty name= "usin " property= "* "/>
</jsp:useBean>
<jsp:useBean id= "regist " class= "com.bxgl.zaly.userregist " scope= "page "/>
<%
regist.setUsin(usin);
regist.regist();
out.println( "注册成功 ");
%>
报错内容
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
com.bxgl.zaly.userregist.regist(userregist.java:34)
org.apache.jsp.newuser_jsp._jspService(newuser_jsp.java:72)
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.30 logs.
[解决办法]
空指针啊,看看哪里没初始化
[解决办法]
控制针,看看你的username和password是否都有值?
[解决办法]
regist bean中的usin为空指针
<jsp:useBean id= "regist " class= "com.bxgl.zaly.userregist " scope= "page "/>
没有设置usin的值,所以为空了
[解决办法]
private userinfo usin;改成 private userinfo usin = new userinfo();试试
顺便说一下类名首字母和后面每个单词的第一个字母最好大写
[解决办法]
两个bean是否都要构造一下
[解决办法]
<jsp:useBean id= "usin " class= "com.bxgl.zaly.userinfo " scope= "page ">
如果有其他页面提交了信息到该页面,并且表单中提交的信息的名字userinfo的属性相同,则不需要构造。但如果其他地方没有提交信息过来,则应当对usin的属性进行 初始化。因为
<jsp:setProperty name= "usin " property= "* "/> 不会对其值进行初始化。
[解决办法]
private userinfo usin;改成 private userinfo usin = new userinfo();试试
顺便说一下类名首字母和后面每个单词的第一个字母最好大写
---------------------------------------------------
同意。。。。。关注。。。。。。。。。。
我的异常网推荐解决方案:The server encountered an internal error () that prevented it from fulfilling this request.,http://www.myexception.cn/java-web/317.html