Calling methods from within another class.

  • Thread starter James via JavaKB.com
  • Start date
J

James via JavaKB.com

Hi,

I'm setting up two servlet classes:

issueOptions() (in issueOptions.java) and menuUI() (in menuUI.java).

Both are in the same package: menu.

menuUI has a method inside called issueOverview().


I want to call and execute the issueOverview() method from the issueOptions
class, but how do I do this? I have been trying to do by creating an
instance of the menuUI class, but I cannot be doing correctly. I have
posted the following code (which I hope is useful) and the error produced
upon compiling:

menuUI.java
~~~~~~~~~~~
package menu;

import java.io.*;
import java.util.*;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.DateFormat.*;
import java.util.Date.*;

public class menuUI extends HttpServlet {
//Initialize global variables
.....
......
public void init(ServletConfig config) throws ServletException {
super.init(config);
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
connection = DriverManager.getConnection(URL, "", "");
....
....
....
}
}

public void doPost(HttpServletRequest request, HttpServletResponse
response) throws
ServletException, IOException {

....
....
}

public void issueOverview(HttpServletRequest
request,HttpServletResponse response, String issueNo)
throws ServletException, IOException {
....
....
}

}


issueOptions.java
~~~~~~~~~~~~~~~~~
package menu;

import java.io.*;
import java.util.*;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class issueOptions extends HttpServlet {
//Initialize global variables
private Connection connection = null;
private Statement statement = null;
private String query = null;
private int result;
private ResultSet rs = null;
private String URL = "jdbc:eek:dbc:myDB";
private menuUI theMenu;


public issueOptions(){
theMenu = new menuUI();
}

public void init(ServletConfig config) throws ServletException {
super.init(config);

try {
......
........
}

public void doPost(HttpServletRequest request, HttpServletResponse
response) throws
ServletException, IOException {
........
........
}

public void addComment(HttpServletRequest request,
HttpServletResponse response, String issueNo, String raisedBy, String
commentTitle, String commentDescription) throws ServletException,
IOException {
....
.....
......
if (success = false) {
out.println("<head><title>Comment Details NOT
Submitted</title></head>");
out.println("<body>");
out.println("An error has occurred!");
out.println("<br>The details could not be submitted!");
out.println("<pre><a href=/ILS/addComment.html>Try Again</a></pre>");
out.println("</body>");
out.println("</html>");
out.close();
}

else {
theMenu.issueOverview(request, response, issueNo);
}
}

}


JAVAC ERRORS
~~~~~~~~~~~~
issueOptions.java:22: cannot find symbol
symbol : class menuUI
location: class menu.issueOptions
private menuUI theMenu;
^
issueOptions.java:26: cannot find symbol
symbol : class menuUI
location: class menu.issueOptions
theMenu = new menuUI();
^
2 errors
 
W

Wendy Smoak

James via JavaKB.com said:
issueOptions.java
~~~~~~~~~~~~~~~~~
package menu;
public class issueOptions extends HttpServlet {
//Initialize global variables
private Connection connection = null;
private Statement statement = null;
...

I just saw this part. This is a very bad idea and it's going to cause
problems as soon as you have more than one request coming in at the same
time. (The connection and statement will more than likely be shared across
multiple threads.)
 
J

James via JavaKB.com

Hi,

thanks for the response.

Which part of the code is going to cause problems??

I didn't think I would have to be using multiple threads in a servlet
application?

Besides this problem, can someone show me how to reference the method from
the second class, within the first class?

Many thanks.
 
W

Wendy Smoak

Which part of the code is going to cause problems??

The part where the Servlet has instance (global) variables (if you don't
synchronize access to them).
I didn't think I would have to be using multiple threads in a servlet
application?

See if this helps:
http://www.unix.org.ua/orelly/java-ent/servlet/ch03_01.htm
Besides this problem, can someone show me how to reference the method from
the second class, within the first class?

Did you read the other response?
 
J

James via JavaKB.com

OKay, so I localise the variables to each method, or create threads for
them.

Now, how do I fix the problem of referencing a method from another class?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,053
Latest member
BrodieSola

Latest Threads

Top