Servlet (iii) HttpServlet

Source: Internet
Author: User

Create a servlet with HttpServlet

1, in Firstservlet the same directory under the establishment of Hservlet.java


Package Com.hunhun;import javax.servlet.*;import javax.servlet.http.*;import java.io.*;import java.util.*;p ublic Class Hservlet extends Httpservlet{<span style= "White-space:pre" ></span>protected void DoGet ( HttpServletRequest req,                     httpservletresponse Res P)               throws servletexception,            &NBSP ;        IOException{         resp.getwriter (). println ("Doget" + Req.getparameter ("username"));   }        protected void DoPost ( HttpServletRequest req,                      HttpServletResponse re SP)                throws servletexception,          &NBSP ;           ioexception{         resP.getwriter (). println ("DoPost" +req.getparameter ("username"));   }} 

2. Web. xml

<?xml version= "1.0" encoding= "iso-8859-1"? ><web-app xmlns= "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 "  metadata-complete=" true ">    <servlet>        <servlet-name>FirstServlet</servlet-name>        <servlet-class> com.hunhun.firstservlet</servlet-class>    </servlet>    <servlet-mapping>         <servlet-name>FirstServlet</servlet-name>        < url-pattern>/firstservlet</url-pattern>    </servlet-mapping>    <servlet >        <servlet-name>GServlet</servlet-name>        <servlet-class>com.hunhun.GServlet</servlet-class>    </servlet>    <servlet-mapping>        <servlet-name>GServlet</servlet-name>        <url-pattern>/GServlet</url-pattern>    </servlet-mapping>    <servlet>        <servlet-name>HServlet</servlet-name>        <servlet-class>com.hunhun.HServlet</servlet-class>    </servlet>    <servlet-mapping>        <servlet-name>HServlet</servlet-name>        <url-pattern>/HServlet</url-pattern>    </servlet-mapping></web-app >

3, compile files,reload MyWeb project, Note that each time you re-modify the *servlet.java file, you need to recompile and reload the MyWeb project or restart TOMCAT7

4, browser access to Http://localhost:8080/myweb/HServlet?username=hunhun, you can see

Dogethunhun
5. Build login.html file in MyWeb directory

<! DOCTYPE html>

Doposthunhun




Servlet (iii) HttpServlet

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.