Solved

session between JSPs and Servlets

Posted on 2002-05-21
12
319 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
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Many businesses neglect disaster recovery and treat it as an after-thought. I can tell you first hand that data will be lost, hard drives die, servers will be hacked, and careless (or malicious) employees can ruin your data.
ConnectWise and their customers need to ensure critical alerts automatically reach the right person at the right time. MSP superheros efficiently respond to these alerts key is providing automatic, intelligent alerting that generates a complete audiā€¦

828 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