Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • sendRedirect error 404

    • 0
    • 0
    • 0
    • 2
    • 0
    • 1
    • 0
    • 1.64k
    Answer it

    Hello, I'm trying to do a redirect of a login to another page but I receiving error 404. Can someone explain me how the sendRedirect work please? I post too a screen shot of the structure of my files inside the project. Thank you a lot.

    /*
     * To change this license header, choose License Headers in Project Properties.
     * To change this template file, choose Tools | Templates
     * and open the template in the editor.
     */
    package Controller.Servlet;

    import Model.Service.LoginService; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse;

    /** * * @author Jakub Lemiszewski */ public class LogInServlet extends HttpServlet {

    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
     * methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        try (PrintWriter out = response.getWriter()) {
            /* TODO output your page here. You may use following sample code. */
    
          String userId, password;
    
          userId   = request.getParameter("userId");
          password = request.getParameter("password");
    
            LoginService loginservice = new LoginService();
            boolean result = loginservice.authenticate(userId, password);
    
            if(result)
            {
                response.sendRedirect("Jsp/success.jsp");
                return;
            }
            else
            {
                response.sendRedirect("Jsp/login.jsp");
                return;  
            }
    
        }
    }
    
    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }
    
    /**
     * Handles the HTTP <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }
    
    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
    

    }

    sendRedirect error 404

 2 Answer(s)

  • I Understand your problem you cannot put the proper action value in login form that is

    /SimpleLogInMVC/login

    I have corrected your code please see the below code Enjoy :)

    login.jsp

    <%-- 
        Document   : login
        Created on : Sep 24, 2015, 7:17:22 PM
        Author     : Jakub Lemiszewski
    --%>
    
    <%@page contentType="text/html" pageEncoding="UTF-8"%>
    <!DOCTYPE html>
    <html>
        <head>
            <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
            <title>LogIn</title>
        </head>
        <body>
            <h1>Log In Here</h1>
            <form action="/SimpleLogInMVC/login" method="post">
                <label>User Id</label>
                <input type="text" name="userId /">
                <label>Password</label>
                <input type="password" name="password" />
                <input type="submit" value="log in" />
            </form>
        </body>
    </html>
    

    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&#95;3_1.xsd">
        <servlet>
            <servlet-name>LogInServlet</servlet-name>
            <servlet-class>Controller.Servlet.LogInServlet</servlet-class>
        </servlet>
        <servlet-mapping>
            <servlet-name>LogInServlet</servlet-name>
            <url-pattern>/login</url-pattern>
        </servlet-mapping>
        <session-config>
            <session-timeout>
                30
            </session-timeout>
        </session-config>
        <welcome-file-list>
            <welcome-file>index.html</welcome-file>
        </welcome-file-list>
    </web-app>
    
Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: