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 阅读(2073) 评论(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

只有注册用户登录后才能发表评论。


网站导航: