java自己写一个上下文_5.自己动手写Java Web框架-上下文
現在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 context = new ThreadLocal();
private Map map = new HashMap();
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];
//...
}
上下文已經準備好了,本篇結束。
總結
以上是生活随笔為你收集整理的java自己写一个上下文_5.自己动手写Java Web框架-上下文的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 自己开母婴店进货渠道 不加盟的话
- 下一篇: 股票独角兽什么意思