How to configure web.xml to run servlet in Tomcat5.0

xiaoxiao2021-03-06  101

1. Use the editor you are most familiar with (it is recommended to use the Java IDE with syntax check), the file name is 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 ("

this is a servlet test. ");

Out.flush ();

}

}

2. Compile Test.java Under C: / Test, compile: C: / test> Javac test.java then generate a compiled servlet file under C: / Test: Test.class

3. Cut the structure Test / Test.class to% catalina_home% / WebApps / myApp / web-inf / class, that is, cut that Test directory to the class content, if the class content does not exist, will create a new one. Now WebApps / MyApp / Web-INF / CLASSES has Test / Test.class file directory structure

4. Modify WebAPPS / MyApp / Web-INF / Web.xml, add servlet and servlet-maping edited Web.xml as follows: MY Web Application a application for test. test test A Test servlet test.test test / TEST This paragraph declares that the servlet you want to call, and servlet-mapping is the declared servlet "mapping" to address / Test 5. Ok, start Tomcat, start the browser, enter http: // localhost: 8080 / myapp / test If you see the output this is a servlet test. The written servlet is successful.

转载请注明原文地址:https://www.9cbs.com/read-77276.html

New Post(0)