Solved

Need variables to carry over to another included page

Posted on 2004-08-13
7
244 Views
Last Modified: 2013-11-18
About half way down a JSP, I have an include statement like this:

<jsp:include page="page2b.jsp" flush="true"/>

page2b.jsp then uses some logic that evaluates some variables from the first page.

Is there a way that I can declare and set the variables on the first page in a way that they can be read on page2.jsp?

I imagine that there are session varibales (although I am not sure how to declare them).
But is there another way that I can include the page2.jsp so that it will be read by the server as a part of the first page an accept the variables as though they were set on that page?
0
Comment
Question by:mderbin
  • 3
  • 3
7 Comments
 

Author Comment

by:mderbin
ID: 11798749
Let me rephrase and show my code:

I have the following page(page1.jsp):
*************** page1.jsp *********************
<html>
<head>
<title>Page1</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<form action="page2.jsp?link=1" method="post" name="form1" enctype="multipart/form-data">
         <input name="name2" type="text" size="25" maxlength="25"><br>
       <input type="file" name="uploadfilename" > <br>
       <input type="reset" value="Reset" name="Reset">
       <input type="submit" value="Submit" name="Submit">
</form>
</body>
</html>
***************end of page1.jsp *******************

This form calls page2.jsp, which uses oReilly.servlet to get the form data:
****************** page2.jsp *********************
<%@ page import="java.sql.*, java.util.*, oracle.sql.*,oracle.jdbc.driver.*,java.io.*" %>
<%@ page import="com.oreilly.servlet.MultipartRequest"%>
<%      
String $link = request.getParameter("link");
if($link.equals("1")){
      MultipartRequest multi = new MultipartRequest(request,"d:/WebSphere/AppServer/installedApps/globalis.ear/globalis.war/META-INF/uploads/images", 50 * 1024, new com.oreilly.servlet.multipart.DefaultFileRenamePolicy());
      out.println("<br><b>Parameters</b><br>");
      Enumeration params = multi.getParameterNames();
      while (params.hasMoreElements()) {
            String $parm_name = (String)params.nextElement();
            String $parm_value = multi.getParameter($parm_name);
            if($parm_value == null || $parm_value.equals("")){ $parm_value = ""; }
            out.println("<br>"+$parm_name + " = " + $parm_value);
            
            if($parm_name.equals("name2")){
                  String $name2 = $parm_value;
            }
      }
}else{
      String $through = request.getParameter("through");
      out.println("<br>through="+$through);
      out.println("<br>link="+$link);
}
%>
<% String $myzone = "page2b.jsp"; %>
<jsp:include page="<%=$myzone%>" flush="true"/>

***************end of page2.jsp *******************

The bottom of page2.jsp calls the simple include page2b.jsp, which I want to print to the screen the variable "name2"
***************end of page1.jsp *******************
<%@ page import="java.sql.*, java.util.*, oracle.sql.*,oracle.jdbc.driver.*,java.io.*" %>
<%@ page import="com.oreilly.servlet.MultipartRequest"%>
<%
String $name2 = request.getParameter("name2");
out.println("<br>The text in name2="+$name2);
out.println("<br><br><a href=page2.jsp?link=1&through=michael>page2 click here</a>");
out.println("<br><a href=page1.jsp>page1 click here</a>");
%>
***************end of page2b.jsp *******************


But "name2" always comes out null.
I cannot use oReilly.servlet to read in the parts of the form again in page2b.jsp (it always fails - probably because page2.jsp is not a form).
How can I get page2b.jsp to recognize "name2" and print the value to the screen that i type into page1.jsp?

Thanks,
-MD
0
 
LVL 92

Expert Comment

by:objects
ID: 11798813
you need to use
<%@ include file="page2b.jsp" %>

That way the file gets included at translation phase
0
 

Author Comment

by:mderbin
ID: 11800747
I tried that, but the value of the variable is still coming up null all of the time on page2b.jsp.


So now my page2.jsp looks like:

<%@ page import="java.sql.*, java.util.*, oracle.sql.*,oracle.jdbc.driver.*,java.io.*" %>
<%@ page import="com.oreilly.servlet.MultipartRequest"%>
<%      
      String $link = request.getParameter("link");
      if($link.equals("1")){
            MultipartRequest multi = new MultipartRequest(request,"d:/WebSphere/AppServer/installedApps/globalis.ear/globalis.war/META-INF/uploads/images", 50 * 1024, new com.oreilly.servlet.multipart.DefaultFileRenamePolicy());
            out.println("<br><b>Parameters</b><br>");
            Enumeration params = multi.getParameterNames();
            while (params.hasMoreElements()) {
                  String $parm_name = (String)params.nextElement();
                  String $parm_value = multi.getParameter($parm_name);
                  if($parm_value == null || $parm_value.equals("")){ $parm_value = ""; }
                  out.println("<br>"+$parm_name + " = " + $parm_value);
                  
                  if($parm_name.equals("name2")){
                        String $name2 = $parm_value;
                  }
            }
      }else{
            String $through = request.getParameter("through");
            out.println("<br>through="+$through);
            out.println("<br>link="+$link);
      }
%>
<%@ include file="page2b.jsp" %>



And page2b.jsp looks like this:

<%@ page import="java.sql.*, java.util.*, oracle.sql.*,oracle.jdbc.driver.*,java.io.*" %>
<%@ page import="com.oreilly.servlet.MultipartRequest"%>
<%
String $name2 = request.getParameter("name2");
out.println("<br>The text in name2="+$name2);
out.println("<br><br><a href=page2.jsp?link=1&through=michael>page2 click here</a>");
out.println("<br><a href=page1.jsp>page1 click here</a>");
%>



And the results on the web look like this:

Parameters

Submit = Submit
link = 1
name2 = kmkmkmkm
The text in name2=null

page2 click here
page1 click here

Do I need to do something on page2b.jsp to get the variable from page2.jsp (right now I am using String $name2 = request.getParameter("name2");)?

Thanks,
-MD
0
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
LVL 27

Expert Comment

by:rrz
ID: 11801441
>using String $name2 = request.getParameter("name2");
You could just remove that line from  page2b.jsp  .  
The variable will be seen in a <%@ include .


0
 
LVL 92

Accepted Solution

by:
objects earned 300 total points
ID: 11802171
> Do I need to do something on page2b.jsp to get the variable from page2.jsp
> (right now I am using String $name2 = request.getParameter("name2");)?

No you don't need anything.
That line is in fact what is setting it to null.


0
 

Author Comment

by:mderbin
ID: 11802625
It worked - thanks - now I just have to get the code from this simple test environment to the REAL code.
This project is going to be the death of me...
Thanks again.
0
 
LVL 92

Expert Comment

by:objects
ID: 11805954
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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

Suggested Solutions

There are two main kinds of selectors in CSS: One is base selector like h1, h2, body, table or any existing HTML tags.  For instance, the following rule sets all paragraphs (<p> elements) to red: (CODE) CSS also allows us to define our own custom …
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

821 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