Often see the JSP beginners ask Tomcat How to configure the JSP, servlet and bean problems, so summed up how Tomcat under the configuration JSP, servlet and Ben, hope for those beginners to help.
Step one: Download J2sdk and tomcat: Download J2SDK to the Sun's official site (http://java.sun.com/j2se/1.4.2/download.html), and note that the download version is Windows Offline Installation SDK, and preferably download j2se 1.4.2 documentation, then go to the Tomcat official site (http://www.apache.org/dist/jakarta/tomcat-4/ Download tomcat (Download the latest 4.1.x version of Tomcat);
Step Two: Install and configure your J2SDK and Tomcat: Perform J2SDK and tomcat setup, and then install by default.
1. After installing the J2SDK, you need to configure the environment variables, add the following environment variables to the system variable, environment variable, properties----let's assume your j2sdk is installed in c:\j2sdk1.4.2:
java_home=c:\j2sdk1.4.2
Classpath=.; %java_home%\lib\dt.jar;%java_home%\lib\tools.jar; (.; Must not be less because it represents the current path)
Path=%java_home%\bin
You can then write a simple Java program to test whether the J2SDK has been successfully installed:
public class test{
public static void Main (String args[]) {
System.out.println ("This was a test program.");
}
}
Save the above program as a file named Test.java.
Then open a command Prompt window, CD to your Test.java directory, and type the following command
Javac Test.java
Java Test
At this point, if you see the print out of this is a test program. The installation is successful, if you do not print out this sentence, you need to carefully check your configuration situation.
2. After installing Tomcat, add the following environment variable (assuming your Tomcat is installed on the C:\tomcat) in the system variable, advanced environment variable, properties, My Computer
Catalina_home=c:\tomcat;
Catalina_base=c:\tomcat;
Then modify the environment variables in the CLASSPATH, the Tomat installation directory under the Common\lib under the Servlet.jar appended to the CLASSPATH, the modified classpath as follows:
Classpath=.; %java_home%\lib\dt.jar;%java_home%\lib\tools.jar;%catalina_home%\common\lib\servlet.jar;
You can then start Tomcat, Access http://localhost:8080 in IE, and if you see the Tomcat Welcome page, the installation is successful.
Step three: Build your own JSP app directory
1. To the WebApps directory of the Tomcat installation directory, you can see Root,examples, tomcat-docs such as the directory of Tomcat;
2. Create a new directory under the WebApps directory named MyApp;
3.myapp Create a new directory Web-inf, note that the directory name is case-sensitive;
4.web-inf Create a new file, Web. XML 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 with the file name index.jsp and the file content as follows:
Now time is: <%=new java.util.Date ()%>
</center></body>
6. Restart Tomcat
7. Open the browser, enter http://localhost:8080/myapp/index.jsp to see the current time, the description is successful.
Fourth step: Build Your own servlet:
1. Create a new servlet program with your most familiar editor (recommended Java IDE with Syntax checker), with the file name Test.java and the file contents 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 ("
Out.flush ();
}
}
2. Compiling
Place Test.java under C:\Test and compile with the following command:
C:\test>javac Test.java
A compiled servlet file is then generated under C:\Test: Test.class
3. Cut the structure Test\test.class to%catalina_home%\webapps\myapp\web-inf\classes, that is, cut the Test directory into the classes directory, if the classes directory does not exist, Just create a new one. Now there's a Test\test.class file directory structure under Webapps\myapp\web-inf\classes.
4. Modify Webapps\myapp\web-inf\web.xml, add Servlets and servlet-mapping
The edited web. XML is shown below, red for the added 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>
<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 in this passage declares the servlet you want to invoke, while servlet-mapping is "mapping" the declared servlet to the address/test
5. OK, start Tomcat, start the browser, enter Http://localhost:8080/myapp/Test if you see the output of this is a servlet Test. The servlet written is successful.
Note: Modify the Web. XML and the new class to restart Tomcat
Fourth step: Build Your own bean:
1. Create a new Java program with your most familiar editor (recommended Java IDE with Syntax checker), with the file name Testbean.java and the file contents 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. Compiling
Place Testbean.java under C:\Test and compile with the following command:
C:\test>javac Testbean.java
A compiled bean file is then generated under C:\Test: Testbean.class
3. Cut the Testbean.class file to%catalina_home%\webapps\myapp\web-inf\classes\test,&NBSP;
4. Create a new testbean.jsp file with the file content: <%@ page import= "test. Testbean "%>&NBSP;
&NBSP;
<%&NBSP;
Testbean testBean=new Testbean ("This is a test Java bean."); &NBSP;
%>&NBSP;
Java Bean name is: <%=testbean.getname ()%>&NBSP;
</center></body>< /html>&NBSP;
5. Ok, restart Tomcat, launch the browser, enter http://localhost:8080/myapp/ testbean.jsp If you see the output Java Bean name Is:this is a test Java bean. That means the bean you wrote was successful. &NBSP;
This completes the configuration of the JSP, Servlet, and JavaBean under Tomcat as a whole. The next thing to do is to read more books, read more people's good code, self-write code
Articles you may be interested in:
- Basic configuration methods and overview of Apache and Tomcat server integration
- Apache Tomcat a website multi-domain name implementation method
- WIN2003 on the APACHE2+IIS6+TOMCAT5 of multi-site perfect Configuration Chapter
- Apache Load Balancing Setup Method Mod_proxy usage Introduction
- How to install and implement Apache load Balancer
- Apache2.2 version and Tomcat integration configuration and load balancing implementation
Configuration of the JSP, servlet, and JavaBean environments under Tomcat