Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8687067
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T23:01:31+00:00 2026-06-12T23:01:31+00:00

In PHP when a user logs into her account, I do the following in

  • 0

In PHP when a user logs into her account, I do the following in order to remember the user as she navigates through the site:

session_start();
...
$_SESSION['username'] = $username;

On any other page that may require sensitive data, I check that $_SESSION['username'] is valid.

When a use logs out, I do the following

unset($_SESSION['username']
session_destroy();

How do I do the same thing in Java? I have a REST API which uses Jersey and EJB. In case the following is important, I am persisting with JPA, Hibernate, Glassfish, and mysql.

UPDATED FOR VERIFICATON:

Is this correct?

@Path("login")
public class UserLoginResource {

  @EJB
  private LoginDao  loginDao;

  @Context
  HttpServletRequest request;

  @POST
  public Response login(JAXBElement<Login> jaxbLogin){
    Login login = jaxbLogin.getValue();
    loginDao.authenticateUserLogin(login);
    HttpSession session = request.getSession();
    session.setAttribute("username", login.getUsername());
    return Response.ok().build();
  }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T23:01:33+00:00Added an answer on June 12, 2026 at 11:01 pm

    Java is very different from php, so in java You will get session from only HttpRequest ‘s getSession() method, In php it is all time assumed, your code is run by some server(ie apache), In java, you will obtain it from ServletContainer(ie Apache Tomcat).

    You do not have to start session in java unlike php, As long as you are in servlet container and giving request, for this client servlet container is responsible to start if there is not session for it

    So for above actions:

        reqest.getSession().setAttribute("udername","Elbek");
        //later 
        reqest.getSession().removeAttribute("udername");
    
       //destroy it
       reqest.getSession().invalidate();
    

    Here request is object of HttpRequest class

    You may have a look to this HttpSession

    I strongly recommend you to have a look java scopes

    There is not this kind of thing in php, I wish there is, BUT there is NO

    Here is how you get request object into your jersey action(method), ie by injecting @Context HttpServletRequest httpRequest

    EDIT:
    You do not create HttpRequest object by yourself, Instead you will get it from servlet container, Your server creates it from clients request and gives for your.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an order table in my database and once a user logs into
A user with a designated account logs into my page and they receive a
Ok here is my problem: When a user logs into my site I put
This is user.php: include(databse.php);//retrieving successfully first name and lastname from databse file into user.php
I am trying to rewrite some server php code which logs into a website,
I'm trying to insert the current DATETIME into MySQL when a user logs into
With PHP and MySQL I am working on a user login type site. Here
The issue here is that when my user logs into my app, they always
I have created a chat system using php and ajax. When a user logs
Below is my code. For some reason, after the user logs into the little

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.