?? dispatcherservlet.java.svn-base
字號:
/**
*
*/
package cn.handson.controller.servlet;
import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import cn.handson.controller.action.*;
/**
* @author Administrator
*
*/
public class DispatcherServlet extends HttpServlet {
@Override
public void init() throws ServletException {
// TODO Auto-generated method stub
super.init();
}
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
dispatch(request,response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
dispatch(request,response);
}
@Override
public void destroy() {
// TODO Auto-generated method stub
super.destroy();
}
/**
* <P>???????例子:
*以用戶登錄為例:
*在前臺頁面中,用戶提交的FORM表單中,action為"login",經配制文件web.xml解析,由此方法判斷是哪個操作:
*if (dispatcher.equals("login")) {
// 用戶較驗,LoginAction為繼承BaseAction的一個類。getLogionAction方法為LoginAction的一個靜態方法。
//public static BaseAction getLoginAction(){
// if(loginAction == null){
// loginAction = new LoginAction();
// }
// return loginAction;
// }此為LoginAction方法。
//其中query方法為LoginAction中的方法。以此為例
BaseAction loginAction = LoginAction.getLoginAction();
URL = loginAction.query(request, response);
}</P>
* @param request
* @param response
*/
private void dispatch(HttpServletRequest request,
HttpServletResponse response){
String URL = null;
try{
String requestURI = request.getRequestURI();
String dispatcher = getPrefix(requestURI);
doForward(URL,request,response);
}catch(Exception e){
e.printStackTrace();
}
}
private String getPrefix(String Url){
String[] prefix = Url.split("/");
try{
for(int i=0;i<prefix.length;i++){
if(prefix[i].contains("do")){
int index = prefix[i].indexOf(".");
String pre = prefix[i].substring(0, index);
return pre;
}
}
}catch(IndexOutOfBoundsException e){
System.err.println("request prefix is error!"
+e.getMessage());
}catch(Exception e){
e.printStackTrace();
}
return null;
}
/**
* <p>???????????,Forword????μ????</p>
*
* @param uri Context-relative URI to forward to
* @param request Current page request
* @param response Current page response
* @throws IOException if an input/output error occurs
* @throws ServletException if a servlet exception occurs
*/
protected void doForward(String uri, HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException {
RequestDispatcher rd = this.getServletContext().getRequestDispatcher(uri);
if (rd == null) {
response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
"requestDispatcher");
return;
}
rd.forward(request, response);
}
/**
* <p>???????????,Redirect????μ????</p>
* @param uri
* @param request
* @param response
* @throws IOException
* @throws ServletException
*
*/
protected void doRedirect(String uri, HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException {
if (uri.startsWith("/")) {
uri = request.getContextPath() + uri;
}
response.sendRedirect(response.encodeRedirectURL(uri));
}
private static final long serialVersionUID = 1L;
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -