⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 dispatcherservlet.java

📁 本项目是基于展示新闻的一款产品。在本系统中用户可以自定义栏目及新闻分类 发布新闻信息
💻 JAVA
字号:
/**
 * 
 */
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>��Dz����例子:
	 *以用户登录为例:
	 *在前台页面中,用户提交的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 + -