在Action中获取JSP内置对象

要在Action获取Request,Response,session等JSP内置对象,我们可以实现XXXAware接口。

代码如下:

package com.test.action;

import java.net.URLEncoder;
import java.util.Map;

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

import org.apache.struts2.interceptor.ApplicationAware;
//import org.apache.struts2.interceptor.CookiesAware;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import org.apache.struts2.interceptor.SessionAware;

public class LoginAction implements ServletResponseAware, ServletRequestAware,
		SessionAware,ApplicationAware {

	private String username;
	private String password;
	private HttpServletResponse response;
	private HttpServletRequest request;
	private Map session;
	private Map application;

	public void setSession(Map session) {
		this.session = session;
	}

	public void setServletResponse(HttpServletResponse response) {
		this.response = response;
	}

	public void setServletRequest(HttpServletRequest request) {
		this.request = request;
	}

	public void setApplication(Map application) {
		this.application = application;
	}
	
	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;
	}

	public String execute() throws Exception {
		
		request.setAttribute("username", username);
		request.setAttribute("password", password);
		
		session.put("username", username);
		session.put("password", password);
		
		application.put("username", username);
		application.put("password", password);
		
		//编码
		Cookie cookie = new Cookie("info",URLEncoder.encode(username+":"+password));
		cookie.setMaxAge(60*60);
		response.setCharacterEncoding("GBk");
		response.addCookie(cookie);
			
		return "success";
	}
}

 然后我们可以写一个测试页面来观察结果:

 result.jsp:

<%@ page language="java" import="java.util.*" pageEncoding="GBK"%>
<%@ page import="java.net.URLEncoder"%>
<%@ page import="java.net.URLDecoder"%>
<%@ taglib prefix="s" uri="/struts-tags"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
	<head>

		<title>My JSP 'result.jsp' starting page</title>

	</head>

	<body>
		采用Struts2标签库输出:<br>
		username:<s:property value="username"/>
		<br>
		password:<s:property value="password" />
		<br>
		<hr>
		采用request输出:<br>
		username:<%=(String)(request.getAttribute("username")) %><br>
		password:<%=(String)(request.getAttribute("password")) %>
		<hr>
		采用session输出:<br>
		username:<%=(String)(request.getSession().getAttribute("username")) %><br>
		password:<%=(String)(request.getSession().getAttribute("password")) %>
		<hr>
		采用application输出:<br>
		username:<%=(String)(application.getAttribute("username")) %><br>
		password:<%=(String)(application.getAttribute("password")) %>
		<hr>
		采用cookie输出:<br>
		<% 
		Cookie cookies[] = request.getCookies();
		for(int i=0;i<cookies.length;i++) {
			if(cookies[i].getName().equals("info")){
		%>
		<!-- 解码 -->
		info:<%=URLDecoder.decode(cookies[i].getValue())%><br>
		<% }}%>
		<hr>
		
		<s:a href="delCookie.jsp">删除cookie</s:a>
	</body>
</html>

 

你可能感兴趣的:(apache,jsp,.net,servlet,struts)