struts2 access web elements
10144 단어 struts2
1. Access request, session, application objects through ActionContext
2. Access request, session, application objects by implementing RequestAware, SessionAware, and ApplicationAware interfaces
3. Access request, session, application objects through ServletActionContext
4. Access request, session, application objects by implementing the ServletRequestAware interface
Demo code:
method one:
/**
* ActionContext request,session,application
* @author
*/
public class UserAction1 extends ActionSupport{
/**
*
*/
private static final long serialVersionUID = 1L;
/**
* request
*/
private Map <String,Object>request;
/**
* response
*/
private Map <String,Object>session;
/**
* application
*/
private Map <String,Object>application;
/**
*
* @return
*/
@SuppressWarnings("unchecked")
@Override
public String execute(){
System.out.println(" ActionContext request,session,application ");
//
request = (Map<String,Object>)ActionContext.getContext().get("request");
session = ActionContext.getContext().getSession();
application = ActionContext.getContext().getApplication();
//
request.put("requestKey", "requestValue");
session.put("sessionKey", "sessionValue");
application.put("applicationKey", "applicationValue");
return "success";
}
}
Method two:/**
* RequestAware、SessionAware、ApplicationAware request,session,application
* @author
*/
public class UserAction2 extends ActionSupport implements RequestAware,SessionAware,ApplicationAware{
/**
*
*/
private static final long serialVersionUID = 1L;
/**
* request
*/
private Map <String,Object>request;
/**
* response
*/
private Map <String,Object>session;
/**
* application
*/
private Map <String,Object>application;
/**
*
*/
@SuppressWarnings("unchecked")
@Override
public String execute(){
System.out.println(" RequestAware、SessionAware、ApplicationAware request,session,application ");
//
request.put("requestKey", "requestValue");
session.put("sessionKey", "sessionValue");
application.put("applicationKey", "applicationValue");
return "success";
}
/*
* RequestAware
*/
@Override
public void setRequest(Map<String, Object> request) {
this.request = request;
}
/*
* ApplicationAware
*/
@Override
public void setApplication(Map<String, Object> application) {
this.application = application;
}
/*
* SessionAware
*/
@Override
public void setSession(Map<String, Object> session) {
this.session = session;
}
}
Method three:+/**
* ServletActionContext request,session,application
* @author
*/
public class UserAction3 extends ActionSupport{
/**
*
*/
private static final long serialVersionUID = 1L;
/**
* request
*/
private HttpServletRequest request;
/**
* response
*/
private HttpSession session;
/**
* application
*/
private ServletContext application;
/**
*
*/
@SuppressWarnings("unchecked")
@Override
public String execute(){
System.out.println(" ServletActionContext request,session,application ");
//
request = ServletActionContext.getRequest();
session = request.getSession();
application = session.getServletContext();
//
request.setAttribute("requestKey", "requestValue");
session.setAttribute("sessionKey", "sessionValue");
application.setAttribute("applicationKey", "applicationValue");
return "success";
}
}
Method four:/**
* ServletRequestAware request,session,application
* @author
*/
public class UserAction4 extends ActionSupport implements ServletRequestAware{
/**
*
*/
private static final long serialVersionUID = 1L;
/**
* request
*/
private HttpServletRequest request;
/**
* response
*/
private HttpSession session;
/**
* application
*/
private ServletContext application;
/**
*
*/
@SuppressWarnings("unchecked")
@Override
public String execute(){
System.out.println(" ServletRequestAware request,session,application ");
//
request.setAttribute("requestKey", "requestValue");
session.setAttribute("sessionKey", "sessionValue");
application.setAttribute("applicationKey", "applicationValue");
return "success";
}
/*
* ServletRequestAware
*/
@Override
public void setServletRequest(HttpServletRequest request) {
this.request = request;
this.session = request.getSession();
this.application = session.getServletContext();
}
}
The above action supporting struts.xml and jsp pagestruts.xml
<struts>
<!-- : -->
<constant name="struts.devMode" value="true"/>
<package name="" namespace="/login" extends="struts-default">
<action name="login*" class="com.wj.struts2.action.UserAction{1}">
<result name="success">/success.jsp</result>
<result name="failure">/failure.jsp</result>
</action>
</package>
</struts>
index.jsp<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@taglib uri="/struts-tags" prefix="s" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>Struts2_AccessWebElements</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css" mce_href="styles.css">
-->
<mce:script language="javascript"><!--
function sub(str){
document.form1.action = str;
document.form1.submit();
}
// --></mce:script>
</head>
<body>
<form name="form1">
<div>
Struts2 web <br>
:<input type="button" value="submit1" onclick="sub('<%=basePath%>login/login1')"><br>
:<input type="button" value="submit2" onclick="sub('<%=basePath%>login/login2')"><br>
:<input type="button" value="submit3" onclick="sub('<%=basePath%>login/login3')"><br>
:<input type="button" value="submit4" onclick="sub('<%=basePath%>login/login4')"><br>
</div>
</form>
</body>
</html>
success.jsp<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@taglib uri="/struts-tags" prefix="s" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>Struts2_AccessWebElements</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css" mce_href="styles.css">
-->
</head>
<body>
requestKey---<s:property value="#request.requestKey"/>|<%=request.getAttribute("requestKey")%><br>
sessionKey---<s:property value="#session.sessionKey"/>|<%=session.getAttribute("sessionKey")%><br>
applicationKey---<s:property value="#application.applicationKey"/>|<%=application.getAttribute("applicationKey")%><br>
--------------------------------------------
<s:debug></s:debug>
</body>
</html>
How to get the background value of the front jsp pageReference link:
http://blog.CSDN.net/what to do 653983/article/details/8039292
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
apache struts2 취약점 검증이번에는 보안 캠프의 과제였던 apache struts2의 취약성에 대해 실제로 손을 움직여 실행해 보고 싶습니다. 환경 VirtualBox에서 브리지 어댑터 사용 호스트:macOS 10.12 게스트:ubuntu 1...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.