user5432358
user5432358

Reputation: 23

Passing session variable values between pages

Hello I have multiple JSP pages implementing a contact type database. I am passing data from one page to the next using html input variables. It works from the first jsp to the second apparently. But after that it does not work anymore. The name and number variables (param.name) no longer are accessible in the fourth jsp, or it atleast appears that way, because the code in the fourth jsp is not returning the value. Below I have the four jsp. Tell me if it is my code or am I missing something? I want to make the input variables available on all jsp.

jsp 1:

<%@ page import="java.io.*,java.util.*,java.sql.*"%>
<%@ page import="javax.servlet.http.*,javax.servlet.*" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/sql" prefix="sql"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

        <sql:setDataSource var="data" driver="org.apache.derby.jdbc.ClientDriver"
     url="jdbc:derby://localhost:1527/testing"
     user="testing"  password="testing"/>
    </head>
    <form action="result.jsp"> 
     Enter name:
    <input type="text" name="name" scope='session'>

    Enter Number
    <input type="text" name="number" scope='session'>
    <input type="submit" value="Search Rolodex" name="submit" />
    </form>

        <br></br>



    <form name="add" action="addData.jsp">
        <input type="submit" value="Add Contact" name="addc" />
    </form>
    <body>

jsp 2:

<%@ page import="java.io.*,java.util.*,java.sql.*"%>
<%@ page import="javax.servlet.http.*,javax.servlet.*" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/sql" prefix="sql"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

        <sql:setDataSource var="data" driver="org.apache.derby.jdbc.ClientDriver"
     url="jdbc:derby://localhost:1527/testing"
     user="testing"  password="testing"/>
    </head>

    <body>
        <form name="editp" action="editData.jsp">

        <sql:query var="result" dataSource="${data}">
            SELECT NAME, NUMBER FROM DATA WHERE NAME = '${param.name}' OR NUMBER = '${param.number}'
        </sql:query>

        <table border="1">
            <!-- column headers -->
            <tr>
                <c:forEach var="columnName" items="${result.columnNames}">
                    <th><c:out value="${columnName}"/></th>
                    </c:forEach>
            </tr>
            <!-- column data -->
            <c:forEach var="row" items="${result.rowsByIndex}">
                <tr>
                    <c:forEach var="column" items="${row}">
                        <td><c:out value="${column}"/></td>
                    </c:forEach>
                </tr>
            </c:forEach>
        </table>


                <input type="submit" value="Edit Contact" name="edib" />
            </form>









    </body>
</html>

jsp 3:

<%@ page import="java.io.*,java.util.*,java.sql.*"%>
<%@ page import="javax.servlet.http.*,javax.servlet.*" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/sql" prefix="sql"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

        <sql:setDataSource var="data" driver="org.apache.derby.jdbc.ClientDriver"
     url="jdbc:derby://localhost:1527/testing"
     user="testing"  password="testing"/>

    </head>
    <body>
       <form name="editred" action="editRedirect.jsp">


    <input type="text" name="newname" scope='session'>
    <input type="text" name="newnumber" scope='session'>

    <input type="submit" value="Close" name="close" />   
        </form>   
    </body>

</html>

jsp 4

<%@ page import="java.io.*,java.util.*,java.sql.*"%>
<%@ page import="javax.servlet.http.*,javax.servlet.*" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/sql" prefix="sql"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

        <sql:setDataSource var="data" driver="org.apache.derby.jdbc.ClientDriver"
                           url="jdbc:derby://localhost:1527/testing"
                           user="testing"  password="testing"/>
    </head>
    <body>

        <form name="sendhome" action="index.jsp">


        <sql:update var="updatevariable" dataSource="${data}">
            UPDATE DATA
            SET NAME = '${param.newname}'
            WHERE NAME = '${param.name}'
        </sql:update> 
        <sql:update var="updatevariable2" dataSource="${data}">
            UPDATE DATA
            SET NUMBER = '${param.newnumber}'
            WHERE NUMBER = '${param.number}'
        </sql:update>  
            <input type="submit" value="Home" name="returnhome" />  
</form>
    </body>

</html>

Upvotes: 2

Views: 2392

Answers (1)

Ish
Ish

Reputation: 4154

Here's how I understand the logic of your code and the flow of your pages. Let me know if my assumptions are incorrect.

JSP 1: This page provides a form to search for contacts. It has 2 inputs, one for name and one for number. When form is submitted, it goes to your JSP 2.

JSP 2: This page displays the result of the search in a table. It has an edit button to edit the contact. If edit button is clicked, it goes to your JSP 3.

JSP 3: This page is used for editing the contact. It provides 2 input elements, one for name and one for number. When form is submitted, it goes to your JSP 4.

JSP 4: This page contains logic to update the contact in the DB table. It also provides a button to return to the HOME page (presumably, that's your JSP 1).

Here are my comments on this code:

<input type="text" name="newname" scope='session'>

I don't think <input> elements have a scope attribute. Setting it to session will not even make it a session variable or stored as a session variable that is accessible across your pages (as long as session is active). It's not going to work that way.

What you can do is, in your JSP 2 use the <c:set> to create a session variable and assign it with the param values. The parameters submitted via form in your JSP 1, will be read in JSP 2 and stored into session variables, namely sessionName and sessionNumber. See code below:

<sql:query var="result" dataSource="${data}">
    SELECT NAME, NUMBER FROM DATA WHERE NAME = '${param.name}' OR NUMBER = '${param.number}'
</sql:query>
<c:set var="sessionName" value="${param.name}" scope="session"/>
<c:set var="sessionNumber" value="${param.number}" scope="session"/>

Now, in your JSP 4, these session variables can be read. See code below:

<sql:update var="updatevariable" dataSource="${data}">
    UPDATE DATA
    SET NAME = '${param.newname}'
    WHERE NAME = '${sessionName}'
</sql:update> 
<sql:update var="updatevariable2" dataSource="${data}">
    UPDATE DATA
    SET NUMBER = '${param.newnumber}'
    WHERE NUMBER = '${sessionNumber}'
</sql:update>

Now, if you no longer need the session variables you can just remove them by using <c:remove>. See sample below:

<c:remove var="sessionName" scope="session"/>
<c:remove var="sessionNumber" scope="session"/>

This code can be placed in your JSP 1, when you are starting over a new search.

Just a side comment: It's not ideal that you are executing SQL code right directly into your JSP code. You should have this encapsulated in a Java class - implement MVC pattern.

Upvotes: 1

Related Questions