Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Posting session variables in single JSP page

 
Mike London
Ranch Hand
Posts: 1185
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I'm trying to do something I think should be really easy, but it isn't working.

Basically, within a single JSP page, I'm trying to put a variable into the session scope and when the JSP page posts to itself, I want to know if the variable is there and take appropriate action.

I don't have a doPost() or doGet() as I would in a servlet, but the code compiles fine; it just doesn't work.

Any suggestions would be appreciated.

Thanks.

-- Mike


<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ page import="javax.mail.Session" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">

<title>My JSP 'Test.jsp' starting page</title>

<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">

<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>

<FORM name="TestForm" method="post">

<body>
Test Session Variables. <br>

<%
if (session.getAttribute("test") == null)
{
String test = "hello First";

// put in session
session.setAttribute("hello", test);
out.println("First time in Webpage, \"Hello First\" added to session");
}
else
{
// already in session
String testme = (String) session.getAttribute("test");
out.println("From session: " + testme);
}

%>

<INPUT type="Submit" name="submit" value="Enter" />
</body>
</form>
</html>
 
Paul Clapham
Sheriff
Pie
Posts: 20764
30
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you put an attribute named "hello" into the session, and then try to get an attribute named "test" out of the session, you should not be surprised that it is not there.
 
Mike London
Ranch Hand
Posts: 1185
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, sorry for the typo. That doesn't resolve the issue, however.

*** Even with the corrected code, I get the same results each time I click Enter:

-----------------------------------------------------
Test Session Variables.
First time in Webpage, "Hello First" added to session
-----------------------------------------------------

I'm not seeing the saved session variable.

What's wrong?

Here's the updated code which *still* doesn't work as expected. <s>.

That is, the code that shows the session variable was retrieved is never displayed regardless of how my times I post the form...

Thanks in advance for any insight as to what's happening here.

-- M

===============================================================


<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ page import="javax.mail.Session" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">

<title>My JSP 'Test.jsp' starting page</title>

<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">

<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>

<FORM name="TestForm" method="post">

<body>
Test Session Variables. <br>

<%
if (session.getAttribute("test") == null)
{
String test = "hello First";

// put in session
session.setAttribute("test", test);
out.println("First time in Webpage, \"Hello First\" added to session");
}
else
{
// already in session
String testme = (String) session.getAttribute("test");
out.println("***** From session: " + testme);
}

%>

<INPUT type="Submit" name="submit" value="Enter" />
</body>
</form>
</html>
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It worked for me (once I got rid of the typo in the import directive).
Check to see if your browser has session cookies disabled.
[ December 04, 2005: Message edited by: Ben Souther ]
 
Mike London
Ranch Hand
Posts: 1185
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ben,

Typo in the import directive? That was all hard-coded by MyEclipse. Not sure whit it added that. <s>

In any case, the problem turned out to be my $$$(#@!!! firewall. A "privacy" setting was keeping this page from working. I _just_ upgraded to Firefox! 1.5 andmy firewall wasn't totally configured yet.

Thanks very much, and to others, who replied!!!

-- Mike
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Mike London:
Hi Ben,

Typo in the import directive? That was all hard-coded by MyEclipse. Not sure whit it added that. <s>


Error:
org.apache.jasper.JasperException: Unable to compile class for JSP

Generated servlet error:
Only a type can be imported. javax.mail.Session resolves to a package


Since javax.mail wasn't used in the page, I just removed it.
Then things worked just fine.



Glad you found the problem.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic