求教jsp中javabean的使用

keranbing 2005-05-12 09:54:44
我想做个注册的页面,调用javabean写进数据库。并在成功后显示用户名和密码 但是总是在运行时抱错!都郁闷我两天了 以下是代码 请高手指教
////userbean.java
import java.sql.*;
public class userbean
{
private String username=null;
private String password=null;
public userbean()
{
}
public void setUsername(String username)
{
this.username=username;
}
public void setPassword(String password)
{
this.password=password;
}
public String getUsername()
{
return this.username;
}
public String getPassword()
{
return this.password;
}
public String insert()
{
try
{

Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
Connection dbcon=DriverManager.getConnection("jdbc:microsoft:sqlserver://localhost:1433; DatabaseName=fight;","sa","");
PreparedStatement stat=dbcon.prepareStatement("insert login values(?,?)");
stat.setString(1,username);
stat.setString(2,password);
stat.executeUpdate();
return "success";
}
catch(Exception e)
{
System.out.println(e);
return e.toString();
}
}
}
////login.jsp
<html>
<center>
<form method=post action="test1.jsp">
username<input type=text name=username>
<br><br>
password<input type=password name=password>
<br><br>
<input type=submit value="注册">
</form>
</center>
</html>
//////////////test1.jsp
<%@ page contentType="text/html;charset=GB2312" language="java" %>
<html>
<jsp:useBean id="hello" class="userbean" scope="session" />
<jsp:setProperty name="hello" property="*" />
your username is:<jsp:getProperty name="hello" property="username"/>
<br><br>
your password is:<jsp:getProperty name="hello" property="password"/>
<br><br>
</html>
运行时抱错,说我bean 有问题
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: Unable to compile class for JSP

An error occurred at line: 4 in the jsp file: /test1.jsp
Generated servlet error:
C:\jakarta-tomcat-5.0.27\work\Catalina\localhost\aiai\org\apache\jsp\test1_jsp.java:45: cannot resolve symbol
symbol : class userbean
location: class org.apache.jsp.test1_jsp
userbean hello = null;
^


An error occurred at line: 4 in the jsp file: /test1.jsp
Generated servlet error:
C:\jakarta-tomcat-5.0.27\work\Catalina\localhost\aiai\org\apache\jsp\test1_jsp.java:47: cannot resolve symbol
symbol : class userbean
location: class org.apache.jsp.test1_jsp
hello = (userbean) _jspx_page_context.getAttribute("hello", PageContext.SESSION_SCOPE);
^


An error occurred at line: 4 in the jsp file: /test1.jsp
Generated servlet error:
C:\jakarta-tomcat-5.0.27\work\Catalina\localhost\aiai\org\apache\jsp\test1_jsp.java:49: cannot resolve symbol
symbol : class userbean
location: class org.apache.jsp.test1_jsp
hello = new userbean();
^


An error occurred at line: 8 in the jsp file: /test1.jsp
Generated servlet error:
C:\jakarta-tomcat-5.0.27\work\Catalina\localhost\aiai\org\apache\jsp\test1_jsp.java:59: cannot resolve symbol
symbol : class userbean
location: class org.apache.jsp.test1_jsp
out.write(org.apache.jasper.runtime.JspRuntimeLibrary.toString((((userbean)_jspx_page_context.findAttribute("hello")).getUsername())));
^


An error occurred at line: 12 in the jsp file: /test1.jsp
Generated servlet error:
C:\jakarta-tomcat-5.0.27\work\Catalina\localhost\aiai\org\apache\jsp\test1_jsp.java:65: cannot resolve symbol
symbol : class userbean
location: class org.apache.jsp.test1_jsp
out.write(org.apache.jasper.runtime.JspRuntimeLibrary.toString((((userbean)_jspx_page_context.findAttribute("hello")).getPassword())));
^
5 errors



org.apache.jasper.compiler.DefaultErrorHandler.javacError(DefaultErrorHandler.java:84)
org.apache.jasper.compiler.ErrorDispatcher.javacError(ErrorDispatcher.java:332)
org.apache.jasper.compiler.Compiler.generateClass(Compiler.java:412)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:472)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:451)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:439)
org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:511)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:295)
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.27 logs.


--------------------------------------------------------------------------------

Apache Tomcat/5.0.27
求教大虾指教啊!!!!




...全文
117 6 点赞 打赏 收藏 举报
写回复
6 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
keranbing 2005-05-12
我连接数据库时候,执行了更新语句
PreparedStatement stat=dbcon.prepareStatement("insert login values(?,?)");
stat.setString(1,username);
stat.setString(2,password);
stat.executeUpdate();
return "success";
如果没有state.executeUpdate()这条语句,不会错,但一旦执行了更新语句就会发生异常为:
java.sql.SQLException: [Microsoft][SQLServer 2000 Driver for JDBC][SQLServer]对象名 'sss' 无效。
我数据库里面一张表sss,两个字段(username(varchar) password(varchar));
请大虾门指教下,这是什么错误,怎么改进啊!!帮帮新手啊!!

  • 打赏
  • 举报
回复
tof214 2005-05-12
bean一定要声明包才能用吗》??不一定吧?有明确的规定吗??

  • 打赏
  • 举报
回复
MARS.nEIL 2005-05-12
最好是把bean放进包里..另外注意一下bean文件的位置..
  • 打赏
  • 举报
回复
keranbing 2005-05-12
bean一定要声明包才能用吗》??不一定吧?有明确的规定吗??
  • 打赏
  • 举报
回复
tof214 2005-05-12
你的bean没有声明包
  • 打赏
  • 举报
回复
keranbing 2005-05-12
附加:
在test1中要把获得的值写入数据库〈%out.println(hello.insert()); %〉
  • 打赏
  • 举报
回复
相关推荐
发帖
Web 开发
创建于2007-09-28

8.0w+

社区成员

Java Web 开发
申请成为版主
帖子事件
创建了帖子
2005-05-12 09:54
社区公告
暂无公告