Servlet Learning (1)

Source: Internet
Author: User

Servlet Learning (1)

1. Servlet is a technology provided by sun for developing dynamic web resources.

2. Servlet location in web applications:


3. Create a Servlet in three ways:
(1) Implement the servlet Interface

(2) InheritanceGenericServlet, override service method

(3) inherit from HttpServle and overwrite doGet () or doPost () methods.

Note:

1>GenericServletImplementedServletAndServletConfigInterface.GenericServletCan be expanded directly by a servlet, although it is more general to expand a subclass of a specified protocol, suchHttpServlet.

GenericServletThis makes writing to servlets easier. It provides a simple life cycle methodinitAndDestroy and methods in the ServletConfig Interface.GenericServletAlso implementedlogMethod, inServletContextThe API is declared.

2> provides an abstract class for creating HTTP servlrt for a WEB site.HttpServletMust override at least one method. Usually one of the following methods:

    doGetIf the servlet supports http get requests doPostFor http post requests doPutFor http put requests doDeleteFor http delete requests initAnd destroyTo manage resources locked for the existence of the servlet. getServletInfoServlet is used to provide information about the device itself. 4. Do not use IDE To create Servlet:
    (1) create a web application mail in the tomcat/webapps/directory.
    The directory structure is as follows:
    Tomcat/webapps
    "
    Mail
    |
    WEB-INF
    |
    Web. xml classes
    |
    Cn. wwh. www. web. servlet. MyServlet. class


    (2) Write a class MyServlet In the tomcat/webapps/mail/directory to implement the Servlet interface.
    (3) enter the cmd command station, add the servlet-api.jar [in tomcat] Support
    Set classpath = % classpath %; D: \ apache-tomcat-6.0.29 \ lib \ servlet-api.jar (import this jar package)
    (4) Go to the directory where MyServlet. java is located and compile MyServlet.
    Javac-d. MyServlet. java (Compilation)
    (5) Send a mail application to the tomcat/webapps/directory and start Tomcat.
    (6) You must configure the MyServletWeb Dynamic Resources in the web. xml file.

    MyServlet (Generally, the name of the write class is good)
    cn.wwh.www.web.servlet.MyServlet (Permission class name)


    MyServlet
    /MyServlet (Virtual URL)

    (7) access the dynamic Web Resource MyServlet through a browser
    IE: http: // localhost: 8080/mail/MyServlet
    Note: 1> Java class files are put under the WEB-INF folder, rather than java source code 2> when writing servlet classes, you must configure them in the web. xml file, just as when you use four android components, you must register them in the manifest. xml file. 3> in the address bar of the browser, enter the virtual directory with Case sensitivity. 4> because jdk is self-contained in MyEclipse, when running tomcat, you must note that the jdk used for the java source file compiled under dos should be the same as that in tomcat. 4. Three methods for creating servlet (only write type, no focus on the purpose of code)
    1. CreateServlet1.java ):
    Package cn. wwh. www. web. servlet;


    Import java. io. IOException;
    Import java. io. PrintWriter;


    Import javax. servlet. Servlet;
    Import javax. servlet. ServletConfig;
    Import javax. servlet. ServletException;
    Import javax. servlet. ServletRequest;
    Import javax. servlet. ServletResponse;


    /**
    * Category:
    *
    *
    * @ Author
    * @ Version 1.0
    * @ Creation Time: 10:57:35 AM
    */
    Public class CreateServlet1 implements Servlet {

    @ Override
    Public void destroy (){

    } @ Override
    Public ServletConfig getServletConfig (){
    Return null;
    }
    @ Override
    Public String getServletInfo (){
    Return null;
    }
    @ Override
    Public void init (ServletConfig arg0) throws ServletException {

    }
    @ Override
    Public void service (ServletRequest request, ServletResponse response)
    Throws ServletException, IOException {
    // Obtain the output stream object from the server to the browser
    PrintWriter pw = response. getWriter ();
    Pw. write ("You must study and never give up your dream! ");

    }
    }

    (2) method 2 (CreateServlet2.java): package cn. wwh. www. web. servlet;

    Import java. io. IOException;
    Import javax. servlet. GenericServlet;
    Import javax. servlet. ServletException;
    Import javax. servlet. ServletRequest;
    Import javax. servlet. ServletResponse;


    /**
    * Category:
    *
    *
    * @ Author
    * @ Version 1.0
    * @ Creation Time: 11:12:14 AM
    */
    Public class CreateServlet2 extends GenericServlet {

    @ Override
    Public void service (ServletRequest requeset, ServletResponse response)
    Throws ServletException, IOException {
    // Sets the encoding method of browser characters, mainly for Chinese characters. If Chinese characters are not set, garbled characters will appear.
    Response. setContentType ("text/html; charset = UTF-8 ");

    Response. getWriter (). write ("Youth is used for struggle, and the ideal is used for implementation! ");
    }
    }
    (3) method 3 (CreateServlet3.java ):
    Package cn. wwh. www. web. servlet;


    Import java. io. IOException;
    Import java. io. PrintWriter;
    Import javax. servlet. ServletException;
    Import javax. servlet. http. HttpServlet;
    Import javax. servlet. http. HttpServletRequest;
    Import javax. servlet. http. HttpServletResponse;


    /**
    * Category:
    *
    *
    * @ Author
    * @ Version 1.0
    * @ Creation Time: 11:41:28 AM
    */
    Public class CreateServlet3 extends HttpServlet {

    // The browser submits the request in Get mode, so it is better to override the doGet method.
    Public void doGet (HttpServletRequest request, HttpServletResponse response)
    Throws ServletException, IOException {
    Response. setContentType ("text/html; charset = UTF-8 ");
    PrintWriter pw = response. getWriter ();
    // Pw. write (" ");
    Pw. write ("");
    Pw. write ("by Ye Binzhou ");
    Pw. write ("");
    Pw. write ("
    ");
    Pw. write ("

    Major: Software Engineering

    ");
    // Pw. write ("
    Pw. close ();
    }
    }


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.