Configure servlet Ko for Tomcat

Source: Internet
Author: User
Tomcat configuration servlet novice Ko (a) Environment Settings atatand jdktomcat.exe download: http://www.apache.org/dist/jakarta/tomcat-4/v4.1.31/bin/jakarta-tomcat-4.1.31.exe
Currently, Tomcat is free of installation and can be directly decompressed,
JDK download: http://java.sun.com/j2se/1.4.2/download.html
You do not need to download the JRE environment variable settings. For more information, see http://blog.csdn.net/wulongtiantang/article/details/8815864. bat command, and then enter http: // localhost: 8080 in the address bar of the browser. If you see the Tomcat big cat, it indicates that you have successfully installed it.

(2) Servlet
After completing the above steps, you still need to make some modifications to run your servlet. You need to modify two files (Web. XML, server. XML) respectively)
(1) first of all to do a good job directory problem: Find the tomcat installation directory of webapps (my c: \ Software \ tomcat6.0.16 \ webapps), create MyApp \ WEB-INF \ classes, are folders, WEB-INF capital. This classes directory is used to store your compiled. Class (that is, the servelt file you want to run)
My path is: C: \ Software \ tomcat6.0.16 \ webapps \ MyApp \ WEB-INF \ Classes

(2) Write A helloworld. Java and compile javac.
Helloworld. Java; the Code is as follows:
Import java. Io .*;
Import javax. servlet .*;
Import javax. servlet. http .*;

Public class helloworld extends httpservlet {

Public void doget (httpservletrequest request, httpservletresponse response)
Throws ioexception, servletexception
{
Response. setcontenttype ("text/html ");
Printwriter out = response. getwriter ();
Out. println ("<HTML> ");
Out. println ("Out. println ("<title> Hello world! </Title> ");
Out. println ("Out. println ("<body> ");
Out. println ("Out. println ("</body> ");
Out. println ("}
} If a compilation error occurs, use eclipse to create a new project compilation, and then add external jars to libraries in the configuration of the Project Build path to import the \ Lib \ servlet-api.jar under the tomcat installation directory, then go to the project directory bin to generate. copy the class file to the created above .. \ MyApp \ WEB-INF \ Classes \ directory

(3) (to modify the Web. xml file)
Copy the Web. xml file under the conf directory under the Tomcat directory to the directory of the new .. \ MyApp \ WEB-INF, open it with notepad, and add the following content:

The code added is as follows: (The helloworld is the name of the. Class file just compiled. You can modify it as needed)

<? 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> welcome to Tomcat </display-Name>
<Description>
Welcome to Tomcat
</Description>
<Servlet>
<Servlet-Name> helloworld </servlet-Name>
<Servlet-class> helloworld </servlet-class>
</Servlet>
<Servlet-mapping>
<Servlet-Name> helloworld </servlet-Name>
<URL-pattern>/helloworld </url-pattern>
</Servlet-mapping>
</Web-app>

(4) (modify the server. xml file) --- skip this step
Use NotePad to open the server. xml file under the Tomcat directory \ conf
Find the following content:
<Context Path = "/Examples" docbase = "Examples" DEBUG = "0"
Reloadable = "true" crosscontext = "true">

Modify as follows:
<Context Path = "/MyApp" docbase = "MyApp" DEBUG = "0"
Reloadable = "true" crosscontext = "true">

/MyApp refers to the new working directory.
Save the changes.

(3)
The next step is to run servelt to pull and start Tomcat (startup. BAT), open the browser, enter http: // localhost: 8080, add the working directory name MyApp, And the Servlet File Name (that is, compiled. class file)
Full path: http: // localhost: 8080/MyApp/helloworld
Hello world! Easy to use

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.