We help IT Professionals succeed at work.

about javabean?

jbas
jbas asked
on
638 Views
Last Modified: 2011-09-20
hi,experts,can you help me
i hava a jsp page,my ClassPath=.;E:\j2sdk1.4.2_03\lib\dt.jar;E:\j2sdk1.4.2_03\lib\tools.jar;E:\Tomcat5\webapps\jforum\WEB-INF\classes;E:\j2sdk1.4.2_03\lib\tools.jar;E:\Tomcat5\webapps\jforum

i use tomcat 5 and jdk 1.4

checkuser.jsp
///in E:\Tomcat5\webapps\jforum\WebApp\checkuser.jsp
<%@ page contentType="text/html;charset=gb2312" language="java"%>

<jsp:useBean id="sysAction" scope="session" class="com.beltino.jbas.jforum.jmain.TSystemAction">
</jsp:useBean>
<%
          String user=request.getParameter("user");
            String pwd=request.getParameter("password");
          boolean logined=sysAction.CheckUser(user,pwd);
            if (logined==true){
                  session.setAttribute("sessionUserName",user);
                  session.setAttribute("sessionUserPwd",pwd);
                  session.setAttribute("sessionLogined","true");
                  response.sendRedirect("index.jsp?logined=true");
            }
            else{
                  response.sendRedirect("index.jsp?logined=false");
            }
%>
+++++++++++++++++++
DataBaseConnection.java

//// in E:\Tomcat5\webapps\jforum\com\beltino\jbas\jforum\util\DataBaseConnection.java
package com.beltino.jbas.jforum.util;

import java.sql.*;
public class DataBaseConnection
{
      public static Connection getConnection()
      {
            Connection con=null;
        String SqlDriver="com.microsoft.jdbc.sqlserver.SQLServerDriver";
        String SqlJdbc="jdbc.microsoft.sqlserver://localhost:1433;DatabaseName=jforum";
        String User="sa";
        String Pwd="btn";
            try
            {
               Class.forName(SqlDriver);
               con=DriverManager.getConnection(SqlJdbc,User,Pwd);
            }
            catch(Exception e)
            {
               e.printStackTrace();
            }
            return con;
      }
}

+++++++++
TSystemAction.java

///in E:\Tomcat5\webapps\jforum\com\beltino\jbas\jforum\jmain\TSystemAction.java
package com.beltino.jbas.jforum.jmain;

import java.sql.*;
import java.util.*;
import java.io.*;
import com.beltino.jbas.jforum.util.*;
public class TSystemAction{
      private Connection conn;
      
      //&#36830;&#25509;&#25968;&#25454;&#24211;
      public TSystemAction(){
         this.conn=DataBaseConnection.getConnection();
      }
    //&#29992;&#25143;&#39564;&#35777;
      public boolean CheckUser(String User,String Pwd) throws Exception{
         Statement stmt=conn.createStatement();
         String sql;
         sql="select * from TUser";
         sql=sql+" where UserName='"+User+"' and UserPwd='"+Pwd+"'";
         ResultSet rst=stmt.executeQuery(sql);
         if (rst.next())
            return true;
         else
            return false;
      }
}


when i use checkuser.jsp,error:
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
      org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:358)
      org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:301)
      org.apache.jasper.servlet.JspServlet.service(JspServlet.java:248)
      javax.servlet.http.HttpServlet.service(HttpServlet.java:856)


root cause

java.lang.NullPointerException
      com.beltino.jbas.jforum.jmain.TSystemAction.CheckUser(TSystemAction.java:17)
      org.apache.jsp.WebApp.CheckUser_jsp._jspService(CheckUser_jsp.java:58)
      org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:133)
      javax.servlet.http.HttpServlet.service(HttpServlet.java:856)
      org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:311)
      org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:301)
      org.apache.jasper.servlet.JspServlet.service(JspServlet.java:248)
      javax.servlet.http.HttpServlet.service(HttpServlet.java:856)


note The full stack trace of the root cause is available in the Tomcat logs.


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

Apache Tomcat/5.0.16
Comment
Watch Question

Commented:
I would check that the parameters user and password is recieved properly. It seems the conn object is null since the nullpointer appears at line 17 and I guess it is conn.createStatement();

remember to close the ResultSet and Statement too, because otherwise it will cause trouble the next time you make the call.

Commented:
Could you run this and check the log that the parameters are correct?

public boolean CheckUser(String User,String Pwd) throws Exception{

       System.out.println("User: " + User);
       System.out.println("Pwd: " + Pwd);

        Statement stmt=conn.createStatement();
        String sql;
        sql="select * from TUser";
        sql=sql+" where UserName='"+User+"' and UserPwd='"+Pwd+"'";
        ResultSet rst=stmt.executeQuery(sql);
        if (rst.next())
          return true;
        else
          return false;
     }

Author

Commented:
hi,Nick 72
when i change the produce
      public TSystemAction(){
         this.conn=DataBaseConnection.getConnection();
      }
    //&#29992;&#25143;&#39564;&#35777;
      public boolean CheckUser(String User,String Pwd) throws Exception{
       System.out.println("User: " + User);
       System.out.println("Pwd: " + Pwd);
       // Statement stmt=conn.createStatement();
       // String sql;
       // sql="select * from TUser";
       // sql=sql+" where UserName='"+User+"' and UserPwd='"+Pwd+"'";
       // ResultSet rst=stmt.executeQuery(sql);
       // if (rst.next())
          return true;
       // else
       //   return false;
      }
the error is same,how can i do?
i copy all class in E:\Tomcat5\webapps\jforum\WEB-INF\classes\

Commented:
The comments I added prints the parameters in the log, can you check the log and see what is says? It should print out the user and password parameters. If one of them is null you should see it there.

Commented:
>>i copy all class in E:\Tomcat5\webapps\jforum\WEB-INF\classes\

ok, but you have a package in your class named: com.beltino.jbas.jforum.jmain

Your TSystemAction class should be in:

E:\Tomcat5\webapps\jforum\WEB-INF\classes\com\beltino\jbas\jforum\jmain
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Author

Commented:
thanks!but the error is same.
i copy all class in :\Tomcat5\webapps\jforum\WEB-INF\classes\com\beltino\jbas\jforum\jmain
the error log:
2004-02-09 21:08:37 StandardWrapperValve[jsp]: Servlet.service() for servlet jsp threw exception
java.lang.NullPointerException
      at com.beltino.jbas.jforum.jmain.TSystemAction.CheckUser(TSystemAction.java:17)
      at org.apache.jsp.WebApp.CheckUser_jsp._jspService(CheckUser_jsp.java:56)
      at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:133)
      at javax.servlet.http.HttpServlet.service(HttpServlet.java:856)
      at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:311)
      at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:301)
      at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:248)
      at javax.servlet.http.HttpServlet.service(HttpServlet.java:856)
      at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:284)
      at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:204)
      at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:256)
      at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:151)
      at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:564)
      at org.apache.catalina.core.StandardContextValve.invokeInternal(StandardContextValve.java:245)
      at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:199)
      at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:151)
      at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:564)
      at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:195)
      at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:151)
      at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:164)
      at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:149)
      at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:564)
      at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:156)
      at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:151)
      at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:564)
      at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:972)
      at org.apache.coyote.tomcat5.CoyoteAdapter.service(CoyoteAdapter.java:211)
      at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:805)
      at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.processConnection(Http11Protocol.java:696)
      at org.apache.tomcat.util.net.TcpWorkerThread.runIt(PoolTcpEndpoint.java:605)
      at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:677)
      at java.lang.Thread.run(Thread.java:536)

Commented:
Alright, I run the code, and got the same error. When I checked the console on the server, it had thrown an ClassNotFoundException, because I don't have the jdbc driver installed for MS SQL-server.
Have you really installed that driver?

If not, go to this site, download and install it.

http://www.microsoft.com/downloads/details.aspx?FamilyID=86212d54-8488-481d-b46b-af29bb18e1e5&DisplayLang=en

If you don't want to use that driver, you can use the jdbc-odbc bridge (if you run on windows). Like this:

public class DataBaseConnection
{
     public static Connection getConnection()
     {
          Connection con=null;
        String SqlDriver="sun.jdbc.odbc.JdbcOdbcDriver";
        String SqlJdbc="jdbc:odbc:myODBCSource";  //create a odbc source in the control panel on the same machine as Tomcat runs on, of type Microsoft SQL Server,  that points to the database jforum, I named it myODBCSource but it can have any name
        String User="sa";
        String Pwd="btn";
          try
          {
             Class.forName(SqlDriver);
             con=DriverManager.getConnection(SqlJdbc,User,Pwd);
          }
          catch(Exception e)
          {
             e.printStackTrace();
          }
          return con;
     }
}

Author

Commented:
hi,expert,
the error is also,

how can i use javabean?
TSystemAction.java&#25991;&#20214;

package com.beltino.jbas.jforum.jmain;

import java.sql.*;
import java.util.*;
import java.io.*;
import com.beltino.jbas.jforum.util.*;
public class TSystemAction{
      //private Connection conn;
      
      //&#36830;&#25509;&#25968;&#25454;&#24211;
      //public TSystemAction(){
       //  this.conn=DataBaseConnection.getConnection();
      //}
    //&#29992;&#25143;&#39564;&#35777;
      public String checkUser(){
       //System.out.println("User: " + User);
       //System.out.println("Pwd: " + Pwd);
      // Statement stmt=conn.createStatement();
       //String sql;
       // sql="select * from TUser";
       // sql=sql+" where UserName='"+User+"' and UserPwd='"+Pwd+"'";
       // ResultSet rst=stmt.executeQuery(sql);
       // if (rst.next())
          return "true";
       // else
        //  return false;
      }
}

////checkuser.jsp
<%@ page contentType="text/html;charset=gb2312" language="java"%>

<jsp:useBean id="sysaction" scope="page"  class="com.beltino.jbas.jforum.jmain.TSystemAction">
</jsp:useBean>
<%
          String user=request.getParameter("user");
            String pwd=request.getParameter("password");
            out.println(user);
            out.println(pwd);
          String logined=sysaction.checkUser();
         // boolean logined=false;
            if (logined=="true"){
                  //session.setAttribute("sessionUserName",user);
                  //session.setAttribute("sessionUserPwd",pwd);
                  //session.setAttribute("sessionLogined","true");
                  out.println("myok");
                  //response.sendRedirect("index.jsp?logined=true");
            }
            else{
                  out.println("false");
                  //response.sendRedirect("index.jsp?logined=false");
            }
%>

&#25552;&#31034;&#38169;&#35823;&#65306;
HTTP Status 500 -

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

type Exception report

message

description The server encountered an internal error () that prevented it from fulfilling this request.

exception

javax.servlet.ServletException: com.beltino.jbas.jforum.jmain.TSystemAction.checkUser()Ljava/lang/String;
      org.apache.jasper.runtime.PageContextImpl.doHandlePageException(PageContextImpl.java:867)
      org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:800)
      org.apache.jsp.WebApp.CheckUser_jsp._jspService(CheckUser_jsp.java:77)
      org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:133)
      javax.servlet.http.HttpServlet.service(HttpServlet.java:856)
      org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:311)
      org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:301)
      org.apache.jasper.servlet.JspServlet.service(JspServlet.java:248)
      javax.servlet.http.HttpServlet.service(HttpServlet.java:856)


root cause

java.lang.NoSuchMethodError: com.beltino.jbas.jforum.jmain.TSystemAction.checkUser()Ljava/lang/String;
      org.apache.jsp.WebApp.CheckUser_jsp._jspService(CheckUser_jsp.java:58)
      org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:133)
      javax.servlet.http.HttpServlet.service(HttpServlet.java:856)
      org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:311)
      org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:301)
      org.apache.jasper.servlet.JspServlet.service(JspServlet.java:248)
      javax.servlet.http.HttpServlet.service(HttpServlet.java:856)


note The full stack trace of the root cause is available in the Tomcat logs.


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

Apache Tomcat/5.0.16

&#24110;&#24537;&#20102;&#65281;

Author

Commented:
to Nick_72
thanks,i reload the tomcat and change something  it can work

i reload the tomcat.
thanks

Commented:
ok great :)

Author

Commented:
to Nick 72
 sorry,
now i can not work the javabean,can you help me ,help


//TSystemAction.java
package com.beltino.jbas.jforum.jmain;

public class TSystemAction{
      public boolean myLogined(){
           return true;
      }
}

checkuser.jsp
<%@ page contentType="text/html;charset=gb2312" language="java"%>

<jsp:useBean id="sysaction" scope="session"  class="com.beltino.jbas.jforum.jmain.TSystemAction">
</jsp:useBean>
<%
          String user=request.getParameter("user");
            String pwd=request.getParameter("password");
            out.println(user);
            out.println(pwd);
            boolean xx=sysaction.myLogined();
            if(xx==false){
                  out.println("true");
            }
            else{
                  out.println("false");
            }
%>

Commented:
What kind of error do you get? Could  you post it here please?

/Nick

Author

Commented:
hi,Nick 72,thank you for help me!!
the error:
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: 5 in the jsp file: /WebApp/CheckUser.jsp

Generated servlet error:
    [javac] Compiling 1 source file

E:\Tomcat5\work\Catalina\localhost\jforum\org\apache\jsp\WebApp\CheckUser_jsp.java:59: cannot resolve symbol
symbol  : method myLogined ()
location: class com.beltino.jbas.jforum.jmain.TSystemAction
            xx=sysaction.myLogined();
                            ^
1 error


      org.apache.jasper.compiler.DefaultErrorHandler.javacError(DefaultErrorHandler.java:127)
      org.apache.jasper.compiler.ErrorDispatcher.javacError(ErrorDispatcher.java:351)
      org.apache.jasper.compiler.Compiler.generateClass(Compiler.java:415)
      org.apache.jasper.compiler.Compiler.compile(Compiler.java:458)
      org.apache.jasper.compiler.Compiler.compile(Compiler.java:439)
      org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:552)
      org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:291)
      org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:301)
      org.apache.jasper.servlet.JspServlet.service(JspServlet.java:248)
      javax.servlet.http.HttpServlet.service(HttpServlet.java:856)


note The full stack trace of the root cause is available in the Tomcat logs.


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

Apache Tomcat/5.0.16

Author

Commented:
to ,Nick 72

thank you very much,my question is completed.i worked it.

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.