使用JAVABEAN报错,求救
环境JDK+TOMCAT+SQLSERVER 2000
问题:JSP文件调用JAVABEAN错误(JAVABEAN能够正确编译并能够正确生成CLASS文件)
存放位子:文件夹/WEB-INF/CLASSES/下
JAVABEAN文件:
package forjavabean;
import java.sql.Connection;
import java.sql.DriverManager;
public class Javabean {
public static Connection getConnection(){
String url= "jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=myjob ";
String user= "sa ";
String password= " ";
Connection conn=null;
try{
conn= DriverManager.getConnection(url,user,password);
}catch (Exception e) {
}
return conn;
}
public Javabean(){
}
}
JSP文件:
<%@ page contentType= "text/html;charset=GB2312 " %>
<%@ page import= "java.sql.* " %>
<%request.setCharacterEncoding( "GBK ");%>
<jsp:useBean id= "forjavabean " class= "forjavabean.javabean " scope= "page "/>
<HTML>
<BODY>
<p>
<%
response.setCharacterEncoding( "GBK ");
String name=request.getParameter( "name ");
String pass=request.getParameter( "password ");
String verify= "select * from user1 where name= ' "+name+ " 'and pass= ' "+pass+ " ' ";
rs=stmt.executeQuery(verify);
if(rs.next()){
response.sendRedirect( "itlife.jsp ");}
else{
response.sendRedirect( "error.jsp ");}
rs.close();
stmt.close();
conn.close();
%>
</p>
</BODY>
</HTML>
运行JSP文件报错:
org.apache.jasper.JasperException: /11.jsp(4,0) The value for the useBean class attribute forjavabean.javabean is invalid.
org.apache.jasper.compiler.DefaultErrorHandler.jspError(DefaultErrorHandler.java:40)
org.apache.jasper.compiler.ErrorDispatcher.dispatch(ErrorDispatcher.java:407)
org.apache.jasper.compiler.ErrorDispatcher.jspError(ErrorDispatcher.java:148)
org.apache.jasper.compiler.Generator$GenerateVisitor.visit(Generator.java:1200)
org.apache.jasper.compiler.Node$UseBean.accept(Node.java:1155)
org.apache.jasper.compiler.Node$Nodes.visit(Node.java:2336)
org.apache.jasper.compiler.Node$Visitor.visitBody(Node.java:2386)
org.apache.jasper.compiler.Node$Visitor.visit(Node.java:2392)
org.apache.jasper.compiler.Node$Root.accept(Node.java:489)
org.apache.jasper.compiler.Node$Nodes.visit(Node.java:2336)
org.apache.jasper.compiler.Generator.generate(Generator.java:3394)
org.apache.jasper.compiler.Compiler.generateJava(Compiler.java:210)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:306)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:286)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:273)
org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:566)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:308)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:320)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:266)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
另一处错:
org.apache.jasper.JasperException: org.apache.jasper.JasperException: Unable to load class for JSP
org.apache.jasper.servlet.JspServletWrapper.getServlet(JspServletWrapper.java:154)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:320)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:320)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:266)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
急盼高手解决
[解决办法]
javabean
package he;
import java.sql.Connection;
import java.sql.DriverManager;
public class Javabean {
public static Connection getConnection(){
String url= "jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=myjob ";
String user= "sa ";
String password= " ";
Connection conn=null;
try{
Class.forName( "com.microsoft.jdbc.sqlserver.SQLServerDriver ");
conn= DriverManager.getConnection(url,user,password);
}catch (Exception e) {
}
return conn;
}
public Javabean(){
}
}
jsp页面
<%@ page contentType= "text/html; charset=GBK " %>
<%@ page import= "java.sql.* " %>
<%request.setCharacterEncoding( "GBK ");%>
<jsp:useBean id= "forjavabean " class= "he.Javabean " scope= "page "/>
<HTML>
<BODY>
<p>
<%
response.setCharacterEncoding( "GBK ");
Connection conn = forjavabean.getConnection();
Statement stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
String verify= "select * from user1 where name= '1 'and pass= '2 ' ";
ResultSet rs=stmt.executeQuery(verify);
if(rs.next()){
response.sendRedirect( "itlife.jsp ");}
else{
response.sendRedirect( "error.jsp ");}
rs.close();
stmt.close();
conn.close();
%>
出来那个你的Javabean的属性是非法的是因为你的 <jsp:useBean/> 的Class写错了 再稍微改一下其他的就OK了啊 !
[解决办法]
大哥,你的JAVABEAN文件是不是少了点什么?
这个BEAN就是连接数据库嘛。
package forjavabean;
import java.sql.*;
public class Javabean {
private Connection con;
private ResultSet rs;
private PreparedStatement pst;
public Javabean () {
try{
Class.forName( "com.microsoft.jdbc.sqlserver.SQLServerDriver ");
con=DriverManager.getConnection( "jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=news ", "sa ", "sa ");
}catch(Exception e){
e.printStackTrace();
}
}
少了这句吧 Class.forName( "com.microsoft.jdbc.sqlserver.SQLServerDriver ");
另外public class Javabean 你的类名会不会是关键字哦,最好换一个类名吧。