JSP with Servlet

0

I'm trying to make the servlet work as a controller I was following the caelum handout but I can not find the error

o jsp

aservlet

@WebServlet(urlPatterns="/LoginInfo")
public class LoginInfo extends HttpServlet {
    public LoginInfo(){
        super();
    }

    @Override
    protected void doGet(HttpServletRequest request,HttpServletResponse response)
            throws ServletException, IOException{

    }
    protected void doPost(HttpServletRequest request,HttpServletResponse response)
    throws ServletException, IOException{
        String acao = request.getParameter("acao");
        String usuario = request.getParameter("usuario");
        String senha= request.getParameter("senha");
        Login login = new Login();
        login.setUsuario(usuario);
        login.setSenha(senha);
        RequestDispatcher rd = null;
        if(acao.equals("Entrar")){
            rd = request.getRequestDispatcher("/paginaInicial.jsp");
            rd.forward(request, response);
        }

    }

the Startpage.jsp

andtheerrorthatgives

  

java.lang.NullPointerException    br.vitor.controller.LoginInfo.doPost(LoginInfo.java:41)    javax.servlet.http.HttpServlet.service(HttpServlet.java:648)    javax.servlet.http.HttpServlet.service(HttpServlet.java:729)    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)    org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:393)

Thisisalinethatcomparesifitisequalto"Enter"

    
asked by anonymous 21.06.2017 / 01:35

1 answer

2

This is probably because there is no input on the page with name="acao" , so the action variable is null. In the submit of the page you put acao="Entrar" and value="Login" , but what you expect in the servlet is name="acao" and value="Entrar" .

Another tip, to avoid null errors. When testing strings, call the equals of the constant, not the variable. So instead of acao.equals("Entrar") you "Entrar".equals(acao) . So if action is null, equals returns false, but does not pop an exception.

    
21.06.2017 / 03:33