session

session

View Answers

January 16, 2009 at 11:01 PM

Hi friend,

There are a number of problems that arise from the fact that HTTP is a "stateless" protocol. In particular, when you are doing on-line shopping, it is a real annoyance that the Web server can't easily remember previous transactions.In session tracking maintain the state of a user throughout
when the user complete the shopping.

There are four types of Session Tracking
1.Session
2.Cookies
3.Url-rewriting
4.Hidden

Code for Session solve the problem and some step to be remember:

1.Code to login the user.

"login.jsp"

<html>
<head>
<script>
function validLogin()
{
if(document.loginform.userName.value=="")
{
alert("Please enter username.");
document.loginform.userName.focus();
return false;
}
if(document.loginform.password.value=="")
{
alert("Please enter password.");
document.loginform.password.focus();
return false;
}
return true;
}
</script>
</head>
<body>
<form name="loginform" method="post" action="loginaction.jsp" onsubmit="return validLogin();">
<table>
<tr>
<td colspan=2>
<h1>User Login</h2>
</td>
</tr>

<tr>
<td>Login Name</td>
<td><input type="text" name="userName" value=""></td>
</tr>
<tr>
<td>Password</td>
<td><input type="password" name="password" value=""></td>
</tr>

<tr>
<td></td>
<td><input type="submit" name="Submit" value="Submit"></td>
</tr>
</table>
</form>


</body>
</html>













January 16, 2009 at 11:09 PM

step 2 : When the user successfully login set the user in session


<%@ page language="java" import="java.sql.*;"%>
<%

String userName = request.getParameter("userName");
String password = request.getParameter("password");




System.out.println("MySQL Connect Example.");
Connection conn = null;
String url = "jdbc:mysql://localhost:3306/";;;
String dbName = "userlogin";
String driver = "com.mysql.jdbc.Driver";
String username = "root";
String userPassword = "root";
try {
Class.forName(driver).newInstance();
conn = DriverManager.getConnection(url+dbName,username,userPassword);
Statement st = conn.createStatement();
String strQuery = "select count(*) from user where username='"+userName+"' and password='"+password+"'";
out.println(strQuery);
ResultSet rs = st.executeQuery(strQuery);

if(rs.next())
{
if(rs.getInt(1)>0)
{
session.setAttribute("userid",userName);
response.sendRedirect("welcome.jsp");
}
else
{
response.sendRedirect("login.jsp");
}
}
System.out.println("Connected to the database");
conn.close();
System.out.println("Disconnected from database");
} catch (Exception e) {
e.printStackTrace();
}
%>






January 16, 2009 at 11:11 PM

3.If the user succesfully the it goes to "welcome.jsp" else back to the "loginaction.jsp"

"welcome.jsp"

<html>
<body>
<%
String user = session.getAttribute("userid").toString();
%>
<br><br>
<h1>Welcome
<%= user%></h1>
</body>
</html>


Thanks









Related Tutorials/Questions & Answers:
Session
Session   how to session maintaining in servlet with use of hidden fieds
session
session  is there any possibility to call one session from another session
Advertisements
session
session  is there any possibility to call one session from another session
session
session  Session management in Java
Session
Session  How fined session is new or old
Session
Session   How can we set the inactivity period on a per-session basis?   We can set the session time out programmatically by using the method setMaxInactiveInterval() of HttpSession
session
Session Management in PHP  Handling session in PHP. Can anyone please explain it with the help of an existing example about, how to handle a session while a user is logged in PHP
session
session  Which methods can be invoked by the container on a stateless session bean
session
session   explain sessioln tracking in jsp and display program
Session
Session   hii, What Is a Session?   hello,ADS_TO_REPLACE_1 Sessions are commonly used to store temporary data to allow multiple PHP pages to offer a complete functional transaction for the same visitor
session
session  What mechanisms are used by a Servlet Container to maintain session information?   Servlet Container uses Cookies, URL rewriting, and HTTPS protocol information to maintain the session
Session
;A servlet session is created and stored on the server side. The servlet container keeps.... To the maintain the session, Web clients must pass back a valid session.... In this sense, the session is also stored by the client, but only as a token reference
Session
Session  What Is a Session?   Hi friends, A session... subsequent HTTP requests. There is only one session object available to your PHP scripts at any time. Data saved to the session by a script can be retrieved
session
session  how can transfer data of one page to another page using session in java swing
session
session  create multiple login on one page in php but how to use session
Session
Session   why do we need session? actually what stored in a session... and user would not able to retrieve the information. So, Session provides that facility to store information on server memory. 2)Variables are stored in session
session
session  how to implement login-logout session????   Please visit the following links: http://www.roseindia.net/quickguide/tomcat/Logout.shtml http://www.roseindia.net/jsp/javascriptpagerefresh.shtml http
Session in Php
Session in Php   What Is a Session
session hanling
session hanling  session handling in facelets/jsf
session handling
session handling  how we can handle session in core java
Session management
Session management  How To Maintain Session in jsp for Online exam project
session Maintainess
session Maintainess  how to maintain session in struts 2
php session
php session  what is session
Terminate the session
Terminate the session  How to terminate the session
session tracking
session tracking  What are the common mechanisms used for session tracking?    Cookies SSL sessions URL- rewriting
session invalidate.
session invalidate.  how to invalidate session? i am calling session.invalidate()but not working
Session control
Session control  Could we make session control in JSP language without having any help of other framework
Session Mamangemnt
Session Mamangemnt  Hi all.. I want invalidate the the session for an element in an array .. for EX: a[0] has to invalidate rest a[1],a[2]... has to be in session .. please help me out urgent
Session Mamangemnt
Session Mamangemnt  Hi all.. I want invalidate the the session for an element in an array .. for EX: a[0] has to invalidate rest a[1],a[2]... has to be in session .. please help me out urgent
session tracking
session tracking  session tracking is of four type. 1.hidden form field 2.url rewriting 3.http session interface 4.cookies. please explan the third type with example
Servlet-session
Servlet-session  step by step example on session in servlets
Session Object
Session Object   What is the Max amount of information that can be saved in a Session Object ?   There is no such limit on the amount of information that can be saved in a Session Object. The only limit is the Session
Session tracking
Session tracking  What are the different ways for session tracking?    Cookies URL rewriting HttpSession Hidden form fields
session tracking
session tracking  What are the different ways for session tracking?   Cookies, URL rewriting, HttpSession, Hidden form fields
Session ID
Session ID  How can I change the session ID of the page?   The servlet session represents a relationship between the servlet container...' session ID values because it is very likely to result in the loss of the session
Session tracking
Session tracking  How can I enable session tracking for JSP pages if the browser has disabled cookies?   By default session tracking uses cookies to associate a session identifier with a user. If the browser does
Session Timeout
Session Timeout  Hi, we are using JBoss 4.0.2 I have edited.... But, the session timeout is not happening for 120 mins... the below is the snip from my web.xml file <session-config> <session
Maximum length of Session ( $_SESSION ) in PHP.
Maximum length of Session ( $_SESSION ) in PHP.  sir, i want to know that how much limit of storing data into session in php. i mean can i store full product descriptions into session. Thank u
Session Object
Session Object  Why do we require Session Object?  Hello.... The session object is used by the developers to store and retrieve user's..., java.lang.Object value)- for storing the an object in session. getAttribute
session object
session object  how to make session from one servlet to another servlet for an integer variable.   Please visit the following link: http://www.roseindia.net/servlets/SessionAttributeListenerExample.shtml
Session time
Session time   I need to develop an application has a user id and password to login and maintain the session for 8 minutes. All the pages under the login are not available if there is no activity for 8 minutes
session maintanance
session maintanance  Hi i am developing a small project using j2ee... i have some problem in maintaing session in my project... in my project when... to the application....suggest me some codes so that i can maintain session in my project
Starting the session.
session_start() session_start function creates a session or resumes the current one based on the  current session id that is being passed via a request like GET, POST, or a cookie. For using named session, first you will have
Session Factory
Session Factory  Define the session factory interface in hibernate... and requests the sessions. Session session = HibernateUtil.getSessionFactory... access it concurrently and requests the sessions Session session
session management
session management  hello, I m doing project completly jsp's .In that jsp only coding and designing,now i m getting a problem that when session timeout some sql statements should execute like to make logout status to true.so
session maintaining
session maintaining  Hi. I have created a login page. Consider the user is logged in. Now if he/she gives refresh it goes to home page i.e. the home page of the website. i want to maintain session here. if user gives refresh
session maintaining
session maintaining  Hi. I have created a login page. Consider the user is logged in. Now if he/she gives refresh it goes to home page i.e. the home page of the website. i want to maintain session here. if user gives refresh
session maintaining
session maintaining  Hi. I have created a login page. Consider the user is logged in. Now if he/she gives refresh it goes to home page i.e. the home page of the website. i want to maintain session here. if user gives refresh
Session management
Session management  I am new to servlet....developing a project in servlet-jsp.... i want to know about session management... that i don't want to let a user can copy url address and run it on same os and other browser Thanks
Session Tracking
Session Tracking  How Session Tracking ?   As we know that the Http is a stateless protocol, means that it can't persist the information.... Session Tracking can be done in three ways:ADS_TO_REPLACE_1 Hidden Form

Ads