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);
user unknown
  • 35,537
  • 11
  • 75
  • 121
saroj
  • 643
  • 8
  • 14
  • 25
  • You need a reference to the current request. Also: don't write Java code in a JSP. http://stackoverflow.com/questions/3177733/how-to-avoid-java-code-in-jsp-files – Matt Ball Mar 24 '12 at 06:30
  • initially i am using jsp but will replace jsp with jstl. How can i get a reference to the current `request`? will i define a separate `request class` ? – saroj Mar 24 '12 at 06:31
  • Can you post your exception? You might want to use request.getAttribute. It is difficult to understand what are you trying to achieve, it seems you don't really understand classes. Username ideally should be string within class User. – Aubergine Mar 24 '12 at 06:43
  • error is `cannot find symbol: variable request` i am new to java ,can you please give some suggestion by an answer how should i modify my java class – saroj Mar 24 '12 at 06:49

2 Answers2

0

In the servlet:

User yourUser = new User (1,"Mathews");

request.setAttribute("userMegaUserOrWhateverYouCall", yourUser); //

In the jsp scriptlet:

  <% User u  = (User) request.getAttribute("userMegaUserOrWhateverYouCall"); // may need casting %>

some html code

   <%= u.getUserName() %>

UPDATE: judging from your class, you need to go through tutorials. You have to use HttpServlet class.

Examples on servlets: http://www.servlets.com/jservlet2/examples/

Also consider using your TA help, they are specifically out there to help you for you ;-)

Aubergine
  • 5,862
  • 19
  • 66
  • 110
  • i appreciate for your answer but this is not what i am looking for, i want to call java class in jsp page, servlet is not present in my question. after login i fetch username from database by user id, so this sql statement in stead of wrting each time i want to define one sql statement in a java class and want to return username to all jsp pages – saroj Mar 24 '12 at 07:10
  • servlet is needed for dealing with requests/responses. I can't remember if you can do it in JSP, but the sole purpose of JSP is presentation, not the controlling flow of requests. You can google for jsp tutorials and you will see servlets everywhere, because JSP are translated to servlets anyway. http://en.wikipedia.org/wiki/Java_Servlet – Aubergine Mar 24 '12 at 07:22
0

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);
vaisakh
  • 1,041
  • 9
  • 19
  • one error is coming `cannot find symbol: variable userId` in `uName.getUserName(userId);` I think `Long userId` is of no use, so i replaced `userId` with `userid/userName` but same error is coming – saroj Mar 24 '12 at 08:13
  • Are you getting the error in the JSP page? Check the declarations carefully. Especially the variable name. Like userId vs userid. – vaisakh Mar 24 '12 at 08:19
  • ya in jsp page error is coming and in both case (userid/userId) this error is coming – saroj Mar 24 '12 at 08:25
  • How about the declaration? Have you declared userId correctly in the JSP page? Symbol not found means the variable is not declared (well, almost always) – vaisakh Mar 24 '12 at 08:30
  • some problem is arising in `userId`. i have declared like `Username uName = new Username(request.getSession()); uName.getUserName(userId);` any modification is required in java class? – saroj Mar 24 '12 at 09:30
  • I meant declaration of the variable userId. Some thing like:- String userId = null; Or int userId = 0; Depending on whether userId is a String or an integer. – vaisakh Mar 24 '12 at 17:03