JSP with Servlet

Asked

Viewed 294 times

0

I’m trying to get Servlet to work as a controller I was following the booklet from Caelum but I can’t find the error

the jsp inserir a descrição da imagem aqui

to Servlet

@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 initial page.jsp inserir a descrição da imagem aqui

and the mistake it makes

java.lang.Nullpointerexception br.com.Vitor.controlador.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)

This is the line that makes the comparison if it equals "Enter"

1 answer

3

It is probably because there is no input on the page with the name="acao", and that is why the acao variable is null. In the Submit of the page you put acao="Entrar" and value="Login", but what you expect in Servlet is name="acao" and value="Entrar".

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

Browser other questions tagged

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