Tomcat + JSP classic configuration instance (which can be found later)

Source: Internet
Author: User
Tags apache 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.

I. Development Environment Configuration

Step 1: Download j2sdk and tomcat: To the sun official site (http://java.sun.com/j2se/1.5.0/download.jsp) download j2sdk, pay attention to download the version of Windows offline installation SDK, at the same time it is best to download j2se 1.5.0 documentation, then go to 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: Install j2sdk and tomcat Program And then install it 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.5.0 ):

Java_home = c: \ j2sdk1.5.0
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 servlet. Jar under the common \ lib directory under the Tomat installation directory (you can append it according to the actual situation) 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 ("<HTML> <body> 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. Restart 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 = 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 we need to do next is to read more books and read more others. Code To enhance your development capabilities in this area.

JVM should be filled in
C: \ j2sdk \ bin

A simple configuration ::::

JSP environment configuration experience

First of all, we can use JDK + Tomcat to configure our JSP server. There are many Article I introduced Apache, but it is not necessary at all. Generally, it is sufficient to study and debug tomcat.

After JDK is installed, Tomcat will automatically find the JDK installation path before installation, and click "Next" all the way. After a period of file copying, and finally "close", complete comcat installation.

You 'd better download a tomcat version with a higher version, such as 4.1 or above, because it does not need to set too many system variables, right-click "my computer ", select "property"-> "advanced"-> "environment variable"-> "system variable" to create a tomcat_home, and set the value to the path of your tomcat, for example, D: \ Program Files \ apache group \ Tomcat 5.5. The configuration is complete.

Find the Tomcat option from the Start Menu. The general opening sequence is start> program> Apache Tomcat 5.5. Select Start tomcat to start running the JSP server, in this case, a DOS-like window is opened, and some related information is displayed.
Related Article

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.