File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JSP and the fly likes Posting session variables in single JSP page Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "Posting session variables in single JSP page" Watch "Posting session variables in single JSP page" New topic
Author

Posting session variables in single JSP page

Mike London
Ranch Hand

Joined: Jul 12, 2002
Posts: 1083
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
Bartender

Joined: Oct 14, 2005
Posts: 18987
    
    8

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

Joined: Jul 12, 2002
Posts: 1083
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

Joined: Dec 11, 2004
Posts: 13410

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 ]

Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Mike London
Ranch Hand

Joined: Jul 12, 2002
Posts: 1083
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

Joined: Dec 11, 2004
Posts: 13410

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Posting session variables in single JSP page