📄 controllerservlet.java
字号:
package com.wl.servlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.wl.action.Action;
import com.wl.servletmanager.ServletManager;
public class ControllerServlet extends HttpServlet {
/**
*
*/
private static final long serialVersionUID = 1L;
/**
* Initialization of the ServLet.
*/
@Override
public void init() throws ServletException {
ServletManager manager = ServletManager.getInstance();
manager.init();
}
/**
* Constructor of the object.
*/
public ControllerServlet() {
super();
}
/**
* Destruction of the servLet. <br>
*/
public void destroy() {
super.destroy();
}
/**
* 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
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doService(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
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doService(request, response);
}
public void doService(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String path = request.getServletPath(); //从页面获取的action的值
path = this.getPath(path);//处理后的action的值(去掉"/"和".")
//进行路径处理
ServletManager manager = ServletManager.getInstance();
if(manager.containsKey(path)) {
Action action = manager.get(path);
String result = action.doExcute(request, response);
request.getRequestDispatcher(result).forward(request, response);
}
else {
throw new RuntimeException("sorry,your path notExsit");
}
}
/**
* 对字符串的截取
* @param path
* @return path
*/
public String getPath(String path) {
int first = path.lastIndexOf("/");
int last = path.lastIndexOf(".");
if(path!=null) {
path = path.substring(first+1,last);
}
else {
throw new RuntimeException("sorry,your path notExist");
}
return path;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -