建立總覆蓋路由
一旦我這麼做了,整個參數後面的路徑都會經過這個 Servlet 處理。
package com.permission; import com.apostlescookies.ApostlesCookies; import com.util.UB_AES; import java.io.IOException; import java.io.PrintWriter; import java.net.URI; import java.net.URLDecoder; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import javax.activation.MimetypesFileTypeMap; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.Cookie; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.commons.io.FilenameUtils; import org.json.JSONObject; /** * * @author Mac */ @WebServlet(urlPatterns = {"/administrator/*"}) //設定這個路徑 public class AdministratorRouting extends HttpServlet { @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //response.setContentType("text/html;charset=UTF-8"); try{ /*這裡可以寫控管權限,像是撈出 Session, Cookie...etc*/ String ext = FilenameUtils.getExtension(request.getPathInfo()); if(ext.equals("css") || ext.equals("png") || ext.equals("js")){ MimetypesFileTypeMap mimeMap = new MimetypesFileTypeMap(); mimeMap.addMimeTypes("text/css css"); mimeMap.addMimeTypes("text/javascript js"); mimeMap.addMimeTypes("image/png png"); mimeMap.addMimeTypes("text/html html"); response.setContentType(mimeMap.getContentType(request.getPathInfo())); } //撈出本機目錄底下的資料夾檔案 request.getRequestDispatcher("/WEB-INF/Administrator/"+request.getPathInfo()).include(request, response); }catch(Exception e){ //找不到就回傳 404 System.out.println(e); response.setContentType("text/html;charset=UTF-8"); response.getWriter().print("<h1>404 NOT FOUND</h1>"); response.getWriter().close(); } } }
沒有留言:
張貼留言