Why I can't redirect to jsp in servlet with JS function

48
June 04, 2021, at 12:20 PM

I have an application which validate login in server, if it unique - redirect to user page if login already exists

javascript function, which print error: "username already exists"    
        $(document).ready(function () {          
       #$('#submit').click(function () {          
          let username = $('#username').val();         
           $.ajax({                 
       type: 'POST',              
          data: {username: username},           
             url: '/validation',             
           success: function (response) {          
                  $('#result1').html(response.responseText);           
             },           
             error: function () {               
             $('#result1').html("username is already exist");                 
       }            
});     
  });    
});

servlet, which validate login

@WebServlet("/validation")
public class ValidationServlet extends HttpServlet
{
    private UserDAO userDAO = new UserDAOImpl();
    private UserService userService = new UserServiceImpl(userDAO);
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
    {
    }
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
    {
        String username = req.getParameter("username");
        if (username.equals(userService.getUserByUsername(username).getUsername()))
        {
            resp.setStatus(400);
        }
        else
        {
            ServletContext servletContext = getServletContext();
            RequestDispatcher requestDispatcher = servletContext.getRequestDispatcher("/views/main.jsp");
            requestDispatcher.forward(req, resp);
        }
    }
}

button "submit" in register.jsp:

<button class="w-100 btn btn-outline-secondary" type="button" id="submit">submit</button>

in common, if type of button "button" - user can see exception, but can't see result of successful - page, that were redirected to. If type of button - "submit", success forwarding, but we can see 404 page, not only printed line about existed login.

How to fix it? How to work with AJAX Sending a redirect from servlet? Currently work - validation login in server, if login uniq -> redirect to another jsp(client page, for example), otherwise - "username already exists".

READ ALSO
WooCommerce Variation Gallery on Product Page

WooCommerce Variation Gallery on Product Page

I am looking to use a products variation images in a gallery on the product page instead of a dropdown and in place of the regular product gallery, and when the image is clicked the variation data is loaded (preferably without reloading the page)

52
How to remove unwanted packages from node_modules? npm rm doesn&#39;t work

How to remove unwanted packages from node_modules? npm rm doesn't work

I can't for the life of me figure out an easy way to remove packages from node_modules

67
How to view logs of custom process event listeners in jBPM

How to view logs of custom process event listeners in jBPM

I have created a custom process listener, deployed to a project and ran the project

69
MySQL database-dump import

MySQL database-dump import

I need some details on importing and exporting a MySQL 8 database

90