JASS implementation in JSF + Tomcat

Asked

Viewed 97 times

0

I want to create an authentication for login in my project, but when I try to log in, the error java.lang.Nullpointerexception appears

Error log

javax.faces.el.EvaluationException: java.lang.NullPointerException
at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:101)
at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:102)
at javax.faces.component.UICommand.broadcast(UICommand.java:315)
at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:790)
at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1282)
at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:81)
at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:198)
at javax.faces.webapp.FacesServlet.service(FacesServlet.java:646)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:505)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:170)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:956)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:423)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1079)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:625)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:318)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.NullPointerException
at br.com.fastchoice.util.FacesUtil.getMensagemI18n(FacesUtil.java:13)
at br.com.fastchoice.bean.SegurancaBean.logar(SegurancaBean.java:23)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at org.apache.el.parser.AstValue.invoke(AstValue.java:278)
at org.apache.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:273)
at com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:105)
at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:87)
... 28 more

Class Facesutil

public class FacesUtil {

public static String getMensagemI18n(String chave) {
    FacesContext context = FacesContext.getCurrentInstance();
    String msg = context.getApplication().getResourceBundle(context, "msg").getString(chave);
    return msg;
}

public static void adicionarMensagem(Severity tipo, String msg) {
    FacesContext.getCurrentInstance().addMessage(null, 
            new FacesMessage(tipo, msg, msg));
}

public static Object getRequestAttribute(String name) {
    FacesContext facesContext = FacesContext.getCurrentInstance();
    ExternalContext externalContext = facesContext.getExternalContext();
    HttpServletRequest request = (HttpServletRequest) externalContext.getRequest();
    return request.getAttribute(name);
}

}

Class Segurncabean

public class SegurancaBean {

private String usuario;
private String senha;

public String logar() {
    try {
        this.getRequest().login(this.usuario, this.senha);
        return "Home?faces-redirect=true";
    } catch (ServletException e) {
        FacesUtil.adicionarMensagem(FacesMessage.SEVERITY_ERROR, 
                FacesUtil.getMensagemI18n("username_password_does_not_match"));
        return null;
    }
}

public String sair() throws ServletException {
    this.getRequest().logout();
    return "Login?faces-redirect=true";
}

private HttpServletRequest getRequest() {
    FacesContext context = FacesContext.getCurrentInstance();
    return (HttpServletRequest) context.getExternalContext().getRequest();
}

public String getUsuario() {
    return usuario;
}
public void setUsuario(String usuario) {
    this.usuario = usuario;
}
public String getSenha() {
    return senha;
}
public void setSenha(String senha) {
    this.senha = senha;
}

}

  • This line: String msg = context.getApplication(). getResourceBundle(context, "msg"). getString(key); it is very likely that JSF is unable to find the file containing the messages

  • Solved, thank you.

  • Put the answer and mark as solved so you can help other people who may have the same problem as yours.

  • David Filipe, I believe the answer: here may help in understanding your question.

1 answer

0


In his method getMensagemI18n you do not check the integrity of the parameter String chave.

Any of the instructions below can return null in any segment.

FacesContext context = FacesContext.getCurrentInstance();
String msg = context.getApplication().getResourceBundle(context, "msg").getString(chave);

It is necessary to protect the code by checking each method execution result with a test against the null value and doing the corresponding error handling correctly.

  • Hello John, good afternoon, this problem has been solved, but another one has appeared, if I can help I am very grateful. http://answall.com/questions/96743/login-com-jaas-tomcat

  • About this problem you would be better to make a simple Servlet first and test the JASS and after it works correctly pass to the problem of using JASS with Faces

  • But I’m using JSF, isn’t Servlet just for JSP? Sorry, I’m layman on the subject.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.