我有这个托管bean:

package com.DX_57.SR_57;
/** include default packages for Beans */
import java.io.Serializable;
import javax.enterprise.context.SessionScoped;
   // or import javax.faces.bean.SessionScoped;
import javax.inject.Named;
/** include package for SHA-256 encryption */
import java.security.NoSuchAlgorithmException;
/** include SQL Packages */
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.sql.DataSource;
import javax.annotation.Resource;
import javax.faces.context.FacesContext;
import javax.inject.Inject;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
   // or import javax.faces.bean.ManagedBean;
import org.DX_57.osgi.CL_27.api.CryptoSHA;
import org.glassfish.osgicdi.OSGiService;
import org.DX_57.osgi.SH_27.api.SessionHandle;


@Named("loginController")

@SessionScoped

public class userCheck implements Serializable {
    private String user = null;
    private String password = null;
    private String error_Message = null;
    private String error_Database = null;

       public userCheck(){
       }

       /** Call the Oracle JDBC Connection driver */
       @Resource(name="jdbc/Oracle")
       private DataSource ds;

       /** Call OSGI Bundle SH_27 SessionHandle Library */
       /** Use transient in order to disable serialization when calling OSGI Bundle */
       @Inject @OSGiService(dynamic=true) transient SessionHandle SH;

       public String OSGISHCheckUserDB(String CheckUser) throws SQLException{
            return SH.CheckUserDB(CheckUser);
       }

       public String OSGISHSessionRegister(String User) throws SQLException{
            return SH.SessionRegister(User);
       }

       /** Call OSGI Bundle CL_27 Crypto Library */
       /** Use transient in order to disable serialization when calling OSGI Bundle */
       @Inject @OSGiService(dynamic=true) transient CryptoSHA CL;

       public String OSGICLShaEncryptHash(String CString) throws NoSuchAlgorithmException{
            return CL.ShaEncryptHash(CString);
       }

       /** get the content of the variables from the JSF Login page */
       public void setUser(String newValue) {
           user = newValue;
       }

       public String getUser(){
           return user;
       }

       public void setPassword(String newValue) {
           password = newValue;
       }

       public String getPassword(){
           return password;
       }

       public String geterror_Database(){
           return error_Database;
       }

       public String geterror_Message(){
           return error_Message;
       }


       /** get user session id */
       FacesContext fCtx = FacesContext.getCurrentInstance();
       HttpSession session = (HttpSession) fCtx.getExternalContext().getSession(false);
       String sessionId = session.getId();

       /** get user IP address */
       FacesContext context = FacesContext.getCurrentInstance();
       HttpServletRequest request = (HttpServletRequest)context.getExternalContext().getRequest();
       String remoteHost = request.getRemoteAddr();



       /** compare the user and the password */
       public String userCompare() throws NoSuchAlgorithmException, SQLException {
            String hash_passwd = null;
            String passwdQuery = null;

            /** check the password into Oracle using the username */
            passwdQuery = OSGISHCheckUserDB(user);

            /** convert the plain password in SHA-256 hash */
//          hash_passwd = string_hash(password);

            /** compare the encrypted passwords */
            if (password.equals(passwdQuery)){
                /** insert into users session table the time when the user login */
//                SessionRegister(user);
                /* success */
                return "0";
            } else {
                /* failer */
                return "1";
            }
       }

}


我可以使用Netbeans成功编译它。当我在Glassfish上部署它并打开它时,出现以下错误堆栈:

[#|2012-02-04T18:29:36.554+0200|INFO|glassfish3.1.1|org.glassfish.osgijavaeebase|_ThreadID=64;_ThreadName=Thread-2;|deployed bundle com.DX_57.SR_57 [296] at file:/tmp/osgiapp4499752333201324876/|#]

[#|2012-02-04T18:29:48.047+0200|SEVERE|glassfish3.1.1|javax.enterprise.resource.webcontainer.jsf.application|_ThreadID=143;_ThreadName=Thread-2;|Error Rendering View[/home.xhtml]
com.sun.faces.mgbean.ManagedBeanCreationException: Cant instantiate class: com.DX_57.SR_57.userCheck.
    at com.sun.faces.mgbean.BeanBuilder.newBeanInstance(BeanBuilder.java:193)
    at com.sun.faces.mgbean.BeanBuilder.build(BeanBuilder.java:102)
    at com.sun.faces.mgbean.BeanManager.createAndPush(BeanManager.java:409)
    at com.sun.faces.mgbean.BeanManager.create(BeanManager.java:269)
    at com.sun.faces.el.ManagedBeanELResolver.resolveBean(ManagedBeanELResolver.java:244)
    at com.sun.faces.el.ManagedBeanELResolver.getValue(ManagedBeanELResolver.java:116)
    at com.sun.faces.el.DemuxCompositeELResolver._getValue(DemuxCompositeELResolver.java:176)
    at com.sun.faces.el.DemuxCompositeELResolver.getValue(DemuxCompositeELResolver.java:203)
    at com.sun.el.parser.AstIdentifier.getValue(AstIdentifier.java:99)
    at com.sun.el.parser.AstValue.getValue(AstValue.java:158)
    at com.sun.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:219)
    at com.sun.faces.facelets.el.TagValueExpression.getValue(TagValueExpression.java:109)
    at javax.faces.component.ComponentStateHelper.eval(ComponentStateHelper.java:194)
    at javax.faces.component.ComponentStateHelper.eval(ComponentStateHelper.java:182)
    at javax.faces.component.UIOutput.getValue(UIOutput.java:169)
    at com.sun.faces.renderkit.html_basic.HtmlBasicInputRenderer.getValue(HtmlBasicInputRenderer.java:205)
    at com.sun.faces.renderkit.html_basic.HtmlBasicRenderer.getCurrentValue(HtmlBasicRenderer.java:355)
    at com.sun.faces.renderkit.html_basic.HtmlBasicRenderer.encodeEnd(HtmlBasicRenderer.java:164)
    at javax.faces.component.UIComponentBase.encodeEnd(UIComponentBase.java:875)
    at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1763)
    at javax.faces.render.Renderer.encodeChildren(Renderer.java:168)
    at javax.faces.component.UIComponentBase.encodeChildren(UIComponentBase.java:845)
    at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1756)
    at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1759)
    at com.sun.faces.application.view.FaceletViewHandlingStrategy.renderView(FaceletViewHandlingStrategy.java:401)
    at com.sun.faces.application.view.MultiViewHandler.renderView(MultiViewHandler.java:131)
    at com.sun.faces.lifecycle.RenderResponsePhase.execute(RenderResponsePhase.java:121)
    at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
    at com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:139)
    at javax.faces.webapp.FacesServlet.service(FacesServlet.java:594)
    at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1539)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:281)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
    at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:655)
    at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:595)
    at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:98)
    at com.sun.enterprise.web.PESessionLockingStandardPipeline.invoke(PESessionLockingStandardPipeline.java:91)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:162)
    at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:330)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:231)
    at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:174)
    at com.sun.grizzly.http.ProcessorTask.invokeAdapter(ProcessorTask.java:828)
    at com.sun.grizzly.http.ProcessorTask.doProcess(ProcessorTask.java:725)
    at com.sun.grizzly.http.ProcessorTask.process(ProcessorTask.java:1019)
    at com.sun.grizzly.http.DefaultProtocolFilter.execute(DefaultProtocolFilter.java:225)
    at com.sun.grizzly.DefaultProtocolChain.executeProtocolFilter(DefaultProtocolChain.java:137)
    at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:104)
    at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:90)
    at com.sun.grizzly.http.HttpProtocolChain.execute(HttpProtocolChain.java:79)
    at com.sun.grizzly.ProtocolChainContextTask.doCall(ProtocolChainContextTask.java:54)
    at com.sun.grizzly.SelectionKeyContextTask.call(SelectionKeyContextTask.java:59)
    at com.sun.grizzly.ContextTask.run(ContextTask.java:71)
    at com.sun.grizzly.util.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:532)
    at com.sun.grizzly.util.AbstractThreadPool$Worker.run(AbstractThreadPool.java:513)
    at java.lang.Thread.run(Thread.java:722)
Caused by: java.lang.NullPointerException
    at com.DX_57.SR_57.userCheck.<init>(userCheck.java:103)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:525)
    at java.lang.Class.newInstance0(Class.java:372)
    at java.lang.Class.newInstance(Class.java:325)
    at com.sun.faces.mgbean.BeanBuilder.newBeanInstance(BeanBuilder.java:188)
    ... 54 more
|#]


问题出在以下几行:

 /** get user session id */
       FacesContext fCtx = FacesContext.getCurrentInstance();
       HttpSession session = (HttpSession) fCtx.getExternalContext().getSession(false);
       String sessionId = session.getId();


您能给我一些建议如何修复它们吗?

最佳答案

罪魁祸首在这里:

...getExternalContext().getSession(false);


阅读javadoc布尔参数的作用。


  如果create参数是true,请创建(如有必要)并返回与当前请求关联的会话实例。如果create参数为false,则返回与当前请求关联的任何现有会话实例,如果没有此类会话,则返回null

09-12 16:56