隨筆-159  評(píng)論-114  文章-7  trackbacks-0
          容器就是負(fù)責(zé)運(yùn)行和銷毀servlet的一組類的程序,一組代碼的運(yùn)行環(huán)境。

          為了開發(fā)方便,可以從javax.servlet.GenericServlet 繼承實(shí)現(xiàn)主要業(yè)務(wù)方法。

          注意幾點(diǎn),

          1.有一個(gè)無(wú)參數(shù)的init()方法,是GenericServlet實(shí)現(xiàn)的,它不是Servlet接口中的方法,所以不會(huì)由容器調(diào)用,而是因?yàn)镚enericServlet本身已經(jīng)覆蓋了帶參數(shù)的init方法,并且在方法的最后調(diào)用了自己的無(wú)參init()。

          所以開發(fā)人員只需要繼承,并覆蓋無(wú)參init,實(shí)現(xiàn)自己的資源申請(qǐng)動(dòng)作。

          如果真要覆蓋帶參數(shù)的init(ServletConfig config),那么需要在第一行調(diào)用super.init(config),因?yàn)間etServletConfig要返回這個(gè)config對(duì)象,如果不調(diào)用父類的init,不會(huì)保存下來(lái)。那么getServletConfig返回空。

          HttpServlet方便了程序員進(jìn)行Http協(xié)議的Servlet開發(fā)。

          主要有幾個(gè)

          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
          主要參數(shù),已經(jīng)由容器封裝成為HttpServletRequest對(duì)象,相應(yīng)也就有更多的方法獲得相關(guān)信息。

          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="注冊(cè)">
                          
          <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
              }


          }



          主站蜘蛛池模板: 潞西市| 金溪县| 鹤山市| 盐亭县| 宣化县| 沛县| 池州市| 天门市| 苍山县| 东港市| 霍山县| 尚义县| 周至县| 万源市| 霍林郭勒市| 呼玛县| 大足县| 大港区| 蒙城县| 临颍县| 尖扎县| 大宁县| 宜丰县| 合水县| 治多县| 行唐县| 敖汉旗| 宝应县| 南阳市| 宜都市| 内乡县| 绩溪县| 大余县| 阆中市| 吉木乃县| 龙里县| 瓮安县| 道孚县| 亳州市| 湘西| 巨野县|