• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

making/calling a package

 
salvador rcn
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
package com.javaranch; // does it making a package or calling a package ? but calling needs // import.

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class PostServlet extends HttpServlet
{
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<html>\n <body>\n");
out.println(" <h2>\n Hello World\n </h2>\n");
out.println(" </body>\n</html>\n");
out.flush();
out.close();
}
}
 
Leo Deegan
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Salvador -

The package declaration indicates the package location of your class. It does not execute a call to the package (in fact, I don't believe packages are executable). In your example, you do not need to import the package com.javaranch.

--Leo
 
salvador rcn
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
package com.javaranch; // it is declaring only ?

import java.io.*; // it is calling only ?

wwhat is the difference ?
 
Leo Deegan
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The package line says that your PostServlet class is in the com.javaranch package, which means it is in the subdirectory com/javaranch relative to your source root directory.

If you have the import java.io.* line, you are indicating that your class can use the java.io package classes without fully qualifying their names. If you notice, your class can use a PrintWriter without having to call it java.io.PrintWriter each time you use the PrintWriter name.

When you declare a package, you are not calling the package. When you import the set of classes in a package (e.g., import java.io.*), you are also not making a call.
 
Jeff Pavlocak
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The package statement allows you to group several classes and interfaces together into a "package". You can then reference the classes you created in that package in other java programs by importing the package.

Example:

package com.myNewPackage;

import com.javaranch.*;

public class myNewClass
{
PostServlet ps = new PostServlet();

// more code...
}
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic