By: aathishankaran in JSP Tutorials on 2007年02月14日 [フレーム]
In a production environment, multiple programmers may be developing servlets for the same server. So, placing all the servlets in the top-level servlet directory results in a massive hard-to-manage directory and risks name conflicts when two developers accidentally choose the same servlet name. Packages are the natural solution to this problem. Using packages results in changes in the way the servlets are created, the way that they are compiled, and the way they are invoked.
HelloWWW.java
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class HelloWWW extends HttpServlet {
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String docType =
"\n";
out.println(docType +
"<HTML>\n" +
"<HEAD><TITLE>Hello WWW</TITLE></HEAD>\n" +
"<BODY>\n" +
"<H1>Hello WWW</H1>\n" +
"</BODY></HTML>");
}
}
Let us take these areas one at a time in the following three subsections. The first two changes are exactly the same as with any other Java class that uses packages; there is nothing specific to servlets.
Two steps are needed to place servlets in packages:
There are two main ways to compile classes that are in packages. The first option is to place your package subdirectory right in the directory where the Web server expects servlets to go. Then, you would set the CLASSPATH variable to point to the directory above the one actually containing your servlets, that is, to the main servlet directory used by the Web server. You can then compile normally from within the package-specific subdirectory. For example, if your base servlet directory is C:\JavaWebServer2.0\servlets and your package name (and thus subdirectory name) is javasamples, and you are running Windows, you would do:
DOS> set CLASSPATH=C:\JavaWebServer2.0\servlets;%CLASSPATH%
DOS> cd C:\JavaWebServer2.0\servlets\javasamples
DOS> javac HelloWorld.java
The first part, setting the CLASSPATH, you probably want to do permanently, rather than each time you start a new DOS window. On Windows 95/98 you typically put the
set CLASSPATH=... statement in your autoexec.bat file
Somewhere after the line that sets the CLASSPATH to point to servlet.jar and the JSP JAR file
HelloWWW2.java
package javasamples;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class HelloWWW2 extends HttpServlet {
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String docType =
"\n";
out.println(docType +
"<HTML>\n" +
"<HEAD><TITLE>Hello WWW</TITLE></HEAD>\n" +
"<BODY>\n" +
"<H1>Hello WWW</H1>\n" +
"</BODY></HTML>");
}
}
On Windows, you go to the Start menu, select Settings, select Control Panel, select System, select Environment, then enter the variable and value. On Unix (C shell), you set the CLASSPATH variable by
setenv CLASSPATH /install_dir/servlets:$CLASSPATH
Put this in your .cshrc file to make it permanent. If your package were of the form name1.name2.name3 rather than simply name1 as here, the CLASSPATH should still point to the top-level servlet directory, that is, the directory containing name1. A second way to compile classes that are in packages is to keep the source code in a location distinct from the class files. First, you put your package directories in any location you find convenient. The CLASSPATH refers to this location.
Second, you use the -d option of javac to install the class files in the directory the Web server expects. An example follows. Again, you will probably want to set the CLASSPATH permanently rather than set it each time.
DOS> cd C:\MyServlets\javasamples
DOS> set CLASSPATH=C:\MyServlets;%CLASSPATH%
DOS> javac -d C:\tomcat\webpages\WEB-INF\classes HelloWWW2.java
Keeping the source code separate from the class files is the approach I use for my own development. To complicate my life further, I have a number of different CLASSPATH settings that I use for different projects, and typically use JDK 1.2, not JDK 1.1 as the Java Web Server expects. So, on Windows I find it convenient to automate the servlet compilation process with a batch file servletc.bat, as shown in (line breaks in the set CLASSPATH line inserted only for readability). I put this batch file in C:\Windows\Command or somewhere else in the Windows PATH. After this, to compile the HelloWWW2 servlet and install it with the Java Web Server, I merely go to C:\MyServlets\javasamples and do “servletc
http://host/servlet/packageName.ServletName instead of http://host/servlet/ServletName
Thus, if the Web server is running on the local system,
http://localhost/servlet/javasamples.HelloWWW2
This policy contains information about your privacy. By posting, you are declaring that you understand this policy:
This policy is subject to change at any time and without notice.
These terms and conditions contain rules about posting comments. By submitting a comment, you are declaring that you agree with these rules:
Failure to comply with these rules may result in being banned from submitting further comments.
These terms and conditions are subject to change at any time and without notice.
Most Viewed Articles (in JSP )
Show a calendar for user input in JSP
Encrypting Passwords in Tomcat using Servlets
JSP Tags for SQL to connect to a database
Steps to get a Free SSL certificate for your Tomcat
Uploading a file to a server using JSP
Server Side Programming using JSP
Latest Articles (in JSP)
Show a calendar for user input in JSP
Steps to get a Free SSL certificate for your Tomcat
Encrypting Passwords in Tomcat using Servlets
JSP Tags for SQL to connect to a database
Uploading a file to a server using JSP
Uploading an Image to a Database using JSP
A JSP page that gets properties from a bean
Scriptlets and Expressions in JSP
The taglib, tag, include, attribute and the variable Directive in JSP
Show a calendar for user input in JSP
Encrypting Passwords in Tomcat using Servlets
Steps to get a Free SSL certificate for your Tomcat
JSP Tags for SQL to connect to a database
Uploading an Image to a Database using JSP
Uploading a file to a server using JSP
© 2023 Java-samples.com
Tutorial Archive: Data Science React Native Android AJAX ASP.net C C++ C# Cocoa Cloud Computing EJB Errors Java Certification Interview iPhone Javascript JSF JSP Java Beans J2ME JDBC Linux Mac OS X MySQL Perl PHP Python Ruby SAP VB.net EJB Struts Trends WebServices XML Office 365 Hibernate
Latest Tutorials on: Data Science React Native Android AJAX ASP.net C Cocoa C++ C# EJB Errors Java Certification Interview iPhone Javascript JSF JSP Java Beans J2ME JDBC Linux Mac OS X MySQL Perl PHP Python Ruby SAP VB.net EJB Struts Cloud Computing WebServices XML Office 365 Hibernate