How to authenticate and redirect a user to his own page i.e to www.mysite.com/"user's email".
I am using the following algo which is not working...
userDB in User class:
Map<String,String> userdata=new HashMap<String,String>();
First my login process form :
@Path("/login")
@POST
@Produces(MediaType.TEXT_HTML)
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
public void login(
@FormParam("email") String emailc,
@FormParam("password") String pass,
@Context HttpServletResponse servletResponse
) throws IOException,RuntimeException {
User u1=new User();
pass=u1.getPassword();
emailc=u1.getEmailaddrs();
boolean checked=false;
boolean exists;
exists=u1.userdata.containsKey(emailc);
if(exists){
String mypass =u1.userdata.get(emailc);
if(mypass==pass){
checked=true;
}else{
checked=false;
}
}else{
checked=false;
}
if(!checked){
//User Doesn't exists
servletResponse.sendRedirect("http://localhost:8080/MySite/pages/Create_Profile.html");
}else{
servletResponse.sendRedirect("http://localhost:8080/MySite/{email}"); <<<< How to redirect using @FormParam("email")
}
}
createprofile
@POST
@Produces(MediaType.TEXT_HTML)
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
public void newUser(
@FormParam("email") String email,
@FormParam("password") String password,
@Context HttpServletResponse servletResponse
) throws IOException {
User u = new User(email,password);
User.userdata.put(email,password);
}
Your usage of
userdata [Map]
looks wrong to me. Is it a part of user class, is it non static or static ? If it is non static then every time you will donew User()
.. that map will be initialized and it will have no data in it. Henceu1.userdata.containsKey(emailc);
will be always false.If you are using a hashmap as a temporary database for dev purposes then, make it static rather keep it in a different class like UserStore or some DB access layer. Exmaple below:
And use this in your REST layer classes like this
Advantages :
Also regarding forward using email
add the email parameter there in the url only, if thats what you want:
UPDATE :
See the fundamental thing is that you get request parameters
[email , password]
. You check it whether it is present in map or not. Now what you are doing wrong here is you create a new user like thisUser u = new User();
and then get email and password from itemailc = u.getEmail();
. Thisemailc
will always benull
and youruserdata map
will always returnfalse
for that. You have two choices :One good practice to follow while programming is that at all times think of your method parameters as final parameters.
UPDATE 2 :
Change
==
toequals
method.String matching
should be done byequals
orequalsIgnoreCase
method not ==.You always create a new
User
without any parameters:User u1=new User();
. All theseUser
instances will have the same property values and probablyexists
is alwaysfalse
.