Solved

session management in JSP

Posted on 2004-09-22
8
248 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

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.

Question has a verified solution.

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

Many businesses neglect disaster recovery and treat it as an after-thought. I can tell you first hand that data will be lost, hard drives die, servers will be hacked, and careless (or malicious) employees can ruin your data.
The advancement in technology has been a great source of betterment and empowerment for the human race, Nevertheless, this is not to say that technology doesn’t have any problems. We are bombarded with constant distractions, whether as an overload o…
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…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

831 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