Solved

session between JSPs and Servlets

Posted on 2002-05-21
12
322 Views
Last Modified: 2010-04-01
Index.jsp ======================

<%@page contentType="text/html;charset=euc-kr" session="true"%>
<%@page
     import="javax.servlet.*"
%>
<%!
     String _result;
     String _confirmBOD;
     String __result;
     String __confirmBOD;
     HttpSession session;
%>
<%
     _result = (String)session.getAttribute("Result");
     _confirmBOD = (String)session.getAttribute("ConfirmBOD");

     session.setAttribute("Result","result : in JSP");
     session.setAttribute("ConfirmBOD","confirmBOD : in JSP");
     
     __result = (String)session.getAttribute("Result");
     __confirmBOD = (String)session.getAttribute("ConfirmBOD");
     
%>
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=euc-kr"/>
</head>


Case 1 ==========================
<body onLoad="alert('Result : <%=_result%>'+'\n'+'ConfirmBOD :<%=_confirmBOD%>');">

Case 2 ==========================
<body onLoad="alert('Result : <%=__result%>'+'\n'+'ConfirmBOD :<%=__confirmBOD%>');">

Case 1 won't work.
Case 2 works OK.

In case 1 the session variables "Result" and "ConfirmBOD" is set in Servlet and doesn't work.
As you expect, Case 2 works well.

How can I get the session variables and their values set in servlets in JSP?

Thanks.

FYI. This page(Index.jsp) call a servlet named BODManager, and the servlet sets value and refresh this page(Index.jsp)
0
Comment
Question by:iamjhkang
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
12 Comments
 
LVL 6

Expert Comment

by:kotan
ID: 7026314
This may cause by the the cookies of the browser have been disable. Please check.
0
 
LVL 6

Accepted Solution

by:
kotan earned 70 total points
ID: 7026319
You don't need to declare
    HttpSession session;

session is predefined variable in jsp.
0
 
LVL 1

Author Comment

by:iamjhkang
ID: 7026789
without it
it won't work either.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 19

Expert Comment

by:cheekycj
ID: 7027481
in case1 aren't you missing an extra underscore in front of each var.

CJ
0
 
LVL 5

Expert Comment

by:Jan Louwerens
ID: 7028174
You are only setting the varibles in the session:
session.setAttribute("Result","result : in JSP");
session.setAttribute("ConfirmBOD","confirmBOD : in JSP");

to retrieve them, simply call:
String result = session.getAttribute("Result").toString();
String confirmBOD = session.getAttribute("ConfirmBOD").toString();


Also, be careful of your globally declared variables (those within the <%! %> block. These variable are global to ALL pages, so that if 2 users happen to hit the page at exactly the same time, they could possiblr overwrite each others' values, depending upon task switching times of the processes. If you want to the variables to be seperate for each page FOR EACH USER, you should place the variable declarations inside a <% %> block.
Functions should stay within the <%! %> block, but use the synchronized keywork if necessary...
0
 
LVL 6

Expert Comment

by:kotan
ID: 7028613
Have you check the cookies in browser?

Show the code on how do you set session in servlet.
0
 
LVL 1

Author Comment

by:iamjhkang
ID: 7028682
kotan!
Thanks.
because of cookies? I don't think so, Cookie is not relevant to JSP session at all.

Anyhow without "HttpSession session", it works OK.
but why doesn't work if I access through IP address? it work Ok when I access through the domain name?
Do you know?
0
 
LVL 6

Expert Comment

by:kotan
ID: 7028740
Cookie is relevant to JSP session. Server container store the session_id in the browser cookie.
0
 
LVL 6

Expert Comment

by:kotan
ID: 7028751
>but why doesn't work if I access through IP address? it work Ok when I access through the domain name?

Strange things. I don't know. Maybe others can help.
0
 
LVL 1

Author Comment

by:iamjhkang
ID: 7028824
>Cookie is relevant to JSP session. Server container store the session_id in the browser cookie.

You mean if I turn off Cookie in IE then session doesn't work?
0
 
LVL 6

Expert Comment

by:kotan
ID: 7028837
yes!
0
 
LVL 1

Author Comment

by:iamjhkang
ID: 7028889
Good. thanks.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

When using a search centre, I'm going to show you how to configure Sharepoint's search to only return results from the current site collection. Very useful when using Office 365 with multiple site collections.
A big percent of today’s marketing activity is performed through the online environment. The marketing strategies that have existed a decade ago no longer relate to what’s happening today. We’re currently facing a revolutionary era, called the digit…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question