Servlet Learning Tutorial (iii)----A simple servlet example

Source: Internet
Author: User

Let's use the simplest servlet example to illustrate the servlet simple configuration and the way the Servlet class implements the class.

First, we create a new dynamic Web Project, named Servlet650) this.width=650; "Src=" http://s5.51cto.com/wyfs02/M02/85/54/ Wkiom1egn83bcu3oaabv2qz6_h0900.png "title=" Qq20160802135849.png "alt=" Wkiom1egn83bcu3oaabv2qz6_h0900.png "/>

Click Next to set the default output folder to Servlet/webcontent/web-inf/classes

Second, create a package named Servlet, and then create a class called the Welcomeservlet class. (The servlet class certainly lacks a servlet container, please note that your development software has integrated servlet containers, such as Tomcat)

Note: Adding the Tomcat class library to the Servlet project

1. Right click on the item you need to add the jar package to.
2. Select "Properties".
3. Then select "Java Build Path" in the dialog box.
4. Select the "Libraries" tab, then click on the "Add Library" on the right.
5. Select "Choose Server Runtime"
6. Select the Tomcat server you have added.
5. Click "Finish".

Third, write the servlet class that implements the business logic, that is, Welcomeservlet.

package servlet;import java.io.ioexception;import java.io.printwriter;import  Java.util.date;import javax.servlet.servletexception;import javax.servlet.http.httpservlet;import  javax.servlet.http.httpservletrequest;import javax.servlet.http.httpservletresponse;//in practice 99.9999% The servlet classes are inherited httpservletpublic class welcomeservlet extends httpservlet{             //actually 99.9% is covered with the doget () or Dopost () method, The servlet gets a reference to the request and response object from this method. @Overridepublic  void doget (HTTPSERVLETREQUEST REQ, HTTPSERVLETRESPONSE RESP) throws  servletexception, ioexception {//gets the value of the page input box name named Username string name=req.getparameter (" Username ");         //in a servlet from the response object fetched from the container, you can get PrintWriter, You can use PrintWriter to output HTML text to a response object or to a picture. Printwriter writer=resp.getwriter ();D ate date=new date (); Writer.println ("Welcome to  SerVlet, "+name+"!,now time is  "+date);}} 

Small trick: Many people do not remember doget () or Dopost () method parameters, when we only need to play doget or dopost in the class, and then alt+/, we can automatically associate the whole of this method.

Finally, we are writing the necessary Web. Xml. Where <servlet-name> is called the deployment name and is used to bind <servlet> with <servlet-mapping>. <servlet-class> represents the fully qualified name of the Servlet class that we need to execute the business logic, that is, the package name + class name. <url-pattern> the servlet name that the customer sees or is fictitious represents a servlet.

The actual function of the <servlet> tag is to map the internal name to the actual class,<servlet-mapping> function is to forward to which <servlet> when the request URL arrives.

<?xml version= "1.0" encoding= "UTF-8"? ><web-app id= "servlet" version= "2.4" xmlns= "http://java.sun.com/xml/ Ns/j2ee "xmlns:xsi=" http://www.w3.org/2001/XMLSchema-instance "xsi:schemalocation=" http://java.sun.com/xml/ns/ Java EE http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd "><servlet><servlet-name>welcomeservlet</ servlet-name><servlet-class>servlet.welcomeservlet</servlet-class></servlet>< servlet-mapping><servlet-name>welcomeservlet</servlet-name><url-pattern>/welcome</ Url-pattern></servlet-mapping></web-app>

Five, a simple HTML page. Place the page under WebContent.

Finally, put the project into Tomcat, launch access to http://localhost:8080/Servlet/input.html, and then output the English name submission.

Note: Here we are just simple configuration, lack of garbled processing, error pages, security processing, initialization configuration and so on.







This article is from the "see-learn-use-say-get" blog, please be sure to keep this source http://zangyanan.blog.51cto.com/11610700/1833583

Servlet Learning Tutorial (iii)----A simple servlet example

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.