• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

reading data from jsp page to another jsp page using request.getParameter

 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello everyone. I've got a problem and hopefully one one you will be able to help me with that.
I am publishing a database on the intranet of a company. Things seemed to work fine until I run into this problem.
I am putting some of data into table then the end user has to click on a link to view the details on a particular data. So from one page(jsp), by cliking on a link, another page is supposed to come up with the details of that project say. But I am suprised to see that it's not reading my project id.
Here is how my code looks like. THis is the page that contains the list of projects with a link on the project id

And the code for the page with the project details look like this:

This part
project = request.getParameter("projectNum");
System.out.println("Here it is:"+project);
reads null. I don't know how to read from a jsp page to another jsp page.
Please heeeeeelp
added code tags to somewhat improve the code formatting
[ January 29, 2004: Message edited by: Frank Carver ]
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your code would be a lot easier to read had you used the UBB code tags to preserve its formatting.
So out of all that, what is the URL that you are using to link to the second page? In order for the projectNum parameter to be available to the second page it must be specified in the query string of the URL (or submitted as part of a form...)
 
Ranch Hand
Posts: 405
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Wow, you have a lot going on there for just 2 jsp pages. First off, I would suggest that you encapsulate all of your SQL statements and database logic into a DAO(data access object) or helper bean, it just makes the jsp page a little more readable and maintainable. But at the very least I wouldn't declare my variables using

<%!

, if I am not mistaken these are instance variables, which can cause you problems when multiple request hit your page.
Now to answer your question :

But I am suprised to see that it's not reading my project id.


The reason is that you are not storing the projectId in the request object. The simpliest solution without changing too much of your code would be to re-define your hyperlink to:
You might want to check on the exact syntax.
But in your second jsp page you can access the projectNum by using the request.getParameter("id").
I hope this helps.
Craig.
 
orelia hans
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks for your help. I guess that was close but it didn't work. The value of id when I use
out.println("<a td href='ProjectDetails.jsp?id=projectNum'>"+projectNum+"</td>");
is actually the string "projectNum". It doesn't take its value. I even used ProjectDetails.jsp?id=valueOf(projectNum) but then the value of id is "valueOf(projectNum). I really have no idea how it can pick up the actual value of the project number, read that string from tha table.
I will come back to the comment on how long are my jsp pages code but I need to have this link working first.
Please heeeeeeelp.
 
Sheriff
Posts: 7001
6
Eclipse IDE Python C++ Debian Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The value of id when I use
out.println("<a td href='ProjectDetails.jsp?id=projectNum'>"+projectNum+"</td>");

Just a little quotes problem. It should be:
out.println("<a td href='ProjectDetails.jsp?id=" + projectNum + "'>"+projectNum+"</td>");
You really need to understand the kind of stuff that has been suggested here to work effectively with server-side Java. If it still seems confusing, you really need to make sure you progress in little, tiny steps, making sure that everything still works after every step. Take on too big a chunk of work and you could very easily get stuck again.
 
orelia hans
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thank you soooooo much it works now. I've been struggling with this for almost a week now. Thanks for helping me out. I will look at the linked you've sent me on java server side.
In the mean time, I've another question.
You can see that my code is really long for each jsp page. i've to make a connection for each, which makes it really long. I've tried to use servlet, have connection serlvet but it didn't work. What would you suggest me for me not to repeat that part of code every time.
Again, thanks so much
 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

have connection serlvet but it didn't work


Handling the connections outside of the JSP in this way is your best bet. There are Servlet and JDBC forums here to help you figure out how to make it work. I also strongly suggest that you look into connection pooling.
 
Frank Carver
Sheriff
Posts: 7001
6
Eclipse IDE Python C++ Debian Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To be honest, before you get into the complexities of conection pooling, you need to get used to the idea that most of the code for a web application should be in classes that are not servlets or JSPs. Whenever you see duplicated code, move it out to a separate method or a separate class.
Also, (as mentioned above) you really need to stop using <%! for variable declarations. When you do this it creates an instance variable, shared by all threads that run the code in that class. Thiswill cause you lots of complex and baffling problems later.
So, looking at your examples, a first step might be to extract the database access code from the first JSP to a new class:

This then greatly reduces the amount of work needed in the JSP:

You can take a similar approach to the second JSP. Then you can look for similarities between the database access class you have made, and remove those to shared code. Eventually your application will be smaller and simpler.
Does that make sense?
 
orelia hans
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
THanks again for paying attention to my problem. I've tried to extract that repetitive code from my jsp page and put it into a different java class but it is not compiling. I am having this message:
C:\Tomcat\Tomcat\webapps\CoreData\WEB-INF\classes\ProjectDetails.java:53: missing return statement
}
^
1 error
Here is my code:
import java.sql.* ;
class ProjectDetails
{
public ProjectDetails()
{
try
{
// ensure the JDBC driver class is loaded
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}
catch (ClassNotFoundException cnfe)
{ cnfe.printStackTrace();
}
}
public String execute(String st, int e)
{
String ret = "unknown";
try
{
String sql = "SELECT * FROM Projects WHERE Status = '" +st+"' and EntryType='"+e+"' ";
Connection con = DriverManager.getConnection("jdbc dbc:AcessCore");
Statement s = con.createStatement();
ResultSet rs = s.executeQuery(sql);
while (rs.next())
{
String projectNum = rs.getString(1);
String subject = rs.getString(2);
String title = rs.getString(10);
System.out.println("<TR>");
/* out.println("<th>");
out.println("<a td href='ProjectDetails.jsp'>"+projectNum+"</td>");
out.println("<td>"+subject+"</td>");
out.println("<td>"+title+"</td>");
out.println( "</TR>" );*/
ret = projectNum;
}
rs.close();
s.close();
con.close();
return ret;
}
catch (SQLException sqle)
{
sqle.printStackTrace();
}
}
}
 
Craig Jackson
Ranch Hand
Posts: 405
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Your code would be a lot easier to read had you used the UBB code tags to preserve its formatting.


The problem is that you need to move the statement
from where it is now to outside of your try/catch block.

Craig
 
Frank Carver
Sheriff
Posts: 7001
6
Eclipse IDE Python C++ Debian Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Craig. I knew I'd probably get something wrong, but I just didn't have time to test that suggested code
 
orelia hans
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Craig and Frank.
It's not working .
And that's why I used so much repetitive code on my jsp cause I tried to have java classes but for some reasons, can't read those classes from my jsp pages.
Here is the error message I am having when I run my jsp:
org.apache.jasper.JasperException: Unable to compile class for JSP
An error occurred at line: -1 in the jsp file: null
Generated servlet error:
[javac] Compiling 1 source file
C:\Tomcat\Tomcat\work\Standalone\localhost\CoreData\Project_jsp.java:10: cannot resolve symbol
symbol : class ProjectDetails
location: class org.apache.jsp.Project_jsp
ProjectDetails projectDetails = new ProjectDetails();
^
C:\Tomcat\Tomcat\work\Standalone\localhost\CoreData\Project_jsp.java:10: cannot resolve symbol
symbol : class ProjectDetails
location: class org.apache.jsp.Project_jsp
ProjectDetails projectDetails = new ProjectDetails();
^
2 errors

My classpath look like this:
CLASSPATH=.;C:\j2sdk1.4.2\bin;.;%CATALINA_HOME%/webapps/CoreData/web-inf/classes;. %CATALINA_HOME%/webapps/coredata;. C:/tomcat/tomcat/webapps/coredata/web-inf/classes/Broker;.%CATALINA_HOME%/common/lib/servlet.jar;.;;%JAVA_HOME%\jre\lib\rt.jar;.;%JAVA_HOME%\lib\tools.jar;.;
And ProjectDetails.java is in the subfolder classes.
And for the links you gave me, it's not free. Can't afford
 
Frank Carver
Sheriff
Posts: 7001
6
Eclipse IDE Python C++ Debian Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This may be a silly question, but have you actually compiled your Java classes before you deployed them?
It is the responsibility of the servler to compile the JSP, but it is your responsibility to compile any regular Java classes that you use. First you need to compile ProjectsDetails.java, then place the compiled class file in WEB-INF/classes.
Does that make sense?
 
orelia hans
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
of course I've compiled my java class before I can run the jsp page. And the class file is in the web-inf as well.
 
Frank Carver
Sheriff
Posts: 7001
6
Eclipse IDE Python C++ Debian Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry if that upset you. I just wanted to make sure.
In general, the external CLASSPATH shouldn't make any difference to a web application. All servlet/JSP containers that I'm aware of create their own classloaders based on the servlet directory structure.
In this case the problem might be that the "helper" class is not in a package. There are all sorts of tricky possibilities when you try and reference a class that's not in a package from one that is, and servlet containers often get confused in this situation.
Can you try moving your helper class to a package (e.g. 'helper'), recompiling, and putting the compiled class file in WEB-INF/classes/helper. You will also need to add <%@page import='helper.ProjectDetails'%> to your JSP.
If we keep trying, I'm sure we can sort this out !
 
orelia hans
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is now the error I am getting:
Generated servlet error:
[javac] Compiling 1 source file
C:\Tomcat\Tomcat\work\Standalone\localhost\CoreData\Project_jsp.java:7:
cannot access Helper.ProjectDetails
bad class file: C:\Tomcat\Tomcat\webapps\CoreData\WEB-INF\classes\Helper\ProjectDetails.class
class file contains wrong class: ProjectDetails
Please remove or make sure it appears in the correct subdirectory of the classpath.
import Helper.ProjectDetails.*;
^
Please, I really want this to work. Help me.
1 error

 
Frank Carver
Sheriff
Posts: 7001
6
Eclipse IDE Python C++ Debian Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OK, you are nearly there. Look at the "wrong class" error message, it's telling you that the file Helper/ProjectDetails contains the wrong class - it should contain Helper.ProjectDetails, but actually contains just ProjectDetails.
You need to tell Java that the class in the file is part of a package. Add the following line at the start of your projectDetails.java:

And just to avoid possible future problems, please be aware that all of these things are case sensitive. If you have named your package "Helper", you need to make sure that everything (the directory name, the package statement in the java file, the <%@ page import %> in the JSP ...) all use Helper, and not helper or HELPER, or any other combination.
So, does it work yet?
[ February 02, 2004: Message edited by: Frank Carver ]
 
orelia hans
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Frank . It works. But my page is empty.
I know I am close, it just that the out.println gives me a error message.

C:\Tomcat\Tomcat\webapps\CoreData\WEB-INF\classes\Helper\ProjectDetails.java:34: cannot resolve symbol
symbol : variable out
location: class Helper.ProjectDetails
out.println("<th>");
^
C:\Tomcat\Tomcat\webapps\CoreData\WEB-INF\classes\Helper\ProjectDetails.java:35: cannot resolve symbol
symbol : variable out
location: class Helper.ProjectDetails
out.println("<a td href='ProjectDetails.jsp'>"+projectNum+"</td>");

^
I was using out.println to print into my page but now the page is empty and when i compile my code ProjectDetails.java, the error message above comes up.

execute method in ProjectDetails.java:
public String execute(String st, int e)
{
String ret = "unknown";
try
{
String sql = "SELECT * FROM Projects WHERE Status = '" +st+"' and EntryType='"+e+"' ";
Connection con = DriverManager.getConnection("jdbc dbc:AcessCore");
Statement s = con.createStatement();
ResultSet rs = s.executeQuery(sql);
while (rs.next())
{
String projectNum = rs.getString(1);
String subject = rs.getString(2);
String title = rs.getString(10);
out.println("<th>");
out.println("<a td href='ProjectDetails.jsp'>"+projectNum+"</td>");
out.println("<td>"+subject+"</td>");
out.println("<td>"+title+"</td>");
System.out.println(title);
out.println( "</TR>" );
ret = projectNum;
}
rs.close();
s.close();
con.close();
}

I believe you can help me with that.
I have so much trustfor you now. THanks
 
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oh my God!
The out variable makes sense en the jsp page, is the JSPWriter used to make the response. This variable doesn't exist in your class.
You can replace the out.println in your class, return a String for the method and use that String in the JSP.
(I think you should read carefully the error messages before posting... )
 
Frank Carver
Sheriff
Posts: 7001
6
Eclipse IDE Python C++ Debian Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks,
This one is fairly simple to solve, too.
"out" is one of the JSP "implicit variables", provided to your JSP by the container. These "implicit variables" are not provided to regular Java code, so you need to pass it in:
Change your method declaration to:

and change where it is used, to pass in the implicit "out" variable:

[/code]
 
orelia hans
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Changed my code this morning. Everything was working fine.My web site was fine, my code all nice and clean. :roll: . Then tried to behave smart by renaming ProjectDetails.java to CoreDataDetails.java everywhere, everything compiled fine. But when I went back again to my web page and run the code :
error occurred at line: 23 in the jsp file: /Project.jsp
Generated servlet error:
[javac] Compiling 1 source file
C:\Tomcat\Tomcat\work\Standalone\localhost\CoreData\Project_jsp.java:75: non-static method execute(java.lang.String,int,java.io.Writer) cannot be referenced from a static context
String projectNum = CoreDataDetails.execute("1C", 1,out);
^
1 error

It was working before I changed the name. But now this is what I am getting. I wanted to use only one java class, that's why i wanted to have a name that made more sens and applied to everything.
I am sure this is my last problem on this issue. Please help.
 
orelia hans
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is my code CoreDataDetails.java code:
package Helper;
import java.sql.* ;
import java.io.*;
public class CoreDataDetails
{
public CoreDataDetails()
{
try
{
// ensure the JDBC driver class is loaded
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}
catch (ClassNotFoundException cnfe)
{ cnfe.printStackTrace();
}
}
public String execute(String st, int e, Writer out)
{
String ret = "unknown";
try
{
String sql = "SELECT * FROM Projects WHERE Status = '" +st+"' and EntryType='"+e+"' ";
Connection con = DriverManager.getConnection("jdbc dbc:AcessCore");
Statement s = con.createStatement();
ResultSet rs = s.executeQuery(sql);
while (rs.next())
{
String projectNum = rs.getString(1);
String subject = rs.getString(2);
String title = rs.getString(10);
out.write("<th>");
out.write("<a td href='ProjectDetails.jsp?id=" + projectNum + "'>"+projectNum+"</td>");
out.write("<td>"+subject+"</td>");
out.write("<td>"+title+"</td>");
System.out.println(title);
out.write( "</TR>" );
ret = projectNum;
}
rs.close();
s.close();
con.close();
}
catch (SQLException sqle)
{
sqle.printStackTrace();
}
catch(IOException eio)
{
}
return ret;
}
public String execute1(int st, int e, Writer out)
{
String et = "unknown";
try
{
String sql = "SELECT * FROM Projects WHERE ProjectSubject = '" +st+"' and EntryType='"+e+"' ";
Connection con = DriverManager.getConnection("jdbc dbc:AcessCore");
Statement s = con.createStatement();
ResultSet rs = s.executeQuery(sql);
while (rs.next())
{
String projectNum = rs.getString(1);
String subject = rs.getString(2);
String title = rs.getString(10);
out.write("<th>");
out.write("<a td href='ProjectDetails.jsp?id=" + projectNum + "'>"+projectNum+"</td>");
out.write("<td>"+title+"</td>");
System.out.println(title);
out.write( "</TR>" );
et = projectNum;
}
rs.close();
s.close();
con.close();
}
catch (SQLException sqle)
{
sqle.printStackTrace();
}
catch(IOException eio)
{
}
return et;
}
public String execute2(int st, Writer out)
{
String et = "unknown";
try
{
String sql = "SELECT * FROM NepruPublications WHERE PublicationTypeID = '" +st+"'";
Connection con = DriverManager.getConnection("jdbc dbc:AcessCore");
Statement s = con.createStatement();
ResultSet rs = s.executeQuery(sql);
while (rs.next())
{
//read data from database
String publicationNum=rs.getString(1);
String subject=rs.getString(2);
String authors=rs.getString(3);
String YearPublished=rs.getString(5);
out.write("<TR>");
out.write("<th>");
out.write("<a tdhref='ProjectDetails.jsp'>"+publicationNum+"</td>");
out.write("<td>"+subject+"</td>");
out.write("<td>"+authors+"</td>");
out.write("<td>"+YearPublished+"</td>");
out.write( "</TR>" );
et = publicationNum;
}
rs.close();
s.close();
con.close();
}
catch (SQLException sqle)
{
sqle.printStackTrace();
}
catch(IOException eio)
{
}
return et;
}
}
Only one line changed on my jsp page for examplw:
</tr><% String publicationNum = CoreDataDetails.execute2(1,out);
Sorry for insisting for much :roll:
 
Frank Carver
Sheriff
Posts: 7001
6
Eclipse IDE Python C++ Debian Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As I pointed aout a few mesages back, you have to be really careful about the difference between upper and lower case.
In the original example, ProductDetails is the class name, and productDetails is the object name. They are quite different things.
Look again at your error message: String projectNum = CoreDataDetails.execute("1C", 1,out);
Can you see that this is all initial-capitalised? The error message is telling us that this is the name of your class, not the name of an object. Unfortunately, you haven't shown the bit of the JSP which creates the object, so I don't know what you have called it now.
Either way, the solution is to make sure your JSP code uses the name of the actual object.
And in the future, don't use a replace tool which ignores the case of the letters.
 
orelia hans
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Frank. You've been really patient and helpfull.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic