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 7684181
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T18:57:59+00:00 2026-05-31T18:57:59+00:00

I am fetching username from database when a user is logging in by his

  • 0

I am fetching username from database when a user is logging in by his userid. so if userid is let’s say mat is logging then I am displaying the name as Mathews in userhome.jsp.

I have 5 jsp pages and in each page instead of writing a sql query (to fetch username from database by their id) I am defining a class Username.java and want to return userName to each jsp page. But this error is coming:

`HttpSession session1 = request.getSession(false);` 

The error tells me to define a request class. How can I solve it?

  public class Username {

  public String getUserName(Long userId) {

      HttpSession session1 = request.getSession(false);// error is coming here for request           
      String userid = (String)session1.getAttribute("userid");
   // I want to fetch user name from database by the userid above 
    String userName = "";
    //all my sql code here
    return userName;
   }
   } 

I am writing the following code in the jsp:

  Username uName = new Username ();
  uName.getUserName (userId);
  • 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-05-31T18:58:00+00:00Added an answer on May 31, 2026 at 6:58 pm

    The implicit object ‘request’ is only available in your JSP page. For the class that you are defining, the object is not present. You will have to define it explicitly.

    One solution would be to get the Session in the JSP page and pass it as an argument (may be to a constructor) to your class.

    For eg, You could define a constructor in the class like this:-

    public class Username {
    
        private HttpSession session;
    
        Username(HttpSession session){
           this.session = session;
        }
    
        public String getUserName(Long userId) {
    
            /* remove the following line */
           //HttpSession session1 = request.getSession(false);// error is coming here for request
    
           String userid = (String)session.getAttribute("userid");
    
           // i want to fetch user name from database by the userid above 
    
           String userName = "";
           //all my sql code here
            return userName;
       }
    
    }
    

    Then modify the code in JSP page like:-

    Username uName = new Username(request.getSession());
    uName.getUserName(userId);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am fetching an array of floats from my database but the array I
I am fetching the datas from the database...And the datas are displayed as dropdownmenu
I am fetching lots of thumbnails from a remote server and displaying them in
When fetching content from a database using activerecord, I would like to fetch a
i am fetching a page using get ajax call. $.get('/notification/viewmessage',{user:username},function(data){ //my code here });
My program is fetching messages from a database, which contains English, German and several
I'm fetching a row of data from a mysql-server using php, then encode it
I'm fetching some data from an MSSQL table using the mssql_fetch_object, but the text
I'm fetching data from all 3 tables at once to avoid network latency. Fetching
I have a quick question about fetching results from a weakly typed cursor and

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.