Jump to content

importing

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
4 replies to this topic

#1
rocker83

rocker83

    Newbie

  • Members
  • Pip
  • 5 posts
Hi.

I have two java files/classes ( both are servlets ) and I want to import S1 into S2. They are both in web-inf/src

class 1 is S1
class 2 is S2
---------------
But this is wrong;


import S1.*;

public class S2...

How should I write this importing?

#2
ZekeDragon

ZekeDragon

    Writes binary right handed and hex left handed

  • Moderators
  • 2,103 posts
Yeah, that is wrong. You shouldn't need to use the * symbol after you've gotten to the class. Further, when you're importing, you'll already have automatic access to any class within the same package so long as it's a public class in the same folder. If you're using importing, that means your classes S1 and S2 are in different packages, and thus in a different folder. Java uses the relative location where java was run (or javac) to determine the search directory for your files, OR the topmost directory of a .jar file (from wherever it was run). A better way to explain this would be with an example. Let's say you have a file that creates class S2, and you're attempting to import the class S1 from another folder location. Here's your file tree:
myProgram/
          - S2.java
          - /s1package/
                       - S1.java
Here's S1.java:
package s1package;

public class S1
{
    public void myMethod()
    {
        System.out.println("Example_myMethod()");
    }
}
S2.java will look like this:
import s1package.S1;

public class S2 extends S1
{
    public static void main(String[] args)
    {
        S2 m = new S2();
        m.myMethod();
    }
}

Wow I changed my sig!

#3
rocker83

rocker83

    Newbie

  • Members
  • Pip
  • 5 posts
Hi! Thanks.

Well,

S1.java will now look like this;
  
import java.io.*;
import java.text.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;

----------------------------


S2.java will now look like this;
 
package S1;

import java.io.*;
import java.text.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;

STILL WRONG??

Edited by Jaan, 14 November 2009 - 05:55 AM.
Please use code tags when you are posting your codes !


#4
rocker83

rocker83

    Newbie

  • Members
  • Pip
  • 5 posts
Forgot to say, that S1 and S2 are in a same folder! Is that a mistake?

#5
rocker83

rocker83

    Newbie

  • Members
  • Pip
  • 5 posts
Here's my whole problem.

I think here are all the necessary information, codes are quite long...


I have 3 servlets, S1, S2, S3. and a java class Data.
All these servlets, classes and build.xml are in a same folder ( WEB-INF/src/



Compiler says that S1 and S2 are ok, but S3; there are 4 errors.

And when I try ( S2 works fine ) to open url S3 a web browser says;

HTTP method GET is not supported by this URL
description The specified HTTP method is not allowed for the requested resource (HTTP method GET is not supported by this URL).



S1

import java.io.*;
import java.text.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;

                                
              
public abstract class S1 extends HttpServlet {

 public void doGet(HttpServletRequest req,
                      HttpServletResponse resp)
        throws IOException, ServletException
    {

Connection db = null;

and so on...

 protected abstract void execute(HttpServletRequest req,
                                    PrintWriter out,
                                    Connection db) throws Exception;



S2

import java.io.*;
import java.text.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;


public class S2 extends S1 {


 protected void execute(HttpServletRequest req,
                           PrintWriter out,
                           Connection db) throws Exception
    {

Connection con = null;
and so on...





S3
// I believe the problem is JUST in S3. I think that S1 and S2 are ok.
package S2;



import java.io.*;
import java.text.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;


public class S3 extends S1 {        // THIS  MUST EXTEND S1



protected void execute(HttpServletRequest req, PrintWriter out, Connection db) throws Exception {}
    
	
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {}


protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
  
and so on...






---------------------------

Compiler says 4 errors;




S3.java:15: cannot find symbol
[javac] symbol: class S1
[javac] public class S3 extends S1 {
[javac] ^

S3.java:64: cannot find symbol
[javac] symbol : variable db
[javac] location: class S2.S3
[javac] db = Data.getConnection();
[javac] ^

S3.java:64: cannot find symbol
[javac] symbol : variable Data
[javac] location: class S2.S3
[javac] db = Data.getConnection();
^

S3.java:65: cannot find symbol
[javac] symbol : variable db
[javac] location: class S2.S3
[javac] Statement stmt6 = db.createStatement();
[javac] ^
[javac] 4 errors




And I have a class Data and method getConnection is in it;


import java.sql.*;

public class Data {
public static Connection getConnection() throws Exception {
......



Please, help. Can someone solve this?

Edited by Jaan, 14 November 2009 - 05:58 AM.
Please use code tags when you are posting your codes !