Solved

session management in JSP

Posted on 2004-09-22
8
243 Views
Last Modified: 2013-12-03
I m using different forms in single JSP. I have one hidden field named block, I m changing its value to accessing different forms with eg. if (block.epuals(“value”))-else if (block.epuals(“elsevalue”)) loops. Now I have to redirect from one JSP to other JSP with certain block value and want to load a form on that JSP which falls under that block value. I know I have to use session id. But I don’t have any idea how to use session. Can anyone give me some idea.
Eg.
JSP.1:
<%
      if (block.equals(“value”))
      {
%>
<form>
</form>
<%
      }
      else if (block.equals(“value1”))
      {
%>
<form>
</form>
<%
      }
%>
JSP.2:
Now here I m changing the value of block = value1
And I want to redirect to JSP.1 and wants to load form falls under block=value1.

Please guide me……..
0
Comment
Question by:kaushal_cooldude
  • 3
  • 2
8 Comments
 
LVL 6

Expert Comment

by:manuel_m
ID: 12121823
i hope i understand you correct :)
maybe this can help you.

you can create a new session with:
request.getSession();

you can set an attribute in your session that you can read out.
e.g.

<%
     if (block.equals(“value”))
     {
%>
<form>
</form>
<%
       // set attribute - "block" is the attribute-name and "value" the attribute-value
       request.getSession().setAttribute("block", "value");
     }
     else if (block.equals(“value1”))
     {
%>
<form>
</form>
<%
       // set attribute
       request.getSession().setAttribute("block", "value1");
     }
%>
     
to read out the attribute on JSP2.jsp:

<%
String blockValue = (String) request.getSession().getAttribute("block");
%>

to include a jsp use

<%@ include file="JSP1.jsp" %>


0
 

Author Comment

by:kaushal_cooldude
ID: 12130523
hello friends,
this will explain what i want.......
I used the suggession but it is not working.

I have 3 JSPs each of them have multiple <form></form>s. They are accessed by chacking the "block"'s value in if(){} or else if(){}.
like:

1.jsp -->

<%@ page language="java" import="java.sql.*,java.io.*,javax.naming.*,javax.sql.*,java.util.*,java.lang.*,java.text.*,java.net.*,sun.jdbc.rowset.*" session="true"%>
<%
      String strBlock=request.getParameter("block");
      if (strBlock==null)
      {
      %>            
      <Script Language="JavaScript">
            <!--
                  function setblock()
                  {
                        document.form1.block.value="view";
                        document.form1.submit();
                  }
            //-->
      </Script>
      </HEAD>
      <BODY BGCOLOR="#FFFFFF">
      <form name="form1" action="candvalidform.jsp">
      <INPUT type="button" value="View" name="Button" onClick="setblock()">
      <input type="hidden" name="block">
      </form>
      </BODY>
      </HTML>
      <%
      }
      else if(strBlock.equals("view"))
      {
            %>
            <HTML>
            <HEAD>
            <Script Language="JavaScript">
            <!--
                  function call()
                  {
                        document.form2.block.value=null;
                        window.location.href='1.jsp'
                  }
                  function seldata()
                  {
                        document.form2.block.value="select";
                        document.form2.submit();
                  }
            
            //-->
            </Script>
            </HEAD>
            <BODY BGCOLOR="#FFFFFF">
            <form name="form2" action="2.jsp">
            <INPUT type="button" name="select" value="SELECT" onClick="seldata()">
            <input type="button" name="back" value="BACK" onClick="call()">
            <input type="hidden" name="block">
            </form>
            </BODY>
            </HTML>
<%
      }
%>

2.jsp -->

now here i used some code and change the value of that hidden block to view and redirecting to 1.jsp. by this i want to load form2 which falls under the else if condition. but when the 1.jsp is get loaded it takes the block's value null so what i m expacting is not happening.
now i think the problem is clear to u all.
please help me to solve this problem. and please state the comments wherever needed because i want to understand the session management concept.

0
 

Author Comment

by:kaushal_cooldude
ID: 12130533
please ignore the previous comment...... this is a original......

hello friends,
this will explain what i want.......
I used the suggession but it is not working.

I have 3 JSPs each of them have multiple <form></form>s. They are accessed by chacking the "block"'s value in if(){} or else if(){}.
like:

1.jsp -->

<%@ page language="java" import="java.sql.*,java.io.*,javax.naming.*,javax.sql.*,java.util.*,java.lang.*,java.text.*,java.net.*,sun.jdbc.rowset.*" session="true"%>
<%
      String strBlock=request.getParameter("block");
      if (strBlock==null)
      {
      %>            
      <Script Language="JavaScript">
            <!--
                  function setblock()
                  {
                        document.form1.block.value="view";
                        document.form1.submit();
                  }
            //-->
      </Script>
      </HEAD>
      <BODY BGCOLOR="#FFFFFF">
      <form name="form1" action="1.jsp">
      <INPUT type="button" value="View" name="Button" onClick="setblock()">
      <input type="hidden" name="block">
      </form>
      </BODY>
      </HTML>
      <%
      }
      else if(strBlock.equals("view"))
      {
            %>
            <HTML>
            <HEAD>
            <Script Language="JavaScript">
            <!--
                  function call()
                  {
                        document.form2.block.value=null;
                        window.location.href='1.jsp'
                  }
                  function seldata()
                  {
                        document.form2.block.value="select";
                        document.form2.submit();
                  }
            
            //-->
            </Script>
            </HEAD>
            <BODY BGCOLOR="#FFFFFF">
            <form name="form2" action="2.jsp">
            <INPUT type="button" name="select" value="SELECT" onClick="seldata()">
            <input type="button" name="back" value="BACK" onClick="call()">
            <input type="hidden" name="block">
            </form>
            </BODY>
            </HTML>
<%
      }
%>

2.jsp -->

now here i used some code and change the value of that hidden block to view and redirecting to 1.jsp. by this i want to load form2 which falls under the else if condition. but when the 1.jsp is get loaded it takes the block's value null so what i m expacting is not happening.
now i think the problem is clear to u all.
please help me to solve this problem. and please state the comments wherever needed because i want to understand the session management concept.

0
 
LVL 6

Accepted Solution

by:
manuel_m earned 20 total points
ID: 12133693
i think you have to use the post method in your form:

<form name="form2" action="2.jsp" method="post">

---

whats is the value you are getting when you read out the String strBlock?

System.out.println("blockvalue: "+ (String) request.getParameter("block"));

instead of using javascript for setting the value variable you can set an attribute in the session.
then you can read out the session attribute to access the value.

try this:

//for setting the variable in form1 - java code- if condition
request.getSession().setAttribute("block", "view");

//for setting the variable in form2 - else if condition
request.getSession().setAttribute("block", "select");

//for reading out the session variable - in if and else if condition!
String strBlock = (String) request.getSession().getAttribute("block");


example for if condition:

<%@ page language="java" import="java.sql.*,java.io.*,javax.naming.*,javax.sql.*,java.util.*,java.lang.*,java.text.*,java.net.*,sun.jdbc.rowset.*" session="true"%>
<%
     String strBlock=request.getParameter("block");
     if (strBlock==null)
     {

     // >>> setting attribute
     request.getSession().setAttribute("block", "view");

%>          

     </HEAD>
     <BODY BGCOLOR="#FFFFFF">
     <form name="form1" action="1.jsp" method="post">
     <INPUT type="button" value="View" name="Button">
     <input type="hidden" name="block">
     </form>
     </BODY>
     </HTML>
     <%
     } ....
0
 

Author Comment

by:kaushal_cooldude
ID: 12685738
dont accept anybodys answers now i got my solution on my own of this question.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

When we talk about DevOps toolchains, I sometimes wonder how many people really get what we’re talking about. I don’t know if it’s just semantics or tone or something else, but sometimes I think it just sounds like buzzword sausage. So it’s always …
Note: This is the third blog post in a series on email clearinghouses (https://www.xmatters.com/alert-management/blog-email-has-failed-us?utm_campaign=70138000000ydLoAAI&utm_source=exex&utm_medium=article&utm_content=blog-post).   We’ve been talki…
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

896 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

16 Experts available now in Live!

Get 1:1 Help Now