導航:首頁 > 編程語言 > javarequest獲取參數

javarequest獲取參數

發布時間:2022-12-14 02:20:38

java中請求參數action怎麼獲取

1. ActionContext
在Struts2開發中,除了將請求參數自動設置到Action的欄位中,我們往往也需要在Action里直接獲取請求(Request)或會話(Session)的一些信息,甚至需要直接對JavaServlet Http的請求(HttpServletRequest),響應(HttpServletResponse)操作. 我們需要在Action中取得request請求參數"username"的值:
ActionContext context = ActionContext.getContext();
Map params = context.getParameters();
String username = (String) params.get("username");
on執行時的上下文,上下文可以看作是一個容器(其實我們這里的容器就是一個Map而已),它存放的是Action在執行時需要用到的對象. 一般情況, 我們的ActionContext都是通過: ActionContext context = (ActionContext) actionContext.get();來獲取的.我們再來看看這里的actionContext對象的創建:
static ThreadLocal actionContext = new ActionContextThreadLocal();
ActionContextThreadLocal是實現ThreadLocal的一個內部類.ThreadLocal可以命名為"線程局部變數",它為每一個使用該變數的線程都提供一個變數值的副本,使每一個線程都可以獨立地改變自己的副本,而不會和其它線程的副本沖突.這樣,我們ActionContext里的屬性只會在對應的當前請求線程中可見,從而保證它是線程安全的.
通過ActionContext取得HttpSession: Map session = ActionContext.getContext().getSession();
2. ServletActionContext
ServletActionContext(com.opensymphony.webwork. ServletActionContext),這個類直接繼承了我們上面介紹的ActionContext,它提供了直接與Servlet相關對象訪問的功能,它可以取得的對象有:
(1)javax.servlet.http.HttpServletRequest : HTTPservlet請求對象
(2)javax.servlet.http.HttpServletResponse : HTTPservlet相應對象
(3)javax.servlet.ServletContext : Servlet上下文信息
(4)javax.servlet.ServletConfig : Servlet配置對象
(5)javax.servlet.jsp.PageContext : Http頁面上下文
如何從ServletActionContext里取得Servlet的相關對象:
<1>取得HttpServletRequest對象: HttpServletRequest request = ServletActionContext. getRequest();
<2>取得HttpSession對象: HttpSession session = ServletActionContext. getRequest().getSession();
3. ServletActionContext和ActionContext聯系
ServletActionContext和ActionContext有著一些重復的功能,在我們的Action中,該如何去抉擇呢?我們遵循的原則是:如果ActionContext能夠實現我們的功能,那最好就不要使用ServletActionContext,讓我們的Action盡量不要直接去訪問Servlet的相關對象.
注意:在使用ActionContext時有一點要注意: 不要在Action的構造函數里使用ActionContext.getContext(),因為這個時候ActionContext里的一些值也許沒有設置,這時通過ActionContext取得的值也許是null;同樣,HttpServletRequest req = ServletActionContext.getRequest()也不要放在構造函數中,也不要直接將req作為類變數給其賦值。至於原因,我想是因為前面講到的static ThreadLocal actionContext = new ActionContextThreadLocal(),從這里我們可以看出ActionContext是線程安全的,而ServletActionContext繼承自ActionContext,所以ServletActionContext也線程安全,線程安全要求每個線程都獨立進行,所以req的創建也要求獨立進行,所以ServletActionContext.getRequest()這句話不要放在構造函數中,也不要直接放在類中,而應該放在每個具體的方法體中(eg:login()、queryAll()、insert()等),這樣才能保證每次產生對象時獨立的建立了一個req。
4. struts2中獲得request、response和session
(1)非IoC方式
方法一:使用org.apache.struts2.ActionContext類,通過它的靜態方法getContext()獲取當前Action的上下文對象。
ActionContext ctx = ActionContext.getContext();
ctx.put("liuwei", "andy"); //request.setAttribute("liuwei", "andy");
Map session = ctx.getSession(); //session
HttpServletRequest request = ctx.get(org.apache.struts2.StrutsStatics.HTTP_REQUEST);
HttpServletResponse response = ctx.get(org.apache.struts2.StrutsStatics.HTTP_RESPONSE);
細心的朋友可以發現這里的session是個Map對象, 在Struts2中底層的session都被封裝成了Map類型. 我們可以直接操作這個Map對象進行對session的寫入和讀取操作, 而不用去直接操作HttpSession對象.
方法二:使用org.apache.struts2.ServletActionContext類
public class UserAction extends ActionSupport {

//其他代碼片段

private HttpServletRequest req;
// private HttpServletRequest req = ServletActionContext.getRequest(); 這條語句放在這個位置是錯誤的,同樣把這條語句放在構造方法中也是錯誤的。
public String login() {
req = ServletActionContext.getRequest(); //req的獲得必須在具體的方法中實現
user = new User();
user.setUid(uid);
user.setPassword(password);
if (userDAO.isLogin(user)) {
req.getSession().setAttribute("user", user);
return SUCCESS;
}
return LOGIN;
}
public String queryAll() {
req = ServletActionContext.getRequest(); //req的獲得必須在具體的方法中實現
uList = userDAO.queryAll();
req.getSession().setAttribute("uList", uList);
return SUCCESS;
}

//其他代碼片段
}
(2)IoC方式(即使用Struts2 Aware攔截器)
要使用IoC方式,我們首先要告訴IoC容器(Container)想取得某個對象的意願,通過實現相應的介面做到這點。
public class UserAction extends ActionSupport implements SessionAware, ServletRequestAware, ServletResponseAware {
private HttpServletRequest request;
private HttpServletResponse response;
public void setServletRequest(HttpServletRequest request) {
this.request = request;
}
public void setServletResponse(HttpServletResponse response) {
this.response = response;
}
public String execute() {
HttpSession session = request.getSession();
return SUCCESS;
}
}

Ⅱ java中Request對象的主要方法有哪些

答:setAttribute(Stringname,Object):設置名字為name的request的參數值

getAttribute(Stringname):返回由name指定的屬性值

getAttributeNames():返回request對象所有屬性的名字集合,結果是一個枚舉的實例

getCookies():返回客戶端的所有Cookie對象,結果是一個Cookie數組

getCharacterEncoding():返回請求中的字元編碼方式

getContentLength():返回請求的Body的長度

getHeader(Stringname):獲得HTTP協議定義的文件頭信息

getHeaders(Stringname):返回指定名字的requestHeader的所有值,結果是一個枚舉的實例

getHeaderNames():返回所以requestHeader的名字,結果是一個枚舉的實例

getInputStream():返回請求的輸入流,用於獲得請求中的數據

getMethod():獲得客戶端向伺服器端傳送數據的方法

getParameter(Stringname):獲得客戶端傳送給伺服器端的有name指定的參數值

getParameterNames():獲得客戶端傳送給伺服器端的所有參數的名字,結果是一個枚舉的實例

getParameterValues(Stringname):獲得有name指定的參數的所有值

getProtocol():獲取客戶端向伺服器端傳送數據所依據的協議名稱

getQueryString():獲得查詢字元串

getRequestURI():獲取發出請求字元串的客戶端地址

getRemoteAddr():獲取客戶端的IP地址

getRemoteHost():獲取客戶端的名字

getSession([Booleancreate]):返回和請求相關Session

getServerName():獲取伺服器的名字

getServletPath():獲取客戶端所請求的腳本文件的路徑

getServerPort():獲取伺服器的埠號

removeAttribute(Stringname):刪除請求中的一個屬性

Ⅲ java 怎麼獲取request url參數

String value = request.getParameter("key");
//地址蘭穿的參數刑辱sss?name=zs&&age=10;這樣的話key就是name或者age就可以獲取zs或者10

Ⅳ java怎樣獲取url參數

String url = request.getScheme()+"://"; //請求協議 http 或 https
url+=request.getHeader("host"); // 請求伺服器
url+=request.getRequestURI(); // 工程名
if(request.getQueryString()!=null) //判斷請求參數是否為空
url+="?"+request.getQueryString(); // 參數

閱讀全文

與javarequest獲取參數相關的資料

熱點內容
dvd光碟存儲漢子演算法 瀏覽:758
蘋果郵件無法連接伺服器地址 瀏覽:963
phpffmpeg轉碼 瀏覽:672
長沙好玩的解壓項目 瀏覽:145
專屬學情分析報告是什麼app 瀏覽:564
php工程部署 瀏覽:833
android全屏透明 瀏覽:737
阿里雲伺服器已開通怎麼辦 瀏覽:803
光遇為什麼登錄時伺服器已滿 瀏覽:302
PDF分析 瀏覽:486
h3c光纖全工半全工設置命令 瀏覽:143
公司法pdf下載 瀏覽:383
linuxmarkdown 瀏覽:350
華為手機怎麼多選文件夾 瀏覽:683
如何取消命令方塊指令 瀏覽:350
風翼app為什麼進不去了 瀏覽:779
im4java壓縮圖片 瀏覽:362
數據查詢網站源碼 瀏覽:151
伊克塞爾文檔怎麼進行加密 瀏覽:893
app轉賬是什麼 瀏覽:163