Solved

session between JSPs and Servlets

Posted on 2002-05-21
12
315 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
Comment Utility
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
Comment Utility
You don't need to declare
    HttpSession session;

session is predefined variable in jsp.
0
 
LVL 1

Author Comment

by:iamjhkang
Comment Utility
without it
it won't work either.
0
 
LVL 19

Expert Comment

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

CJ
0
 
LVL 5

Expert Comment

by:Jan Louwerens
Comment Utility
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
Comment Utility
Have you check the cookies in browser?

Show the code on how do you set session in servlet.
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 1

Author Comment

by:iamjhkang
Comment Utility
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
Comment Utility
Cookie is relevant to JSP session. Server container store the session_id in the browser cookie.
0
 
LVL 6

Expert Comment

by:kotan
Comment Utility
>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
Comment Utility
>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
Comment Utility
yes!
0
 
LVL 1

Author Comment

by:iamjhkang
Comment Utility
Good. thanks.
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

This article explains in simple steps how to renew expiring Exchange Server Internal Transport Certificate.
HOW TO: Connect to the VMware vSphere Hypervisor 6.5 (ESXi 6.5) using the vSphere (HTML5 Web) Host Client 6.5, and perform a simple configuration task of adding a new VMFS 6 datastore.
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

771 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now