Solved

session management in JSP

Posted on 2004-09-22
8
241 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
dont accept anybodys answers now i got my solution on my own of this question.
0

Featured Post

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

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
reflection example 3 79
getting param value 7 51
excpetion with multiple catch 11 104
how do you get these � Marks in your text? 9 57
Possible fixes for Windows 7 and Windows Server 2008 updating problem. Solutions mentioned are from Microsoft themselves. I started a case with them from our Microsoft Silver Partner option to open a case and get direct support from Microsoft. If s…
Restoring deleted objects in Active Directory has been a standard feature in Active Directory for many years, yet some admins may not know what is available.
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…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

763 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

12 Experts available now in Live!

Get 1:1 Help Now