struts 2 학습 노트 2(초보 servlet)

10846 단어 자바Webservlet
1、  
        servlet              ,    -      web  ,             ,           servlet,servlet                   ,                。
            :
               servlet   -------->      servlet ------>   servlet  

        -------->   servlet  init  ----->  service      

        -------->  servlet  destroy  
      web.xml:
        <?xml version="1.0" encoding="UTF-8"?>
        <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd">
        <web-app>
          <servlet>
            <servlet-name>helloreader</servlet-name>
            <display-name>This is the display name of my J2EE component</display-name>
            <description>This is the description of my J2EE component</description>
            <servlet-class>com.book.web3.helloreader</servlet-class>
          </servlet>

          <servlet-mapping>
            <servlet-name>helloreader</servlet-name>
            <url-pattern>/helloreader</url-pattern>
          </servlet-mapping>

        </web-app>
       helloreader.java
        package com.book.web3;
        public class helloreader extends HttpServlet {
            @Override
            public void service(ServletRequest req, ServletResponse res)
                    throws ServletException, IOException {
                // TODO Auto-generated method stub
                //super.service(req, res);
                String name = req.getParameter("name");
                res.setContentType("text/html");
                PrintWriter out = res.getWriter();
                out.println("<html>");

                out.println("<head>");
                out.println("<title>Hello Reader</title>");
                out.println("</head>");

                out.println("<body>");
                out.println("<h1>Hello " + name + "!</h1>");
                out.println("</body>");
                out.println("</html>");
            }
        }
            ,        servlet     。
        tomcat        ,            web.xml,     <servlet-mapping>      ,     url   servlet-name         ,        ,      。
          <servlet>
            <servlet-name>helloreader</servlet-name>
            <servlet-class>com.book.web3.helloreader</servlet-class>
          </servlet>
          <servlet-mapping>
            <servlet-name>helloreader</servlet-name>
            <url-pattern>/helloreader</url-pattern>
          </servlet-mapping>
2、   
                    ,       servlet,       servlet       ,     servlet       。
     :            
        package com.book.web3;
        import java.io.*;
        import javax.servlet.*;
        import javax.servlet.http.*;
        public class TimeFilter implements Filter{
            private FilterConfig config = null;

            public void init(FilterConfig config) throws ServletException{
                    this.config = config;
            }
            @Override
            public void destroy(){
                config=null;
            }
            /*
             * filter       
             */
            public void doFilter(ServletRequest request,ServletResponse response,FilterChain chain) throws IOException,ServletException{
                long before = System.currentTimeMillis();
                chain.doFilter(request, response);
                long after = System.currentTimeMillis();
                String name = "";
                if(request instanceof HttpServletRequest){          
                    name = ((HttpServletRequest)request).getRequestURI();
                }
                config.getServletContext().log(name+":"+(after-before) + "ms");
            }
        }
    myeclipse      new->filter    ,       filter 
3、   
            ,       。     ,          。         
            
        ServletContextAttributeListener
               ServletContext     ,    ,  ,  
        ServletContextListener
              ServletContext,   ServletContext ,  contextInitialized  ;   servletcontext ,  contextDestroyed  
        HttpSessionListener
              HttpSession  ,    session ,  sessionCreated  ;     session ,  sessionDestroyed  
        HttpSessionAttributeListener
              HttpSession      。  session        ,  attributeAdded  ;        ,  attributeRemove  ;     ,  attributeReplaced  
     :        
          OnlineCounterListener.java

           public class OnlineCounterListener implements HttpSessionListener {

            /**
             * Default constructor. 
             */
            public OnlineCounterListener() {
            // TODO Auto-generated constructor stub
            }

            /**
             * @see HttpSessionListener#sessionCreated(HttpSessionEvent)
             */
            public void sessionCreated(HttpSessionEvent arg0) {
            // TODO Auto-generated method stub
                OnlineCounter.raise();
                System.out.println(OnlineCounter.getOnline());
            }

            /**
             * @see HttpSessionListener#sessionDestroyed(HttpSessionEvent)
             */
            public void sessionDestroyed(HttpSessionEvent arg0) {
            // TODO Auto-generated method stub
                OnlineCounter.reduce();
                System.out.println(OnlineCounter.getOnline());
            }

        }
        OnlineCounter.java
        public class OnlineCounter {
            private static long online = 0;

            public static long getOnline(){
                return online;
            }
            public static void raise(){
                online++;
            }
            public static void reduce(){
                online--;
            }
        }
              ,             。     web.xml  ,   listener          ,         ,   listener    。
4、   JSP  
                ,  JSP        。
         ,            。
                  ,                    。
    a、  
          
              xml  ,            
           
                    ,               
               
              xml  ,  TLD  ,             JSP           ,       , web.xml  。
             
            java ,   TagSupport     SimpleTag  ,            JSP       
                 ,        TLD          
    b、       
            Tag   doStartTag doEndTag  。
         JSP    tag      ,  doStartTag;  tag   ,doEndTag   。
         :     “Hello World!”
            public class SimpleTag extends TagSupport {

                /**
                 * 
                 */
                private static final long serialVersionUID = 1L;

                @Override
                public int doEndTag() throws JspException {
                    // TODO Auto-generated method stub
                    try{
                            pageContext.getOut().print("Hello Reader!");
                            pageContext.getOut().print("<br>");
                            pageContext.getOut().print("I'm from custom Tag");
                    }catch(Exception e){
                        throw new JspTagException("SimpleTag" + e.getMessage());
                    }
                    return SKIP_BODY;//   tag  body,    SKIP_BODY
                }

                @Override
                public int doStartTag() throws JspException {
                    // TODO Auto-generated method stub
                    return EVAL_PAGE;//          ,          ,   SKIP_PAGE
                }

            }
    c、         
                 TLD  , xml    。TLD             ,   JSP               ,           。TLD         .tld,        WEB   WEB-INF       。
         :                   
            <?xml version="1.0" encoding="UTF-8"?>
            <!DOCTYPE taglib PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN"
                        "http://java.sun.com/dtd/web-jsptaglibrary_1_2.dtd">
            <taglib>
             <tlib-version>1.0</tlib-version>
             <jsp-version>1.2</jsp-version>
             <short-name>hellodemo</short-name>
             <tag>
                <name>hello</name>
                <tag-class>com.book.web3.SimpleTag</tag-class>
                <body-content>emtpty</body-content>
             </tag>
            </taglib>   
    d、       
          web.xml,       <taglib>
            <?xml version="1.0" encoding="UTF-8"?>
            <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd">
            <web-app>

                <filter>
                <filter-name>timefilter</filter-name>
                <filter-class>com.book.web3.TimeFilter</filter-class>
                </filter>
                <listener>
                <listener-class>com.book.web3.OnlineCounterListener</listener-class>
                </listener>
                <servlet>
                <servlet-name>helloreader</servlet-name>
                <servlet-class>com.book.web3.helloreader</servlet-class>
                </servlet>
                <servlet-mapping>
                <servlet-name>helloreader</servlet-name>
                <url-pattern>/helloreader</url-pattern>
                </servlet-mapping>
                <taglib>
                    <taglib-uri> /WEB-INF/helloreader.tld </taglib-uri>
                    <taglib-location>/WEB-INF/helloreader.tld</taglib-location>
                </taglib>
            </web-app>
              :web.xml        ,<taglib>    web.xml   
          :
            <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
            <%@ taglib uri="/WEB-INF/helloreader.tld" prefix="demo"%>

            <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
            <html>
              <head>
                <title>My JSP 'tagTest.jsp' starting page</title>
              </head>

              <body>
                    <demo:hello/>
              </body>
            </html> 
                 ,      tag  ,jsp      ,           。

좋은 웹페이지 즐겨찾기