First step: Create a Directory
Create a new folder Firstservlet, and then build two folders under the Firstservlet directory, respectively: Web-inf and SRC. Finally, build a classes folder underneath the Web-inf.
Step Two: Write Sevlet
Under SRC, add a file Helloservlet.java. The code is as follows:
1 PackageCom.firstservlet;2 3 Importjavax.servlet.*;4 Importjavax.servlet.http.*;5 ImportJava.io.*;6 7 Public classHelloServletextendsHttpServlet8 {9 Public voidService (ServletRequest Req,servletresponse resp)throwsservletexception,ioexception{TenResp.getwriter (). Write ("Hello Servlet"); One } A}
Step three: Compile the Helloservlet.java file
After compiling, the COM folder will appear under the SRC directory and the folder will be moved to the Web-ini>classes directory.
Fourth step: Create Web. xml
Under Web-ini, create a Web. xml file that reads as follows:
1 <?XML version= "1.0" encoding= "UTF-8"?>2 <Web-appversion= "2.5" 3 xmlns= "Http://java.sun.com/xml/ns/javaee" 4 Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance" 5 xsi:schemalocation= "Http://java.sun.com/xml/ns/javaee6 http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd ">7 8 <servlet>9 <Servlet-name>HelloServlet</Servlet-name>Ten <Servlet-class>Com.firstServlet.HelloServlet</Servlet-class> One </servlet> A <servlet-mapping> - <Servlet-name>HelloServlet</Servlet-name> - <Url-pattern>*.do</Url-pattern> the </servlet-mapping> - </Web-app>
Fifth Step: Deploy
1. Delete src directory
2. Copy the Firstservlet to Tomcat
3. Access Http://localhost:8080/FirstServlet/HelloServlet.do through the browser
Write a servlet project in Notepad