I often see jsp beginners asking how to configure jsp, servlet and bean under tomcat, so I summarized how to configure jsp, servlet and ben under tomcat, hoping to be helpful to those beginners.
1. Development environment configuration Step 1: Download j2sdk and tomcat: Go to the sun official website ( http://java.sun.com/j2se/1.5.0/download.jsp ) to download j2sdk. Note that the downloaded version is Windows Offline Installation. SDK, and it is best to download J2SE 1.5.0 Documentation, and then go to the tomcat official site ( http://jakarta.apache.org/site/downloads/downloads_tomcat-5.cgi ) to download tomcat (download the latest 5.5.9 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.5.0):
JAVA_HOME=c:j2sdk1.5.0
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 a command prompt window, cd to the directory where your Test.java is located, and then type the following command
javacTest.java
java Test
At this time, if you see This is a test program. printed out, it means the installation is successful. If this sentence is not printed out, 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 (can be added according to the actual situation) under commonlib in the tomat installation directory to the classpath. The modified classpath is as follows:
classpath=.;%JAVA_HOME%libdt.jar;%JAVA_HOME%libtools.jar;%CATALINA_HOME%commonlibservlet.jar;
Then you can start tomcat and visit http://localhost:8080 in IE. If you see the welcome page of tomcat, the installation is successful.
Step 3: Create your own jsp app directory 1. Go to the webapps directory of Tomcat’s installation directory, and you can see Tomcat’s own directories such as ROOT, examples, tomcat-docs;
2. Create a new directory under the webapps directory and 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"
" http://java.sun.com/dtd/web-app_2_3.dtd ">
<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 http://localhost:8080/myapp/index.jsp. 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 and use the following command to compile:
C:Test>javac Test.java
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 the 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"
" http://java.sun.com/dtd/web-app_2_3.dtd ">
<web-app>
<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, restart Tomcat and start In the browser, enter http://localhost:8080/myapp/Test . If you see the output This is a servlet test., it means that the written servlet is successful.
Note: If you modify web.xml and add a new class, you must restart Tomcat.
Step 4: Create your own Bean:
1. Use the editor you are most familiar with (it is recommended to use a java ide with syntax checking) to create a new java program. The file name is TestBean.java. 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, and use the following command to compile:
C:Test>javac TestBean.java
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 http://localhost:8080/myapp/TestBean.jsp. If you see the output Java bean name is: This is a test java bean., it means you wrote it. Bean succeeded.
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.