• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 262
  • Last Modified:

session management in JSP

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
kaushal_cooldude
Asked:
kaushal_cooldude
  • 3
  • 2
1 Solution
 
manuel_mCommented:
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
 
kaushal_cooldudeAuthor Commented:
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
 
kaushal_cooldudeAuthor Commented:
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
 
manuel_mCommented:
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
 
kaushal_cooldudeAuthor Commented:
dont accept anybodys answers now i got my solution on my own of this question.
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now