struts2笔记关于request/response/session

<script>function StorePage(){d=document;t=d.selection?(d.selection.type!='None'?d.selection.createRange().text:''):(d.getSelection?d.getSelection():'');void(keyit=window.open('http://www.365key.com/storeit.aspx?t='+escape(d.title)+'&u='+escape(d.location.href)+'&c='+escape(t),'keyit','scrollbars=no,width=475,height=575,left=75,top=20,status=no,resizable=yes'));keyit.focus();}</script> request/response/session
非Ioc方式

IocServlet.java。
package tutorial;

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

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

publicclass NonIoCServlet extends ActionSupport {
    private String message;
   
    public String getMessage() {
        return message;       
    }
   
    @Override
    public String execute() {   
        ActionContext.getContext().getSession().put("msg", "Hello World from Session!");
       
        HttpServletRequest request = ServletActionContext.getRequest();
        HttpServletResponse response = ServletActionContext.getResponse();       
        HttpSession session = request.getSession();
       
        StringBuffer sb =new StringBuffer("Message from request: ");
        sb.append(request.getParameter("msg"));
        sb.append("<br>Response Buffer Size: ");
        sb.append(response.getBufferSize());
        sb.append("<br>Session ID: ");
        sb.append(session.getId());
       
        message = sb.toString();
        return SUCCESS;
    }
}

Ioc方式

NonIoCServlet.java

package tutorial;

import java.util.Map;

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

import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import org.apache.struts2.interceptor.SessionAware;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

publicclass IoCServlet extends ActionSupport implements SessionAware, ServletRequestAware, ServletResponseAware {
    private String message;
    private Map att;
    private HttpServletRequest request;
    private HttpServletResponse response;   
   
    public String getMessage() {
        return message;       
    }
   
    publicvoid setSession(Map att) {
        this.att = att;
    }
   
    publicvoid setServletRequest(HttpServletRequest request) {
        this.request = request;
    }
   
    publicvoid setServletResponse(HttpServletResponse response) {
        this.response = response;
    }
   
    @Override
    public String execute() {       
        att.put("msg", "Hello World from Session!");
       
        HttpSession session = request.getSession();
       
        StringBuffer sb =new StringBuffer("Message from request: ");
        sb.append(request.getParameter("msg"));
        sb.append("<br>Response Buffer Size: ");
        sb.append(response.getBufferSize());
        sb.append("<br>Session ID: ");
        sb.append(session.getId());
       
        message = sb.toString();
        return SUCCESS;
    }
}

你可能感兴趣的:(apache,C++,c,servlet,IOC)