error when calling Servlet from index.jsp

0

Good afternoon, I have a problem calling a servlet from my index.jsp, inside the index I have a form

  

index.jsp

<form action="ServletLogin" role="form" method="post" class="form-horizontal">
                                        <div class="form-group">
                                            <label for="email" class="col-sm-2 control-label">
                                                Usuario</label>
                                            <div class="col-sm-10">
                                                <input type="text" class="form-control" name="txtUsuario" id="txtUsuario" placeholder="Usuario" />
                                            </div>
                                        </div>
                                        <div class="form-group">
                                            <label for="exampleInputPassword1" class="col-sm-2 control-label">
                                                Contraseña</label>
                                            <div class="col-sm-10">
                                                <input type="password" class="form-control" name="txtPassword" id="txtPassword" placeholder="Contraseña" />
                                            </div>
                                        </div>
                                        <div class="row">

                                            <div class="col-sm-6 col-md-offset-7">
                                                <a class="btn btn-primary" data-dismiss="modal">Cancelar</a>
                                                <a class="btn btn-dark" type="submit">Entrar</a>
                                            </div>
                                        </div>
                                        </form>

  

ServletLogin.java

protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        try (PrintWriter out = response.getWriter()) {
            Privilegio pv = new Privilegio();
            HttpSession session = request.getSession(true);

            String tipo_usuario;
            int num_empleado = Integer.parseInt(request.getParameter("txtUsuario"));
            String password = request.getParameter("txtxPassword");

            tipo_usuario = pv.autenticar(num_empleado, password);

            session.setAttribute("tipo_usuario", tipo_usuario);

            response.sendRedirect("jefe.jsp");
        }
    }
  

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
    <servlet>
        <servlet-name>ServletLogin</servlet-name>
        <servlet-class>servlets.ServletLogin</servlet-class>
    </servlet>
    <servlet>
        <servlet-name>Login</servlet-name>
        <servlet-class>servlets.Login</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>ServletLogin</servlet-name>
        <url-pattern>/ServletLogin</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
        <servlet-name>Login</servlet-name>
        <url-pattern>/Login</url-pattern>
    </servlet-mapping>
    <session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>
</web-app>
    
asked by Javier fr 29.11.2017 в 23:48
source

4 answers

1

How about, how do you have your "ServletLogin" configured in your project?

This is the way in which I configure a servlet to later invoke it. 1) create the servlet class in this case you created "ServletLogin", in that class it contains the methods doGet (), doPost (), I suppose you are using the doPost () since in your form you declared method="POST" 2) in the web.xml that usually is in WEB-INF / web.xml     you need to add the following configuration of your servlet:

      <servlet> <servlet-name>ServletLogin</servlet-name>  <servlet-class>paquete.ServletLogin</servlet-class></servlet>
  <servlet-mapping>  
        <servlet-name>ServletLogin</servlet-name>
        <url-pattern>/ServletLogin</url-pattern>
  </servlet-mapping> 

3) in the doPost () method:    you get the values String email = request.getParameter ("email");

I do not know if any configuration is missing in your project. Greetings.

    
answered by 30.11.2017 в 00:21
1

Using Ajax (You need jquery):

1) On your submit button: <a onClick="Entrar()" class="btn btn-dark" > Entrar</a> 2) in a .js file you add the function Enter ().

  function Entrar(){
      $.ajax({  
                url:"/ServletLogin", 
                type:'POST',
                data:{txtUsuario:$("#txtUsuario").val(),txtPassword:$("#txtPassword").val()},
                success: function( resp ) {
               //aqui obtienes la respuesta de tu servlet
                },
                error:function(error){
                    console.error(error);
                }
            });

        }
    }
    
answered by 30.11.2017 в 01:42
1

If you are not using any javascript call, and make calls by the form, all you have to do is leave the original two methods doGet () and doPost () , you put processRequest () , change it to doPost () because in your form html you declared it as method="post" , < that you only have to do , if the servlet routes are well declared, you would not have any more problems.

By default from a form html if you do not make any javascript calls, the form only accepts get and post that matches doGet () and doPost () of servlets.

protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        try (PrintWriter out = response.getWriter()) {
            Privilegio pv = new Privilegio();
            HttpSession session = request.getSession(true);

            String tipo_usuario;
            int num_empleado = Integer.parseInt(request.getParameter("txtUsuario"));
            String password = request.getParameter("txtxPassword");

            tipo_usuario = pv.autenticar(num_empleado, password);

            session.setAttribute("tipo_usuario", tipo_usuario);

            response.sendRedirect("jefe.jsp");
        }
    }

In the event that the name change that I mentioned does not work , you can try a minimum change that sure will work . Making a call very very simple by javascript and without any framework.

You only have to add this call somewhere in your html, add the name tag to your form html and finally change the button of the form with the html tag, start tag button value="Send" onclick="ajaxCall ()" , end tag.

I leave you in code.

<!DOCTYPE html>
<html>
<head>

<!-- LLamada ajax javascript puro -->
<script type="text/javascript">
function ajaxCall(){
    var frm = document.frmServletLogin;
    frm.submit();
}
</script>


<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<!-- Acá le agregamos la etiqueta name, para que tome el valor en la llamada js -->
    <form action="ServletLogin" name="frmServletLogin" role="form" method="post"class="form-horizontal">
        <div class="form-group">
            <label for="email" class="col-sm-2 control-label"> Usuario</label>
            <div class="col-sm-10">
                <input type="text" class="form-control" name="txtUsuario"
                    id="txtUsuario" placeholder="Usuario" />
            </div>
        </div>
        <div class="form-group">
            <label for="exampleInputPassword1" class="col-sm-2 control-label">
                Contraseña</label>
            <div class="col-sm-10">
                <input type="password" class="form-control" name="txtPassword"
                    id="txtPassword" placeholder="Contraseña" />
            </div>
        </div>
        <div class="row">

            <div class="col-sm-6 col-md-offset-7">
                <a class="btn btn-primary" data-dismiss="modal">Cancelar</a>
                <!-- Cambiamos el boton anterior por este que referencia a una function javascript por medio del onclick -->
                <button value="Enviar" onclick="ajaxCall()">Entrar</button>
            </div>
        </div>
    </form>
</body>
</html> 

Notice that we gave the form a name, then we made an ajax call, in the same we created a variable with the path document.frmLogin (html where we work followed by the name of the frm), then the submit (), which send all the parameters if they exist;

Do not forget to rename the method that expects the call from the jsp / html , obviously you can have more methods to do many things within the servlet, it is not highly recommended but you can, what by custom are kept in this type of system is that the two methods strong> that exist within a servlet are mainly doGet () and doPost () .

    
answered by 02.12.2017 в 04:27
-1

If you are using Java Server Pages, it is correct to use:

<jsp:include page=" URLdeServlet " />

But I see that you are actually using a html , then in action of form define the url of servlet , like this:

<form action=" URLdeServlet " role="form" method="post" class="form-horizontal">
    
answered by 30.11.2017 в 00:42