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
Joined: Jun 15, 2004
Posts: 12
posted
0
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
Joined: Feb 18, 2004
Posts: 51
posted
0
package com.javaranch; // it is declaring only ?
import java.io.*; // it is calling only ?
wwhat is the difference ?
Leo Deegan
Greenhorn
Joined: Jun 15, 2004
Posts: 12
posted
0
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
Joined: Apr 26, 2004
Posts: 16
posted
0
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();