Step 1: Download j2sdk and tomcat: Go to the sun official website ([url]http://java.sun.com/j2se/1.4.2/download.html[/url]) to download j2sdk. Note that the download version is Windows Offline Installation SDK, it is best to download J2SE 1.4.2 Documentation, and then go to the tomcat official site ([url]http://www.apache.org/dist/jakarta/tomcat-4/[/url]) to download tomcat (download The latest 4.1.x version of tomcat);
Step 2: Install and configure your j2sdk and tomcat: execute the installation program of j2sdk and tomcat, and then install according to the default settings.
1. After installing j2sdk, you need to configure the environment variables. Add the following environment variables in My Computer->Properties->Advanced->Environment Variables->System Variables (assuming that your j2sdk is installed in c:j2sdk1.4.2):
JAVA_HOME=c:j2sdk1.4.2
classpath=.;%JAVA_HOME%libdt.jar;%JAVA_HOME%libtools.jar; (.; must not be less, because it represents the current path)
path=%JAVA_HOME% bin
Then you can write a simple java program to test whether the J2SDK has been installed successfully:
public class Test{
public static void main(String args[]){
System.out.println("This is a test program.");
}
}
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
javac Test.java
java Test
. If you see This is a test program. printed out at this time, the installation is successful. If it does not print out After saying this, you need to carefully check your configuration.
2. After installing Tomcat, add the following environment variables in My Computer->Properties->Advanced->Environment Variables->System Variables (assuming your tomcat is installed in c:tomcat):
CATALINA_HOME=c:tomcat;
CATALINA_BASE =c:tomcat;
Then modify the classpath in the environment variable, and append the servlet.jar under commonlib in the tomat installation directory to the classpath. The modified classpath is as follows:
classpath=.;%JAVA_HOME%lib dt.jar;%JAVA_HOME%libtools.jar;%CATALINA_HOME%commonlibservlet.jar;
Then you can start tomcat and visit [url]http://localhost:8080[/url] in IE. If you see the tomcat welcome page, the installation is successful.
Step 3: Create your own jsp app directory
1. Go to the webapps directory of Tomcat's installation directory. You can see ROOT, examples, tomcat-docs and other directories that come with Tomcat;
2. Create a new directory under the webapps directory. Name it myapp;
3. Create a new directory WEB-INF under myapp. Note that the directory name is case-sensitive;
4. Create a new file web.xml under WEB-INF with the following content:
<?xml version="1.0" encoding="ISO-8859-1"?> <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "[url]http://java.sun.com/dtd/web-app_2_3.dtd[/url]"> <web-app> <display-name>My Web Application</display-name> <description> A application for test. </description> </web-app> |
5. Create a new test jsp page under myapp. The file name is index.jsp. The file content is as follows:
<html><body><center> Now time is: <%=new java.util.Date()%> </center></body></html> |
6. Restart Tomcat
7. Open the browser and enter [url]http://localhost:8080/myapp/index.jsp[/url]. If you see the current time, it means success.
Step 4: Create your own Servlet:
1. Use the editor you are most familiar with (it is recommended to use java ide with syntax check) to create a new servlet program. The file name is Test.java, and the file content is as follows:
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("<html><body><h1>This is a servlet test.</h1></body></html>"); out.flush(); } } |
2. Compile
and place Test.java under c:test, use the following command to compile:
C:Test>javac Test.java
and then a compiled servlet file will be generated under c:Test: Test.class
3. Cut the structure testTest.class to %CATALINA_HOME%webappsmyappWEB-INFclasses, that is, cut the test directory to the classes directory. If the classes directory does not exist, create a new one. Now there is a file directory structure of testTest.class under webappsmyappWEB-INFclasses.
4. Modify webappsmyappWEB-INFweb.xml and add servlet and servlet-mapping.
The edited web.xml is as follows, with the added content in red:
<?xml version="1.0" encoding="ISO-8859-1"?> <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "[url]http://java.sun.com/dtd/web-app_2_3.dtd[/url]"> <display-name>My Web Application</display-name> <description> A application for test. </description> <servlet> <servlet-name>Test</servlet-name> <display-name>Test</display-name> <description>A test Servlet</description> <servlet-class>test.Test</servlet-class> </servlet> <servlet-mapping> <servlet-name>Test</servlet-name> <url-pattern>/Test</url-pattern> </servlet-mapping> </web-app> |
The servlet section in this paragraph declares the Servlet you want to call, and servlet-mapping "maps" the declared servlet to the address/Test
5. Okay, start Tomcat, start the browser, and enter [url] http://localhost:8080/myapp/Test[/url] If you see the output This is a servlet test., it means that the servlet written is successful.
Note: If you modify web.xml or add a new class, you must restart Tomcat.
Step 5: Create your own Bean:
1. Use your most familiar editor (it is recommended to use a java ide with syntax checking) to create a new java program. The file name is TestBean.java and the file content is as follows:
package test; public class TestBean{ private String name = null; public TestBean(String strName_p){ this.name=strName_p; } public void setName(String strName_p){ this.name=strName_p; } public String getName(){ return this.name; } } |
2. Compile
and place TestBean.java under c:test, use the following command to compile:
C:Test>javac TestBean.java
and then a compiled bean file will be generated under c:Test: TestBean.class
3. Cut the TestBean.class file to %CATALINA_HOME%webappsmyappWEB-INFclassestest.
4. Create a new TestBean.jsp file with the following contents:
<%@ page import="test.TestBean" %> <html><body><center> <% TestBean testBean=new TestBean("This is a test java bean."); %> Java bean name is: <%=testBean.getName()%> </center></body></html> |
5. Okay, restart Tomcat, start the browser, enter [url]http://localhost:8080/myapp/TestBean.jsp[/url] If you see the output Java bean name is: This is a test java bean. This indicates that the Bean written was successful.
This completes the configuration of jsp, servlet and javabean under Tomcat. The next thing you need to do is to read more books, read more good code from others, and write more code yourself to enhance your development capabilities in this area.