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





































































































































































測試類


































































web.xml








訪問路徑:http://localhost/Users?method=login
posted on 2012-07-19 09:10 feezh 閱讀(2094) 評論(1) 編輯 收藏 所屬分類: Java相關