how to create sessions in netbeans

JAI BLOG Taste My Java Examples, materials and Many More Of Java http://www.sattvaq.com/jai How to create sessions in N...

1 downloads 134 Views 27KB Size
JAI BLOG Taste My Java Examples, materials and Many More Of Java http://www.sattvaq.com/jai

How to create sessions in Netbeans Sessions What is session? The Session is a variable to transfer the data from one place to another place. The session is really very useful in the web applications. Without sessions there is no web application. We can use session variable in any page to get the particular user data. We can create number of sessions in one application there is no limit to create sessions. Why is session? Session variable is to transfer the particular user data from one place to another place in an application. How to create or set the session? We can create or set the session variable, by creating object for HttpSession class. Lets we see about how to create session. //this is our local variable String uid=null; //her e we are getting the field name i.e. uid of userid from jsp page. //by using request object of HttpServletRequest class. uid=re quest.getParameter("uid"); //creating session variable by using s ession object of HttpSession class. HttpSession session=request .getSession(); // here "suid" is a session variable and uid is local variable of userid. session.setAttribute("suid",uid);

In the above code we are creating (or) setting the session, by using HttpSession Class object (i.e. session) and using the method of setAttribute(). In that setAttribute method() we are passing two variables those are “suid”, and “uid”. Here uid is local variable for userid, and suid is a session variable for that uid, we are creating that “suid” To get in any page. How to get the session? Here we will get the suid variable in another page by using getAttribute() method. But here also we will use session object to get the session attribute (i.e. “suid”).

Here I am using jsp page to get the session variable. Because jsp is combination of html and java. We can use java code into jsp page but that must be in scriptlet tags (i.e.open scriptlet tag). Now I have stored session attribute in local variable i.e. uid by using method getAttribute(). And here one more method is there i.e. toString. this method is to convert the any variable into string type of variable. So, we can use that local variable (uid) in any where.

page 1 / 1 Powered by TCPDF (www.tcpdf.org)