Solved

session between JSPs and Servlets

Posted on 2002-05-21
12
318 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
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
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 
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

Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
maven project in eclipse 11 59
java beans and EJBs 5 192
TreeSet comparator example 7 95
Is possible to make a jsp script work with oracle wallet? 7 101
If you thought ransomware was bad, think again! Doxware has the potential to be even more damaging.
Each year, investment in cloud platforms grows more than 20% (https://www.immun.io/hubfs/Immunio_2016/Content/Marketing/Cloud-Security-Report-2016.pdf?submissionGuid=a8d80a00-6fee-4b85-81db-a4e28f681762) as an increasing number of companies begin to…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

772 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