<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    隨筆-159  評論-114  文章-7  trackbacks-0
    容器就是負責運行和銷毀servlet的一組類的程序,一組代碼的運行環境。

    為了開發方便,可以從javax.servlet.GenericServlet 繼承實現主要業務方法。

    注意幾點,

    1.有一個無參數的init()方法,是GenericServlet實現的,它不是Servlet接口中的方法,所以不會由容器調用,而是因為GenericServlet本身已經覆蓋了帶參數的init方法,并且在方法的最后調用了自己的無參init()。

    所以開發人員只需要繼承,并覆蓋無參init,實現自己的資源申請動作。

    如果真要覆蓋帶參數的init(ServletConfig config),那么需要在第一行調用super.init(config),因為getServletConfig要返回這個config對象,如果不調用父類的init,不會保存下來。那么getServletConfig返回空。

    HttpServlet方便了程序員進行Http協議的Servlet開發。

    主要有幾個

    protected void doGet(HttpServletRequest req,
                         HttpServletResponse resp) throws ServletException,
                         java.io.IOException

    protected void doPost(HttpServletRequest req,
                          HttpServletResponse resp) throws ServletException,
                          java.io.IOException

    protected void service(HttpServletRequest req,
                           HttpServletResponse resp) throws ServletException,
                           java.io.IOException
    主要參數,已經由容器封裝成為HttpServletRequest對象,相應也就有更多的方法獲得相關信息。

    package imshark.servlet.http;

    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;

    public class PrintHeadingServlet extends HttpServlet {

        
    /**
         * Constructor of the object.
         
    */

        
    public PrintHeadingServlet() {
            
    super();
        }


        
    /**
         * Destruction of the servlet. <br>
         
    */

        
    public void destroy() {
            
    super.destroy(); // Just puts "destroy" string in log
            
    // Put your code here
        }


        
    /**
         * The doGet method of the servlet. <br>
         * 
         * This method is called when a form has its tag value method equals to get.
         * 
         * 
    @param request
         *            the request send by the client to the server
         * 
    @param response
         *            the response send by the server to the client
         * 
    @throws ServletException
         *             if an error occurred
         * 
    @throws IOException
         *             if an error occurred
         
    */

        
    public void doGet(HttpServletRequest request, HttpServletResponse response)
                
    throws ServletException, IOException {

            response.setContentType(
    "text/html");
            PrintWriter out 
    = response.getWriter();
            out.println(
    "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
            out.println("<HTML>");
            out.println(
    "  <HEAD><TITLE>A Servlet</TITLE></HEAD>");
            out.println(
    "  <BODY>");
            
    for (int i = 1; i < 6; i++{
                out.print(
    "     <h" + i + ">" + this.getClass() + "</h" + i + ">");
            }

            out.println(
    "  </BODY>");
            out.println(
    "</HTML>");
            out.flush();
            out.close();
        }


        
    /**
         * The doPost method of the servlet. <br>
         * 
         * This method is called when a form has its tag value method equals to
         * post.
         * 
         * 
    @param request
         *            the request send by the client to the server
         * 
    @param response
         *            the response send by the server to the client
         * 
    @throws ServletException
         *             if an error occurred
         * 
    @throws IOException
         *             if an error occurred
         
    */

        
    public void doPost(HttpServletRequest request, HttpServletResponse response)
                
    throws ServletException, IOException {

            response.setContentType(
    "text/html");
            PrintWriter out 
    = response.getWriter();
            out
                    .println(
    "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
            out.println("<HTML>");
            out.println(
    "  <HEAD><TITLE>A Servlet</TITLE></HEAD>");
            out.println(
    "  <BODY>");
            out.print(
    "    This is ");
            out.print(
    this.getClass());
            out.println(
    ", using the POST method");
            out.println(
    "  </BODY>");
            out.println(
    "</HTML>");
            out.flush();
            out.close();
        }


        
    /**
         * Initialization of the servlet. <br>
         * 
         * 
    @throws ServletException
         *             if an error occure
         
    */

        
    public void init() throws ServletException {
            
    // Put your code here
        }


    }


    ===============================================

    處理Html表單。

    主要適用HttpServletRequest的一些方法。

    public java.lang.String getParameter(java.lang.String name)

    public java.util.Enumeration getParameterNames()

    public java.lang.String[] getParameterValues(java.lang.String name)

    public java.util.Map getParameterMap()



    ============================
    public java.lang.String getProtocol()

    public java.lang.String getServerName()

    public int getServerPort()
    ============================


    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
      
    <head>
        
    <title>index.html</title>
        
        
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
        
    <meta http-equiv="description" content="this is my page">
        
    <meta http-equiv="content-type" content="text/html; charset=UTF-8">
        
        
    <!--<link rel="stylesheet" type="text/css" href="./styles.css">-->
        
      
    </head>
      
      
    <body>
            
    <FORM name="regForm" action="/MyWebApp/servlet/HandleFormServlet" method="POST">
                
    <P>
                     
    &nbsp;
                    
    <INPUT type="text" name="userName" value="輸入姓名" size="20" maxlength="15">
                
    </P><P>
                     
    &nbsp;
                    
    <INPUT type="password" name="userPw" size="20" maxlength="18">
                
    </P><P>
                     
    &nbsp; 男<INPUT type="radio" name="sex" value="1" checked>
                     女
    <INPUT type="radio" name="sex" value="0">                
                
    </P><P>
                     
    &nbsp; 足球<INPUT type="checkbox" name="habit" value="football">
                     籃球
    <INPUT type="checkbox" name="habit" value="basketball">
                
    </P><P>
                     
    &nbsp; 自我介紹:</P><P>
                     
    &nbsp;
                    
    <TEXTAREA name="selfInfo" rows="5" cols="40"></TEXTAREA>
                
    </P><P>
                    
    &nbsp; <INPUT type="submit" value="注冊">
                    
    <INPUT type="Reset" value="重置" />
                    
    &nbsp;</P><P></P></FORM>
        
    </body>
    </html>


    package imshark.servlet.http;

    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;

    public class HandleFormServlet extends HttpServlet {

        
    /**
         * Constructor of the object.
         
    */

        
    public HandleFormServlet() {
            
    super();
        }


        
    /**
         * Destruction of the servlet. <br>
         
    */

        
    public void destroy() {
            
    super.destroy(); // Just puts "destroy" string in log
            
    // Put your code here
        }


        
    /**
         * The doGet method of the servlet. <br>
         *
         * This method is called when a form has its tag value method equals to get.
         * 
         * 
    @param request the request send by the client to the server
         * 
    @param response the response send by the server to the client
         * 
    @throws ServletException if an error occurred
         * 
    @throws IOException if an error occurred
         
    */

        
    public void doGet(HttpServletRequest request, HttpServletResponse response)
                
    throws ServletException, IOException {

            response.setContentType(
    "text/html");
            PrintWriter out 
    = response.getWriter();
            out
                    .println(
    "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
            out.println("<HTML>");
            out.println(
    "  <HEAD><TITLE>A Servlet</TITLE></HEAD>");
            out.println(
    "  <BODY>");
            out.print(
    "    This is ");
            out.print(
    this.getClass());
            out.println(
    ", using the GET method");
            out.println(
    "  </BODY>");
            out.println(
    "</HTML>");
            out.flush();
            out.close();
        }


        
    /**
         * The doPost method of the servlet. <br>
         *
         * This method is called when a form has its tag value method equals to post.
         * 
         * 
    @param request the request send by the client to the server
         * 
    @param response the response send by the server to the client
         * 
    @throws ServletException if an error occurred
         * 
    @throws IOException if an error occurred
         
    */

        
    public void doPost(HttpServletRequest request, HttpServletResponse response)
                
    throws ServletException, IOException {

            response.setContentType(
    "text/html");
            PrintWriter out 
    = response.getWriter();
            out.println(
    "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
            out.println("<HTML>");
            out.println(
    "  <HEAD><TITLE>A Servlet</TITLE></HEAD>");
            out.println(
    "  <BODY>");
            java.util.Enumeration names 
    = request.getParameterNames();
            
    while(names.hasMoreElements())
            
    {
                String nameKey 
    = (String)names.nextElement();
                String[] values 
    = request.getParameterValues(nameKey);
                out.print(nameKey 
    + "{");
                
    for(int i = 0; i < values.length; i++)
                
    {
                    
    if(i != 0)
                        out.print(
    ","+ values[i]);
                    out.print(values[i]);
                }

                out.println(
    "}");
            }

            out.println(
    "  </BODY>");
            out.println(
    "</HTML>");
            out.flush();
            out.close();
        }


        
    /**
         * Initialization of the servlet. <br>
         *
         * 
    @throws ServletException if an error occure
         
    */

        
    public void init() throws ServletException {
            
    // Put your code here
        }


    }



    posted on 2006-02-11 12:10 北國狼人的BloG 閱讀(443) 評論(0)  編輯  收藏 所屬分類: 達內學習總結
    主站蜘蛛池模板: 亚洲AV成人一区二区三区观看| 亚洲午夜国产精品| 一级做a爱过程免费视| 亚洲永久在线观看| 五月婷婷综合免费| 亚洲成人激情小说| 国产成人青青热久免费精品| 国产成人综合亚洲| 91成人免费观看网站| 亚洲一区精品视频在线| 波多野结衣久久高清免费| 国产精品亚洲а∨无码播放麻豆| 免费国产一级特黄久久| 春意影院午夜爽爽爽免费| 成人免费无码大片a毛片软件| 日韩一卡2卡3卡4卡新区亚洲| 亚洲综合无码一区二区痴汉| 97在线免费视频| 久久精品亚洲中文字幕无码麻豆 | 日韩视频在线观看免费| 亚洲成色在线影院| 中文字幕无码成人免费视频 | 亚洲AV午夜福利精品一区二区| 国产午夜精品免费一区二区三区| 亚洲大尺度无码无码专区| 日本片免费观看一区二区| 无码亚洲成a人在线观看| 国产av无码专区亚洲国产精品| 七色永久性tv网站免费看| 亚洲一区二区久久| 国产一级淫片免费播放| 在线免费观看伊人三级电影| 亚洲日韩乱码中文无码蜜桃| 国产国产人免费视频成69大陆 | 亚洲av一本岛在线播放| 免费在线一级毛片| 日本免费大黄在线观看| 亚洲av无码一区二区三区人妖| 亚洲欧洲日产国码无码网站| 最新欧洲大片免费在线| 99免费精品视频|