Implement a servlet's HelloWorld applet (not applicable to eclipse)
1. Build an application under WebApps in Tomcat Firstservlet (C:\tomcat\webapps\FirstServletweb)
2. Create a directory under the application
C:\tomcat\webapps\FirstServletweb\WEB-INF\classes
3. Create a file in the class directory Firstservlet.java
4. Implementing content
Package Cn.school; Import java.io.*; Import javax.servlet.*; Extendsthrows Servletexception, java.io.IOException {outputstream out = res.getoutputstream (); Out.write ("Hello". GetBytes ());}}
5. Compiling the servlet program
The problem occurs because the imported import javax.servlet.*; This package cannot be found.
The reason for this is that this is the time to start Javac, can only find the J2SE under the API, and can not find the API under the Java EE
6. Add the jar containing the servlet to the Classpath
The Tomcat server can run Tomcat, so it must have this jar package inside.
Directory C:\tomcat\lib There is a tomcat-api.jar below
So
The jar package is successfully imported
7. Recompile, success
A folder with more than one package in the classes directory
8. Configure an external access path for the servlet
configuring in the Web. xml file
Create a new Web. xml file under Web-inf
<?XML version= "1.0" encoding= "Iso-8859-1"?><Web-appxmlns= "Http://java.sun.com/xml/ns/javaee"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"Xsi:schemalocation= "Http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"Version= "3.0"><Servlet><Servlet-name>firstservlet</Servlet-name><Servlet-class>cn.school.firstservlet</Servlet-class></Servlet> <> < servlet-name>firstservlet </servlet-name > <url-pattern</url-pattern> </servlet-mapping ></web-app< Span style= "color: #0000ff;" >>
Configuring Servlet and Servlet mappings
9. Start the tomcat server and have access to the
Http://localhost:8080/FirstServletWeb/FirstServlet