I have a JSP login page on my system, but when trying to perform a request via ajax to authenticate me, the form does not seem to be sending correctly, as it is redirected 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();
}
}
});
});
});
DoPost 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>