Solved

session management in JSP

Posted on 2004-09-22
8
253 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Technology Partners: 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!

Question has a verified solution.

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

Ever visit a website where you spotted a really cool looking Font, yet couldn't figure out which font family it belonged to, or how to get a copy of it for your own use? This article explains the process of doing exactly that, as well as showing how…
In this blog post, we’ll look at how ClickHouse performs in a general analytical workload using the star schema benchmark test.
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…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

707 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