Step 1: Download j2sdk and tomcat: To the sun official site (http://java.sun.com/j2se/1.4.2/download.html) download j2sdk, pay attention to download the version of windowsofflineinstallation SDK, at the same time it is best to download j2se1.4.2documentation, and then to the Tomcat official site (http://www.apache.org/dist/jakarta/tomcat-4) download Tomcat (download Tomcat of the latest version 4.1.x );
Step 2: install and configure your j2sdk and tomcat: Execute the j2sdk and tomcat installation programs and install them according to the default settings.
1. After j2sdk is installed, You need to configure the environment variables and add the following environment variables to my computer> Properties> advanced> environment variables> system variables.
(Assume that your j2sdk is installed in C:/j2sdk1.4.2 ):
(1) java_home = C:/j2sdk1.4.2
(2) classpath =.; % java_home %/lib/dt. jar; % java_home %/lib/tools. jar; (.; must not be small, because it represents the current path)
(3) Path = % java_home %/bin
Then, you can write a simple Java program to test whether j2sdk has been installed successfully:
Public class test {
Public static void main (stringargs []) {
System. Out. println ("thisatestprogram."); // Note: System S should be capitalized because no package is required.
}
}
Save the above program as a file named test. java.
Then open the Command Prompt window, CD to the directory where your test. Java is located, and then type the following command
Javactest. Java (Compilation)
Javatest (execution)
If this is a test program is printed, the installation is successful. If this is not printed, You need to carefully check your configuration.
------------------------------------------------------------------
<----- How to create your own directory under Tomcat's webapp ----->
2. after Tomcat is installed, add the following environment variables to my computer> Properties> advanced> environment variables> system variables (assuming your Tomcat is installed in C:/tomcat ):
(1) catalina_home = C:/tomcat;
(2) catalina_base = C:/tomcat;
Modify the classpath in the environment variable and append the servlet. Jar under the Common/lib directory under the Tomat installation directory to the classpath. The modified classpath is as follows:
(3) classpath =.; % java_home %/lib/dt. jar; % java_home %/lib/tools. jar; % catalina_home %/common/lib/servlet. jar;
Start Tomcat and access http: // localhost: 8080 in IE. If you see the welcome page of Tomcat, the installation is successful.
Create a directory
1. Create a MyApp directory under webapp and an index. jsp directory
<% @ Page contenttype = "text/html; charset = GBK" %>
<% @ Page import = "Java. util. Date" %>
<HTML>
<Body>
Now time is: <% = new date () %>
</Body>
</Html>
Create a folder named WEB-INF under webapp and create a file named Web. xml in the WEB-INF Folder:
<? XML version = "1.0" encoding = "ISO-8859-1"?>
<! Doctype web-app
Public "-// Sun Microsystems, Inc. // DTD web application 2.3 // en"
Http://java.sun.com/dtd/web-app_2_3.dtd>
<Web-app>
</Web-app>
2. Edit the-server. xml file in the conf folder under the Tomcat directory.
Join:
<Context Path = "/MyApp" docbase = "MyApp" DEBUG = "0" reloadable = "true"/>
3 http: localhost: 8086/MyApp to view the current time
-----------------------------------------------------------------
<------- Create your own servlet ----->
(1)
D: Create a folder test and create a test. Java folder in notepad.
Package test;
Import java. Io. ioexception;
Import java. Io. printwriter;
Import javax. servlet. servletexception;
Import javax. servlet. http. httpservlet;
Import javax. servlet. http. httpservletrequest;
Import javax. servlet. http. httpservletresponse;
Public class test extends httpservlet {
Protected void doget (httpservletrequest request, httpservletresponse response)
Throws servletexception, ioexception
{
Printwriter out = response. getwriter ();
Out. println ("this is a servlet test ");
Out. Flush ();
}
}
(2)
D:/test> javac test. Java under cmd, a test. Class file is generated under the D:/test directory.
Copy the test directory to the classes under MyApp (if you do not have a new one)
(3)
Rewrite the Web. xml of the WEB-INF under the MyApp directory
<? XML version = "1.0" encoding = "ISO-8859-1"?>
<! Doctype web-app
Public "-// Sun Microsystems, Inc. // DTD web application 2.3 // en"
Http://java.sun.com/dtd/web-app_2_3.dtd>
<Web-app>
<Servlet>
<Servlet-Name> A test </servlet-Name>
<Servlet-class> Test. Test </servlet-class>
</Servlet>
<Servlet-mapping>
<Servlet-Name> A test </servlet-Name>
<URL-pattern>/test </url-pattern>
</Servlet-mapping>
</Web-app>
Note: sevelet-name must be consistent.
Servlet-class must be clear: package name. Java file name
(4)start startup.exe http: // localhost: 8086/MyApp/test. Note that the/test under MyApp corresponds to/test in Web. xml.
<----- Create a JavaBean by yourself ---->
(1) create a testbean. Java under D:/test
Package test;
Public class testbean
{
Private string name = NULL;
Public testbean ()
{}
Public void setname (string strname_p)
{
This. Name = strname_p;
}
Public String getname ()
{
Return this. Name;
}
}
(2) cmd compile D:/test> javac testbean. Java
The testbean. Class file is displayed under D:/test.
Copy it to webapps -- MyApp -- WEB-INF -- classes
(3)
Create testbean. jsp under webapps-MyApp
<% @ Page contenttype = "text/html; charset = GBK" %>
<% @ Page import = "test. testbean" %>
<HTML>
<JSP: usebean id = "A" Scope = "request" class = "test. testbean"/>
<Body>
<JSP: setproperty name = "A" property = "name" value = "Good Morning Tang huitian"/>
This is: <JSP: getproperty name = "A" property = "name"/>
</Body>
</Html>
(4)start startup.exe
HTTP: /localhost: 8086/MyApp/testbean. jsp
This is: Tang huitian Good morning
Bytes -----------------------------------------------------------------------------------------------------------