Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Answers Viewqa JSP-Servlet set cookieless session variables in jsp
Login         

View Questions and Answers by Category

Advertisements


 
Have Programming Question? Ask it here!
 
 
 


purnima khurana
set cookieless session variables in jsp
2 Answer(s)      a year and 5 months ago
Posted in : JSP-Servlet


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


Advertisement
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 ???



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
How to implement session variables - JSP-Servlet
How to implement session variables  Hi, I have a servlet which gets parameter from a jsp page(x). it is stored in a variable in servlet as shown below. the servlet response is another jsp page(y). I should put the value of amp
Advertisements
session variables server side
session variables server side  how to hold session variable for server side..?   request.getSession().setAttribute("wellSelection", uid
JSP - Update displayed content & session variables when user clicks on a button - JSP-Servlet
JSP - Update displayed content & session variables when user clicks on a button  Hi, I'm trying to setup a form in which the user can click... JSP to do some internal check and then increment the value of a number
JSP - Update displayed content & session variables when user clicks on a button - JSP-Servlet
JSP - Update displayed content & session variables when user clicks... JSP to do some internal check and then increment the value of a number... combination of JSP and javascript would work since my knowledge base is in Java. Here
How can we register the variables into a session?
How can we register the variables into a session?  How can we register the variables into a session
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
Variables in a Servlet - JSP-Servlet
Variables in a Servlet   Hi, I'm using some variables in a servlet1 (which i get from a jsp, like "String x = request.getParameter("xyz");"). the servlet response is a jsp page which after taking some more parameters gets
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
jsp - session - JSP-Servlet
JSP - Session  How to manage session in JSP
JSP Session Object
JSP Session Object  JSP Session Object?   Session Object denotes the data associated with a specific session of user. The class... in a application where variables are stored for the entire user session. Methods
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
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
How do I set environment variables from Java?
How do I set environment variables from Java?  How do I set environment variables from Java
jsp scope variables - JSP-Interview Questions
jsp scope variables  what is the importance of page,session,request n application scope variables in JSP?Am not understanding where which scope..., the jsp object is accessible from pages that belong to the same session from
assignment of variables - JSP-Servlet
page on the server. The loginPage,jsp should something like.../jsp/ Thanks
Jsp Scope Variables - JSP-Interview Questions
Jsp Scope Variables  what is the importance of page,session,request n application scope variables in JSP?Am not understanding where which scope variable we have to use in programs..let me know when we have to use them seperately
session in jsp - Java Beginners
  Hi friend, session is implicit object in jsp. using session oject in jsp. first u set using following methods String name... let me know how to create a session in jsp. Session for jsp with two side
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
pasing variables in jsp file - JSP-Servlet
pasing variables in jsp file  How can more than one pages of jsp files share variables and how can one pass local variables from one jsp file to another jsp file?  Hi Friend, To pass variables from JSP file
Jsp Scope Variables - Java Interview Questions
Jsp Scope Variables  what is the importance of page,session,request n application scope variables in JSP?Am not understanding where which scope... in advance.  Hi Friend, JSP Scope Variables: Page scope-It makes
how to get javascript variables into jsp - JSP-Servlet
how to get javascript variables into jsp  How to pass variables from java script into jsp page  Hi Friend, Try it: 1)pass.jsp: var msg="Hello World" window.location.href="show.jsp?value=" + msg
Session removing - JSP-Servlet
Session removing  Hi, I am destroying session by using session.invalidate() in JSP but I am not able to destroy it completely can anyone help me... has been in session using session. setAttribute() but at log off I am using
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 concept - JSP-Servlet
session concept  Hello friends, How can we track unexpectedly closing a window when a jsp project running with session concept. And this tracking should update the log in status in data base
session - JSP-Servlet
session  How to manage session for a particular user ..using session... then userid is set in your session.RegardsAmar  Answer:If you get id as a integer from mlid field then userid is set in your session.RegardsAmar
Variables
Variables  What are the difference between Static variables, instance variables and local variables
Session concept - JSP-Servlet
Session concept  Hai friends, I am doing a jsp project with session concept. If one person is not accessing his logged window for more than 10 minutes it gets automatically log out.Anybody explain me the reason
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
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 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
Advertisements
 

 

 

DMCA.com