代码
servlet
map集合是关键
package com.rong.mvc.framework;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.rong.mvc.action.AddAction;
import com.rong.mvc.action.HelloAction;
/**
* 中央控制器
* @author 小荣
*
* 下午3:02:59
*/
@WebServlet("*.action")
public class ActionServlet extends HttpServlet {
private Map<String, Action> map;
@Override
public void init() throws ServletException {
map=new HashMap<String, Action>();
map.put("/helloAction", new HelloAction());
map.put("/addAction", new AddAction());
}
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// TODO Auto-generated method stub
doPost(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//1.获取请求路径
String reqURI=req.getRequestURI();
System.out.println(reqURI);
// System.out.println(req.getRequestURL());
int start=reqURI.lastIndexOf("/");
int end=reqURI.lastIndexOf(".action");
String actionName=reqURI.substring(start, end);
Action action=map.get(actionName);
action.execute(req, resp);
}
}
控制器(action)
package com.rong.mvc.framework;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* 控制器
* @author 小荣
*
* 下午3:07:09
*/
public abstract class Action {
public abstract String execute(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException;
}
addAction(子控制器)
这个才是处理相关业务的
package com.rong.mvc.action;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.rong.mvc.framework.Action;
public class AddAction extends Action {
@Override
public String execute(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String num1=req.getParameter("num1");
String num2=req.getParameter("num2");
int num3=Integer.parseInt(num1)+Integer.parseInt(num2);
req.setAttribute("num3", num3);
req.getRequestDispatcher("result.jsp").forward(req, resp);
return null;
}
}
jsp页面
add.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="addAction.action" method="post">
num1:<input name="num1" /><br>
num2:<input name="num2" /><br>
<input type="submit" value="+">
</form>
</body>
</html>
result.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<h2>${num3 }</h2>
</body>
</html>
运行效果
add.jsp
result.jsp
总结
今天讲了一下这个逻辑关系,map集合也是手动添加的,后期会有完善;
不要秃头!