java - How to check whether a user is logged in or not in Servlets? -


In an Java servlet I want to check the program to see if the user has log in < / Strong> or no .

As told in the second answer only applies when you use Java EE Container underlined managed security.

If you are the owner of the house for your safety, then you need to trust it. It's not very hard, here's an overview that you need to implement at each step:

Get the user from db and login it to session Store DoPost () :

  User user = userDAO.find (username, password); If (user! = Null) {session.setAttribute ("user", user); } Else {// error "Show login failed, unknown user, try again" error }  

At logout, simply invalidate the session in the doPost () of the servlet. This will destroy the session and remove all the features.

  session.invalidate ();  

To check whether the is logged in to the user , create one, with the url-pattern The page is restricted, where the page is restricted, such as / safe / * , / protected / * , etcetera and doFilter () below Apply as:

  if (session.getAttribute ("user") == tap) {response.sendRedirect (request.getContectPath () + "/ login"); // login, redirect to login page } And {chain.doFilter (request, response); // logged in, just continue the series}  

This is basically all.

Also see:


Comments

Popular posts from this blog

asp.net - Javascript/DOM Why is does my form not support submit()? -

sockets - Delphi: TTcpServer, connection reset when reading -

javascript - Classic ASP "ExecuteGlobal" statement acting differently on two servers -