Hello world of JSP Servlet

Source: Internet
Author: User
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?

Related Article

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.