set cookieless session variables in jsp

set cookieless session variables in jsp

I want to know how to set cookieless session variables in jsp, because when I run my application multiple times, multiple tabs get open and the session variable is shared among them(because these session variables internally make use of cookie). But I don't want that . So, please tell is it possible in jsp to use cookieless session variables ?

please reply asap.... its urgent. Thanks in advance

View Answers

May 16, 2013 at 3:23 PM

hi friend,

Try this code may this will be helpful for you

index.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@ page import = "javax.servlet.RequestDispatcher" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Session Example</title>
</head>
<body>
<form method="post" action="user.jsp">
User:<input type="text" name="user">
<input type="submit" value="send">
</form>
</body>
</html>

user.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title></title>
</head>
<body>
<%
//response.setHeader("Cache-Control","no-cache");
String user = request.getParameter("user");
if(!(user == null || user.isEmpty()))
{   
        session.setAttribute("user", user);
        String jsessionid = session.getId();
        out.println(jsessionid);
        response.sendRedirect("home.jsp?user="+user+";"+jsessionid);    
}

%>
</body>
</html>

home.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>home</title>
</head>
<body>
<p>Hello, <%= session.getAttribute( "user" ) %></p>
<%
Cookie cookie = null;
Cookie[] cookies = null;

if(!(session.getAttribute("user")==null))
{
Cookie userName = new Cookie("us",
          request.getParameter("user"));
Cookie sId = new Cookie("sessionId",
          session.getId());
cookies = request.getCookies();
for (int i = 0; i < cookies.length; i++)
{
    cookie = cookies[i];
    //out.println(cookie.getName());
    if((cookie.getName( )).compareTo("JSESSIONID") == 0 ){
       cookie.setMaxAge(0);
       response.addCookie(cookie);
      // out.println(cookie.getName()+" deleted");
}
}
}
%>
</body>
</html>

May 16, 2013 at 5:31 PM

thanq sir... but what is the usage of cookies here ???

Ads









Related Tutorials/Questions & Answers:
set cookieless session variables in jsp
set cookieless session variables in jsp  I want to know how to set cookieless session variables in jsp, because when I run my application multiple... that . So, please tell is it possible in jsp to use cookieless session variables
session variables server side
session variables server side  how to hold session variable for server side..?   request.getSession().setAttribute("wellSelection", uid
Advertisements
how to initialise variables in jsp?
how to initialise variables in jsp?   1. what is the problem in the folloing jsp <%@ page import="net.viralpatel.struts.helloworld.form.LoginForm" %> <%@ page language="java" contentType="text/html
session tracking in jsp
session tracking in jsp  hi... i am creating 3 jsp pages ie. login.jsp,display.jsp,userinput.jsp.. i am able to pass the value from login.jsp to display.jsp using session set attribut ang get attribute.. but same ting
how to set the values in jsp
how to set the values in jsp  how to set the values text boxs in jsp frm dbase via servlet
JSTL: Set Session Attribute
and jsp. That's why the jstl is too easy.  In jstl we will set the session...JSTL: Set Session Attribute     ... are using the jstl and there is a need to set a variable in the session. You all know
How do I set environment variables from Java?
How do I set environment variables from Java?  How do I set environment variables from Java
How do servlets work? Instantiation, session variables and multithreading
How do servlets work? Instantiation, session variables and multithreading  How do servlets work? Instantiation, session variables and multithreading
Duplicated Session Variables
Duplicated Session Variables     ... a new session variable because it has been created by the JSP container for you... name. Consider we have declared a session variable true in jsp page <%@ page
Session Timeour - JSP-Servlet
Session Timeour  Hi, How to create a session timeout page in JSP? Session timeout should happen after 15 mins of idle instance. Thanks ... the following link: http://www.roseindia.net/jsp/jsp-session-parameter
session in jsp
session in jsp In this section you will learn about session in JSP. Here you will learn about how to use the HttpSession in JSP. session in JSP is defined... the attribute value that is set using setAttribute() method within this session
JSP Session Parameter rewrite
JSP Session Parameter rewrite   ... in jsp. JSP session provides methods like getCreationtime(), getLastAccessedTime... to set the time out for each session. removeAttribute() method is used
Scripting Variables in JSP Custom Tag
Scripting Variables in JSP Custom Tag   ... are variables that are available to the JSP page when any JSP page is called. Scripting... of JSP page we mean page level variables declared by the JSP page. You can access
how to set time in jsp page
how to set time in jsp page   I need code for set the time in jsp code .iam using struts frame work back end oracle 10g ide is eclipse 6.0
Session In JSP
Session In JSP      ... of World Wide Web (WWW).    Session Management in JSP...; Cookies in JSP When cookie based session management is used, a token
JSp session time out
JSp session time out  Consider a case where we are working on a java... for it??   If you want to make session timeout programatically, you can use... the maximum time in seconds before a session becomes invalid. Its syntax
Clearing session in jsp
Clearing session in jsp  i have developed a web appilcation using jsp and when user try to logout, session is not clearing even though i have used session.invalidate() and session.removeAttribute("username
session value not get in many jsp page.
session value not get in many jsp page.  I am using servlet to set session by following code :- HttpSession session=request.getSession(false...,response); and get session value on jsp page by follwing:- String user=(String
session management - JSP-Servlet
session management  hi friends... hope u all dng fine. i am dng... think i am not using session management properly. and also in my web.xml file i said session timeout 1min. even it is also not working.wht to do :-( i will give
Variables
Variables  What are the difference between Static variables, instance variables and local variables
Passing java variables from JSP to Servlet - return null values
Passing java variables from JSP to Servlet - return null values  I want to pass some variables from a JSP page to a servlet. These variables are from... to get java variables from JSP tp servlet? If there is some error in my code
JSP bean set property
JSP bean set property   ... you a code that help in describing an example from JSP bean set property... it exists.     <jsp:set Property>This is used to set
how to Use jsp:setProperty to set one bean in other bean while to be set bean is of type java.util.List<beantype>
how to Use jsp:setProperty to set one bean in other bean while to be set bean...;jsp:useBean id="publisher" class="chapter.one.Publisher" scope="session"/> <..." class="chapter.one.Chapter" scope="session"/> <jsp:setProperty name
session maintaining after logged in in jsp
session maintaining after logged in in jsp  Hi. I have created a login page. after the user logged in successfully if the user gives refresh the the control goes to the website home page. i want to retain in the same page
ModuleNotFoundError: No module named 'django-cookieless'
ModuleNotFoundError: No module named 'django-cookieless'  Hi, My... named 'django-cookieless' How to remove the ModuleNotFoundError: No module named 'django-cookieless' error? Thanks   Hi, In your
Remove attribute in user defined session - JSP-Servlet
Remove attribute in user defined session  Hi, How we can set remove attribute for user defined session. I am appreciating if you are sending solution as soon as possible? Thanks, Sanat Kumar
Session expire and back button in jsp and servlet .
Session expire and back button in jsp and servlet .  Hii Sir... to invalidate the session after a certain interval of time and after clicking logout button.Means that after session expiry time and logout action no one can access
Preventing the creation of a Session in a Jsp Page
Preventing the creation of a Session in a Jsp Page          In jsp we have been provided the implicit session object. In jsp the session
The session Attribute of page Directive In JSP
for running the JSP page on the server. If you set the value of session object false... The session Attribute of page Directive In JSP  ... illustration of the session attribute of the page directive in JSP
can pass list of n values in session and get in jsp
can pass list of n values in session and get in jsp  In dao: am geting username,companyname,usertype and set to userBean and add to arraylist In servlet: list=userBean.selectUserBo(); HttpSession session = request.getSession
can pass list of n values in session and get in jsp
can pass list of n values in session and get in jsp  In dao: am geting username,companyname,usertype and set to userBean and add to arraylist In servlet: list=userBean.selectUserBo(); HttpSession session = request.getSession

Ads