Windows tomcat configuration overview [Details]

Source: Internet
Author: User

Configuration of JSP, Servlet, and JavaBean environments in Tomcat
I often see jsp beginners asking how to configure jsp, servlet, and bean in tomcat. So I summarized how to configure jsp, servlet, and ben in tomcat, hoping to help beginners.
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 Windows Offline Installation SDK, at the same time it is best to download J2SE 1.4.2 Documentation, then go to the tomcat official site (http://www.apache.org/dist/jakarta/tomcat-4/) to download tomcat (download the latest version of tomcat 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 installing j2sdk, You need to configure the environment variables, choose my computer> Properties> advanced> environment variables> system variables to add the following environment variables (assuming 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 small, because it represents the current path)
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 (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 This is a test program is printed, the installation is successful. If This is not printed, You need to carefully check your configuration.
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 ):
CATALINA_HOME = c: \ tomcat;
CATALINA_BASE = c: \ tomcat;
Modify the classpath in the environment variable and append the servlet. jar under the common \ lib under the tomat installation directory to the classpath. The modified classpath is as follows:
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.
Step 3: Create your own jsp app directory
1. Go to the webapps directory of the Tomcat installation directory, and you can see the tomcat built-in directories such as ROOT, examples, and Tomcat-docs;
2. Create a directory named myapp under the webapps directory;
3. Create a directory WEB-INF under myapp, note that the directory name is case sensitive;
4. Create a file web. xml under the 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 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> 6. Restart Tomcat
7. Open the browser and enter http: // localhost: 8080/myapp/index. jsp to view the current time.
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 servlet program named Test. java. 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 ("Out. flush ();
}
}
2. Compile
Place Test. java under c: \ test and compile it with the following command:
C: \ Test> javac Test. java
Then a compiled servlet File: Test. class will be generated under c: \ Test.
3. structure test \ Test. class cut to % CATALINA_HOME % \ webapps \ myapp \ WEB-INF \ classes, that is, cut the test directory to the classes directory, if the classes directory does not exist, create a new one. Now webapps \ myapp \ WEB-INF \ classes has the file directory structure test \ Test. class
4. Modify webapps \ myapp \ WEB-INF \ web. xml, add servlet and servlet-mapping
The edited web. xml is shown as follows, and red indicates 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>
In this section, servlet declares the Servlet you want to call, while servlet-mapping maps the declared servlet to the address/Test.
5. Start Tomcat, start the browser, and enter http: // localhost: 8080/myapp/Test. If This is a servlet test, the servlet is successfully written.
Note: You must restart Tomcat after modifying web. xml and adding a new class.
Step 4: Create your own Bean:
1. Use the editor you are most familiar with (we recommend using java ide with syntax check) to create a java program named 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
Place TestBean. java under c: \ test and compile it with the following command:
C: \ Test> javac TestBean. java
Then a compiled bean file TestBean. class will be generated under c: \ Test.
3. Cut the TestBean. class file to % CATALINA_HOME % \ webapps \ myapp \ WEB-INF \ classes \ test,
4. Create a New TestBean. jsp file with the following content:
<% @ 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> 5. restart Tomcat, start the browser, and enter http: // localhost: 8080/myapp/TestBean. jsp if the output Java bean name is: This is a test java bean. it indicates that the Bean is successfully written.
In this way, the configuration of jsp, servlet, and javabean in Tomcat is completed. What needs to be done next is to read more books, read more good code from others, and write more code by yourself.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.