Java EE Specification--servlet example

Source: Internet
Author: User

Before learning the specification of the Java EE the servlet (Java EE specification--servlet), it is from the macro understanding of the servlet, know what he is used for? The servlet is now used in the project to solve the problem of how to use it. Use an example to summarize how to use it.

It takes three steps to create a new servlet program in MyEclipse. 1. The foreground display code. 2. inherit HttpServlet. Rewrite doget or dopost.3. Register the servlet in Web. Xml.


1. Foreground Display Code

  <body>    


2. inherit the HttpServlet. rewrite doget or dopost.

Inherited from Httpservletpublic class HelloServlet extends HttpServlet {@Overrideprotected void doget (httpservletrequest Request, HttpServletResponse response) throws Servletexception, IOException {//TODO auto-generated method STUBSYSTEM.OUT.PRINTLN ("Processing Get Request"); PrintWriter out=response.getwriter (); Out.println ("Hello servlet");} @Overrideprotected void DoPost (HttpServletRequest request, httpservletresponse response) throws Servletexception, IOException {//TODO auto-generated method Stub//super.dopost (req, resp);}}


3. Register the servlet in Web. Xml.

<servlet>  <servlet-name>HelloServlet</servlet-name>  <servlet-class>servlet. helloservlet</servlet-class>  </servlet>  <servlet-mapping>  <servlet-name> helloservlet</servlet-name>  <url-pattern>/servlet/HelloServlet</url-pattern>  </ Servlet-mapping>
The servlet class that needs to be accessed is entered in the Servlet-class node. Url-pattern This node specifies the URL to access this class.


Summarize

An instance of the starter servlet, re - doget or Dopost,is the main way for the servlet to connect the server side to the client. In addition, MyEclipse also integrates the new Servlet application, which can be new->servlet directly in the MyEclipse, which makes it easier to develop.

Java EE Specification--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.