• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 328
  • Last Modified:

session between JSPs and Servlets

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
iamjhkang
Asked:
iamjhkang
1 Solution
 
kotanCommented:
This may cause by the the cookies of the browser have been disable. Please check.
0
 
kotanCommented:
You don't need to declare
    HttpSession session;

session is predefined variable in jsp.
0
 
iamjhkangAuthor Commented:
without it
it won't work either.
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
cheekycjCommented:
in case1 aren't you missing an extra underscore in front of each var.

CJ
0
 
Jan LouwerensCommented:
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
 
kotanCommented:
Have you check the cookies in browser?

Show the code on how do you set session in servlet.
0
 
iamjhkangAuthor Commented:
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
 
kotanCommented:
Cookie is relevant to JSP session. Server container store the session_id in the browser cookie.
0
 
kotanCommented:
>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
 
iamjhkangAuthor Commented:
>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
 
kotanCommented:
yes!
0
 
iamjhkangAuthor Commented:
Good. thanks.
0

Featured Post

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!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now