5.自己动手写Java Web框架-上下文

jw的github地址是https://github.com/menyouping/jw

现在IndexController中的方法都只有两个参数,request和response,和Servlet的参数保持一致。但是,这个很不Spring啊!@RequestParam,@PathValue,@ModelAtrribute,Model都嗨起来!要把这几个嗨起来等慢慢来,本篇介绍一个最简单也是最基本的,每个请求维护一个上下文,把request,response,session都单独保存在SessionContext中, 也可以保存其他信息。

新增类jw-core/src/main/java/com/jw/util/SessionContext.java

package com.jw.util;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * 
 * @author jay
 *
 */
public class SessionContext implements java.io.Serializable {

    private static final long serialVersionUID = -8726882324373214664L;

    public final static String REQUEST = "request";
    public final static String RESPONSE = "response";
    public final static String SESSION = "session";

    private final static ThreadLocal<SessionContext> context = new ThreadLocal<SessionContext>();

    private Map<String, Object> map = new HashMap<String, Object>();

    public static SessionContext buildContext() {
        context.set(new SessionContext());
        return context.get();
    }

    public static void setContext(SessionContext ctx) {
        context.set(ctx);
    }

    public static SessionContext getContext() {
        return context.get();
    }

    public static void removeContext() {
        context.remove();
    }

    public SessionContext() {

    }

    public static HttpServletRequest getRequest() {
        return (HttpServletRequest) getContext().map.get(REQUEST);
    }

    public static HttpServletResponse getResponse() {
        return (HttpServletResponse) getContext().map.get(RESPONSE);
    }

    public static HttpSession getSession() {
        return (HttpSession) getContext().map.get(SESSION);
    }

    public boolean containsKey(String key) {
        return map.containsKey(key);
    }

    public void put(String key, Object value) {
        map.put(key, value);
    }

    public SessionContext set(String key, Object value) {
        map.put(key, value);
        return this;
    }

    public Object get(String key) {
        return map.get(key);
    }

    public Object get(String key, Object defaultValue) {
        if (map.containsKey(key))
            return map.get(key);

        return defaultValue;
    }

    public String getString(String key) {
        return (String) map.get(key);
    }

    public String getString(String key, String defaultValue) {
        return map.containsKey(key) ? getString(key) : defaultValue;
    }

    public boolean getBoolean(String key) {
        return (Boolean) map.get(key);
    }

    public boolean getBoolean(String key, boolean defaultValue) {
        return map.containsKey(key) ? getBoolean(key) : defaultValue;
    }

    public int getInt(String key) {
        return (Integer) map.get(key);
    }

    public int getInt(String key, int defaultValue) {
        return map.containsKey(key) ? getInt(key) : defaultValue;
    }
}

修改类jw-core/src/main/java/com/jw/web/servlet/DispatcherServlet.java
保存request,response,session等信息

public void service(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        String appName = request.getSession().getServletContext().getContextPath();
        request.setAttribute("root", appName);// 在jsp页面中添加root变量

        //...
        String[] paths = path.split("/", 2);

        // build context
        SessionContext.buildContext()
            .set(SessionContext.REQUEST, request)
            .set(SessionContext.RESPONSE, response)
            .set(SessionContext.SESSION, request.getSession())
            .set("requestUrl", path);


        String controllerClazeName = ConfigUtils.getProperty("package.scan") + "." + StringUtils.upperFirst(paths[0])
                + "Controller";
        String methodName = paths[1];
        //...
    }

上下文已经准备好了,本篇结束。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值