I am trying to build a web service that responds to a browser. Do I have to return the html
code as a hardcoded string
or is there a better way? It's a simple authentication service that needs to direct to the main page of an exam. I am really confused with all the tutorials and answers around the net, everyone has a different way for implementing a web service. What am I missing here? I use RestEasy on Wildfly 13.
Html call:
<!DOCTYPE html>
<html>
<head>
<title>Login</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<h1>Hello</h1>
<form method="POST" action="resources/login/userlogin" >
Username: <input type="text" name="Username">
<br>
Password: <input type="password" name="Password">
<br>
<input type="submit" value="Submit">
</form>
</body>
</html>
Service:
@POST
@Path("userlogin")
@Produces(MediaType.TEXT_HTML)
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
public String Authenticate(@FormParam("Username") String Username, @FormParam("Password") String Password) {
LoginToken token = LoginTokenSingleton.instance.getToken(Username, Password);
if (token == null) {
throw new RuntimeException("POST: User not found");
}
//return the html code for a succesfull login?
return null;
}
You are invoking a POST request
when you submit the form having the username
and password
.
Once the user is authenticated in the backend you can return a Success HTTP status code - 200
and on the basis of that you can redirect the user to the main page. And if the user is not authenticated you can send a Not Authenticated Status Code 401
and redirect the user to some other page.