J2EE自定义MVC一

代码

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集合也是手动添加的,后期会有完善;
不要秃头!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值