feezh

          我們之所以努力賺錢,是為了讓父母為自己買東西時能像給我們買東西時一樣大方!
          隨筆 - 7, 文章 - 0, 評論 - 10, 引用 - 0
          數據加載中……

          Servlet簡單封裝

          封裝類
          package com.weidu.servlet;

          import java.io.IOException;

          import javax.servlet.ServletException;
          import javax.servlet.http.HttpServlet;
          import javax.servlet.http.HttpServletRequest;
          import javax.servlet.http.HttpServletResponse;

          /**
          *
          * 標題:servlet跳轉基類<br>
          * 描述:用戶打開鏈接時傳遞參數method即可跳轉到相應的方法,示例:users?method=add,參數有:add,update,del,select<br>
          * 實現時需覆寫父類相應的方法。 如果新增方法時覆寫addMethod()方法,添加判斷并調用即可<br>
          * 示例:@Override protected boolean addMethod(String method) {<br>
          * if (method.equals("login")) {<br>
          * login();<br>
          * return true;<br>
          * }<br>
          * return super.addMethod(method);<br>
          * }<br>
          *
          * 作者:feezh<br>
          * 郵箱:feezh@qq.com <br>
          * 日期:2012-7-18 時間:下午04:02:35 <br>
          * 版本:1.0 <br>
          */

          public class BaseServlet extends HttpServlet {
             

             
          public BaseServlet() {
                 
          super();
              }


             
          /**
               *
               * 功能說明: 添加新的方法<br>
               * 作者:feezh <br>
               * 日期:2012-7-18 時間:下午04:30:02 <br>
               *
               *
          @param method
               *
              
          */

             
          protected boolean addMethod(String method) {
                 
          return false;
              }


             
          /**
               * 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 {

                  doPost(request, response);
              }


             
          /**
               * 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");
                  String method
          = request.getParameter("method");
                 
          if (method == null || method.equals("select")) {
                      select(request, response);
                  }
          else if (method.equals("del")) {
                      del(request, response);
                  }
          else if (method.equals("update")) {
                      update(request, response);
                  }
          else if (method.equals("add")) {
                      add(request, response);
                  }
          else if (addMethod(method)) {
                  }
          else {
                      response.sendError(HttpServletResponse.SC_NOT_FOUND,
          "路徑不存在!");
                  }

              }


             
          /**
               * 功能說明: 刪除信息<br>
               * 作者:feezh <br>
               * 日期:2012-7-18 時間:下午04:02:57 <br>
               *
               *
          @param request
               *
          @param response
               *
          @throws ServletException
               *
          @throws IOException
              
          */


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

              }


             
          /**
               *
               * 功能說明: 添加信息<br>
               * 作者:feezh <br>
               * 日期:2012-7-18 時間:下午04:02:49 <br>
               *
               *
          @param request
               *
          @param response
               *
          @throws ServletException
               *
          @throws IOException
              
          */

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

              }


             
          /**
               *
               * 功能說明: 修改信息<br>
               * 作者:feezh <br>
               * 日期:2012-7-18 時間:下午04:03:07 <br>
               *
               *
          @param request
               *
          @param response
               *
          @throws ServletException
               *
          @throws IOException
              
          */

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

              }


             
          /**
               *
               * 功能說明: 查詢信息<br>
               * 作者:feezh <br>
               * 日期:2012-7-18 時間:下午04:03:13 <br>
               *
               *
          @param request
               *
          @param response
               *
          @throws ServletException
               *
          @throws IOException
              
          */

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

              }


          }


          測試類
          package com.weidu.servlet;

          import java.io.IOException;

          import javax.servlet.ServletException;
          import javax.servlet.http.HttpServletRequest;
          import javax.servlet.http.HttpServletResponse;

          /**
          * 標題:<br>
          * 描述:TODO<br>
          * 作者:feezh<br>
          * 郵箱:feezh@qq.com <br>
          * 日期:2012-7-18 時間:下午04:04:44 <br>
          * 版本:1.0 <br>
          */

          public class UsersServlet extends BaseServlet {

             
          /**
               * @Fields serialVersionUID : TODO
              
          */


             
          private static final long serialVersionUID = 1L;

              @Override
             
          public void del(HttpServletRequest request, HttpServletResponse response)
                     
          throws ServletException, IOException {
                 
          super.del(request, response);
                  System.out.println(
          "刪除");
              }


              @Override
             
          public void add(HttpServletRequest request, HttpServletResponse response)
                     
          throws ServletException, IOException {
                 
          super.add(request, response);
                  System.out.println(
          "添加");
              }


              @Override
             
          public void update(HttpServletRequest request, HttpServletResponse response)
                     
          throws ServletException, IOException {
                 
          super.update(request, response);
                  System.out.println(
          "修改");
              }


              @Override
             
          public void select(HttpServletRequest request, HttpServletResponse response)
                     
          throws ServletException, IOException {
                 
          super.select(request, response);
                  System.out.println(
          "查詢");
              }


              @Override
             
          protected boolean addMethod(String method) {
                 
          if (method.equals("login")) {
                      login();
                     
          return true;
                  }

                 
          return super.addMethod(method);
              }


             
          public void login() {
                  System.out.println(
          "登錄");
              }

          }


           web.xml
           
          <servlet>
                 
          <servlet-name>UsersServlet</servlet-name>
                 
          <servlet-class>com.weidu.servlet.UsersServlet</servlet-class>
             
          </servlet>
             
          <servlet-mapping>
                 
          <servlet-name>UsersServlet</servlet-name>
                 
          <url-pattern>/Users</url-pattern>
             
          </servlet-mapping>

          訪問路徑:http://localhost/Users?method=login

          posted on 2012-07-19 09:10 feezh 閱讀(2094) 評論(1)  編輯  收藏 所屬分類: Java相關

          評論

          # re: Servlet簡單封裝  回復  更多評論   

          Valuable information ..I am delighted to read this article..thank you for giving us this useful information. Great walk-through. I value this post...
          2012-10-30 13:30 | web designing jp nagar
          主站蜘蛛池模板: 江孜县| 故城县| 西安市| 弋阳县| 巫山县| 永和县| 南康市| 汾阳市| 荆门市| 日照市| 靖边县| 和静县| 孙吴县| 山阳县| 元江| 绿春县| 会理县| 蓬安县| 北票市| 元氏县| 赞皇县| 公安县| 枞阳县| 广德县| 文化| 宝鸡市| 凤台县| 晴隆县| 乳山市| 旬阳县| 克什克腾旗| 关岭| 东乡县| 晴隆县| 保定市| 镇赉县| 垦利县| 遂宁市| 澄城县| 中宁县| 南涧|