1. Compile the servlet class.
This is just a simple servlet. in actual application, it may need to be complicated.Code.
The above is a simple implementation of using eclipse to generate classes. This class only overrides the doget method of httpservlet. The Code is as follows:
/** <Br/> */<br/> package COM. jefferyxu; <br/> Import Java. io. ioexception; <br/> Import Java. io. printwriter; <br/> Import javax. servlet. servletexception; <br/> Import javax. servlet. HTTP. httpservlet; <br/> Import javax. servlet. HTTP. httpservletrequest; <br/> Import javax. servlet. HTTP. httpservletresponse; </P> <p>/** <br/> * @ author jefferyxu <br/> */<br/> public class helloworldservlet extend S httpservlet {<br/>/* (non-javadoc) <br/> * @ see javax. servlet. HTTP. httpservlet # doget (javax. servlet. HTTP. httpservletrequest, javax. servlet. HTTP. httpservletresponse) <br/> */<br/> @ override <br/> protected void doget (httpservletrequest req, httpservletresponse resp) <br/> throws servletexception, ioexception {<br/> // todo auto-generated method stub </P> <p> printwriter writer = resp. getwriter (); <br/> Writer. Print ("Hello world! "); <Br/>}< br/>/** <br/> */<br/> Private Static final long serialversionuid = 1l; <br/>/** <br/> * @ Param ARGs <br/> */<br/> Public static void main (string [] ARGs) {<br/> // todo auto-generated method stub <br/>}< br/>
2. Modify the Web. xml file.
<? XML version = "1.0" encoding = "UTF-8"?> <Br/> <web-app version = "2.5" xmlns = "http://java.sun.com/xml/ns/javaee" <br/> 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_2_5.xsd"> <br/> <servlet-Name> helloworldservlet </servlet-Name> <br/> <servlet-class> com. jefferyxu. helloworldservlet </servlet-class> <br/> </servlet> <br/> <servlet-mapping> <br/> <servlet-Name> helloworldservlet </servlet-Name> <br/> <URL-pattern>/helloworldservlet. action </url-pattern> <br/> </servlet-mapping> <br/> <welcome-file-List> <br/> <welcome-File> index. JSP </welcome-File> <br/> </welcome-file-List> <br/> <login-config> <br/> <auth-method> basic </ auth-method> <br/> </login-config> <br/> </Web-app> <br/>
3. Test the application Program .
Deploy the entire web program to Tomcat, start the server, and access the servlet: http: // localhost: 8080/helloworldservlet through the following connection, A friendly hello world should be displayed in the browser.
4. issues left over.
In web. XML, what are the URL-pattern rules?