strtus2 web资源获取

拦截器获取web资源模式


方式一:使用ServletRequestAware拦截器

FirstAction.java

<span style="font-size:18px;"><span style="font-size:18px;">package action;

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

import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import org.apache.struts2.util.ServletContextAware;

import com.opensymphony.xwork2.ActionSupport;

public class FirstAction extends ActionSupport implements ServletRequestAware,ServletResponseAware,ServletContextAware{
	private HttpServletRequest request;
	private HttpServletResponse response;
	private ServletContext context;
	@Override
	public String execute() throws Exception {
		String username=this.request.getParameter("username");
		String password=this.request.getParameter("password");
		System.out.println(username+password);
		return "success";
	}

	@Override
	public void setServletContext(ServletContext arg0) {
		this.context=arg0;
		
	}

	@Override
	public void setServletResponse(HttpServletResponse arg0) {

		this.response=arg0;
	}

	@Override
	public void setServletRequest(HttpServletRequest arg0) {

		this.request=arg0;
	}
	

}</span>
</span>


struts.xml
<span style="font-size:18px;"><span style="font-size:18px;"><?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
<package name="action" extends="struts-default">
<action name="firstAction" class="action.FirstAction">
<result name="success">/index.jsp</result>
</action>
</package>

</struts>    
</span></span>


index.jsp

<span style="font-size:18px;"><span style="font-size:18px;"><%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%
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>My JSP 'index.jsp' starting page</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">
	-->
  </head>
  
  <body>
  <form action="<%=path%>/firstAction.action" method="post">
      username: <input type="text" name="username">
      password: <input type="password" name="password">
      <button type="submit">submit</button>

  </form>
  </body>
</html></span>
</span>

方式二:使用RequestAware拦截器

SecondAction.java

<span style="font-size:18px;"><span style="font-size:18px;">package action;

import java.util.Map;

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

import org.apache.struts2.StrutsStatics;
import org.apache.struts2.interceptor.RequestAware;

import com.opensymphony.xwork2.ActionSupport;

public class SecondAction extends ActionSupport implements RequestAware {
	private HttpServletRequest request;
	private HttpServletResponse response;
	private ServletContext context;
	@Override
	public void setRequest(Map<String, Object> arg0) {
		request=(HttpServletRequest)arg0.get(StrutsStatics.HTTP_REQUEST);
		response=(HttpServletResponse)arg0.get(StrutsStatics.HTTP_RESPONSE);
		context=(ServletContext)arg0.get(StrutsStatics.SERVLET_CONTEXT);
	}

	@Override
	public String execute() throws Exception {
		String username=request.getParameter("username");
		String password=request.getParameter("password");
		System.out.println(username+password);
		return "success";
	}
	

}
</span></span>


静态对象获取web资源模式

使用Struts2内置静态对象ActionContext

ThirdAction.java

<span style="font-size:18px;">package action;

import javax.servlet.ServletContext;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

import org.apache.struts2.ServletActionContext;

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

public class ThirdAction extends ActionSupport {
	
	
	@Override
	public String execute() throws Exception {
		ActionContext context=ActionContext.getContext();
		ServletRequest request=(ServletRequest) context.get(ServletActionContext.HTTP_REQUEST);
		ServletResponse response=(ServletResponse) context.get(ServletActionContext.HTTP_RESPONSE);
		ServletContext servletContext=(ServletContext) context.get(ServletActionContext.SERVLET_CONTEXT);
		System.out.println(request.getParameter("username")+request.getParameter("password"));
		
		
		return "success";
	}
	

}
</span>


使用Struts2内置静态对象ServletActionContext

<span style="font-size:18px;">package action;

import javax.servlet.ServletContext;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

public class FourthAction extends ActionSupport {

	@Override
	public String execute() throws Exception {
		// TODO Auto-generated method stub
		ServletRequest request=ServletActionContext.getRequest();
		ServletResponse response=ServletActionContext.getResponse();
		ServletContext context=ServletActionContext.getServletContext();
		System.out.println(request.getParameter("username")+request.getParameter("password"));
		return "success";
	}
	

}
</span>


登陆实例

LoginAction.java

package action;

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

import org.apache.struts2.ServletActionContext;

import bean.User;

import com.opensymphony.xwork2.ActionSupport;

public class LoginAction extends ActionSupport {
	private String username;
	private String password;
	
	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	@Override
	public String execute() throws Exception {
		if(username.equals("admin")&&password.equals("helloworld")){
			User user=new User();
			user.setUsername(username);
			user.setPassword(password);
			HttpServletRequest request=ServletActionContext.getRequest();
			HttpSession session=request.getSession();
			session.setAttribute("user", user);
			return "success";
		}else{
			return "failed";
		}
		
	}
	

}


success.jsp


<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%@ page import="bean.User" %>
<%
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%>">
    
    

  </head>
  
  <body>
    <% User user=(User)session.getAttribute("user"); %>
    welcome: <%=user.getUsername() %>!
    
  </body>
</html>


struts.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
<package name="action" extends="struts-default">
<action name="loginAction" class="action.LoginAction">
<result name="success">/success.jsp</result>
<result name="failed">/index.jsp</result>
</action>

</package>

</struts>  

application ServletContext 服务器对象 只要服务器不关闭 则该对象始终存在

存储在服务器内存中的对象,因此一般的对象不能保存在服务器中,因为容易导致服务器内存溢出而宕机

具体的使用场景可以是:驾校的考试系统,只需要注册用户就可以免费使用,用户量庞大

每次只出现一个题,然后做完本道题,自动跳转到下一个道题

实际的原理是:程序启动时,立即从数据库中取出需要获取的内容,放置到服务器application对象中,



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值