AD

Servlet first example (to)

We code and servlet web.xml configuration file to describe the operating principle and life cycle. In this case we achieve by implementing the servlet interface to our code. Servlet interface, the interface must implement all the methods to achieve.

First, the code section (test.java)


import javax.servlet.*;
import java.io.*;

public class test implements Servlet {
        private int i;
        
        public test() {
        }

        /**
         *   Initialization servlet, the function will only be called once  (  When the user when the first visit to the servlet  )
         */
        public void init(ServletConfig parm1) throws ServletException {
                System.out.println("init it");
                
                i = 0;
        }

        public ServletConfig getServletConfig() {
                return null;
        }

        /**
         *  This function is used to handle business logic, the business sections of code to achieve here  (  When the user when accessing the servlet  ,  This function will be called  )
         */
        public void service(ServletRequest parm1, ServletResponse parm2)
                        throws ServletException, IOException {
                
                System.out.println("service it");

                ++i;
                
                PrintWriter pw = parm2.getWriter();
                pw.println("Hello World");
                pw.println(i);//  Values ​​from the input of the i would know  ,servlet  Is a singleton to work, regardless of which machine to access it  ,  I will increase its value  , Therefore, in multi-user concurrent access, will solve the problem of thread synchronization by the programmer to control  
        }

        public String getServletInfo() {
                return null;
        }

        /**
         *   The following two situations that call destory 1.reload  servlet(  In tomcat console  reload) 2.  Close tomcat, this is the end force  
         */
        public void destroy() {
                System.out.println("destory it");
        }
}

Second, web.xml configuration file

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="**java.sun**/xml/ns/javaee"
        xmlns:xsi="****w3**/2001/XMLSchema-instance"
        xsi:schemaLocation="**java.sun**/xml/ns/javaee 
        **java.sun**/xml/ns/javaee/web-app_2_5.xsd">


        <servlet>
                <!--  To take the name servlet  ,  Names can be arbitrary  -->
                <servlet-name>hello</servlet-name>
                <!--  The path specified in servlet  ,(  Package name + class name  )-->
                <servlet-class>test</servlet-class>
        </servlet>
        <!-- Define the hello Servlet Mapping -->
        <servlet-mapping>
                <!--  And the servlet in  servlet-name  Consistent -->
                <servlet-name>hello</servlet-name>
                <!--  This is entered in the browser to access the servlet's  url,  Is arbitrary  -->
                <!--  According to the pattern found in the browser corresponding  servlet-name,  Found according to the corresponding servlet-name  class-->
                <url-pattern>/list</url-pattern>
        </servlet-mapping>


        <welcome-file-list>
                <welcome-file>index.jsp</welcome-file>
        </welcome-file-list>
</web-app>
Third, access to servlet
1, start tomcat
2, input **127.0.0.1:8080/ServletTest/list can access the servlet

Fourth, servlet life cycle when the servlet when the first visit, init () is called, then calls service (). Then when servlet each visit, service will be called. When closing the servlet, or reload the project, destory called
标签: import java, public string, lt xml, utf 8, javax, public void, principle, configuration file, println, life cycle, hello world, public class test, void init, test java, servlet interface, getwriter, public test, destory, section test
分类: Java
时间: 2011-04-21

相关文章

  1. Servlet brief introduction

    Servlet brief introduction: Servlet is a small application server Are used to complete the B / S architecture, ...
  2. In the servlet use Bean

    According to Sun's definition, JavaBean is a reusable software components. In fact JavaBean is a Java class, t ...
  3. WebQQ, ExtJs + Servlet + Hibernate + Spring implementation

    Code for the development of boredom when using ExtJs + Servlet + hibernate (Ant + xdoclet generate HBM files) ...
  4. NoClassDefFoundError: javax / servlet / Servlet

    In the project in order to achieve a listener in web.xml set up a listener, did start in Tomcat actually occur ...
  5. servlet specification 2.2

    Introduction: Spring Security watch, when to see more use of the Servlet Listener, filter and so on. Filter do ...
  6. Pring Beanfactory at the jsp, servlet, web.xml and other configuration

    Keywords: spring beanfactory Spring in the web application called the Beanfactory 1) Configure web.xml Java co ...
  7. prototype Ajax + Servlet (attached Demo)

    Test WEB project set up prototype.js location WebContent / js / prototype.js Create test2.jsp location WebCont ...
  8. The second stage of examination (HTML.Servlet.JSP.Javascript.Ajax.JQuery)

    ------------------------------------- Following is the pen questions (questions 2 points each) --- ----------- ...
  9. Servlet 2.5 new features

    September 26, 2005, Sun Company and the JSR154 Expert Group on Servlet API released a new version. Under norma ...
  10. Jsp | Ajax + fileupload + json + servlet by many file upload progress bar shows

    Sales: Jsp | Ajax + fileupload + json + servlet by many file upload progress bar shows demo video and screensh ...
  11. Servlet / JSP Detailed configuration (basic article)

    Beginners question such as: "How to configure the environment variable" "How to run Servlet&quo ...
  12. [Cactus entry] eclipse environment, integration with Cactus and Jetty to test Servlet

    1) download Cactus-1.8.1 Download commons-codec-1.3 2) decompression Cactus, put under lib add the following j ...
  13. jquery servlet jsp to verify the existence of complete user name $. ajax ()

    Finally do come out, sharing, online very few similar things, but also wrong for everyone to see a demo, there ...
  14. Tomcat how objects interact with the Servlet

    Tomcat is a Web application server, a Servlet / JSP container. Tomcat as a Servlet container, responsible for ...
  15. Servlet life cycle

    Servlet is a summary of previous research and the development of a standards development in the WEB occupy an ...
  16. Java Servlet Overview

    Java Servlet Overview
  17. Ajax servlet to achieve

    Ajax servlet to achieve
  18. javax.servlet.ServletException

    I have encountered in the development of this kind of error: javax.servlet.ServletException: Servlet.init () f ...
  19. JAIN SIP vs SIP Servlet

    JAIN SIP: Java API for Integrated Networks SIP, JSR32 defines its specification. PSTN, Wireless, Internet use ...
  20. Java problems in Chinese mysql, oracle, servlet, jsp

    [url] http://blog.csdn.net/arielxp/archive/2004/06/23/23874.aspx [/ url] Java problems in Chinese mysql, oracl ...