0
I have a JSP page to login to my system, but when trying to make a request via ajax to authenticate me, the form does not seem to be being sent correctly, as the redirection is done to the login page itself.
Ajax code:
$( document ).ready( function()
{
$( '#submitLogin' ).click( function( e )
{
e.preventDefault();
$.ajax
({
type: 'POST',
url: '/system/users?parameter=login',
data: $( '#loginForm' ).serialize(),
success: function( msg )
{
if ( $.trim( msg ) === "ERROR" )
{
loginErrorAlert();
$( '#userLogin' ).val( "" );
$( '#userPassword' ).val( "" );
$( '#userLogin' ).focus();
}
}
});
});
});
Post method:
protected void doPost( HttpServletRequest request, HttpServletResponse response ) throws ServletException, IOException
{
String parameter = request.getParameter( "parameter" );
if ( parameter.equals( "login" ) )
{
try
{
if ( LoginController.getInstance().authenticateUser( request, response ) )
{
response.sendRedirect( "home.jsp" );
}
else
{
response.setContentType( "text/plain" );
response.setCharacterEncoding( "UTF-8" );
response.getWriter().println( "ERROR" );
}
}
catch ( Exception e )
{
System.out.println( e.toString() );
}
}
}
web xml.:
<servlet>
<servlet-name>Users</servlet-name>
<servlet-class>servlet.Users</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Users</servlet-name>
<url-pattern>/users</url-pattern>
</servlet-mapping>
Thank you, David! It worked exactly as I needed it.
– Luís Felipe Dal Molin