您的位置:首页 > 其它

Action间接、直接访问Servlet API

2017-07-31 14:57 357 查看
一、间接访问:

需要经常访问的Servlet API就是HttpServletRequest 、HttpSession 、ServletContext三个类,分别代表着jsp内置对象的request、session、application

struts2 提供一个类ActionContext(当前Action上下文对象,通过这个类可以访问Servlet API。包含以下几个常用方法:

Map getApplication();

static ActionContext Context();

Map getParameters();

Map getSesstion();

void set Apptioncatoin(Map application);

案例1:用户登录验证:

longin2.jsp页面:

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib prefix = "s" uri = "/struts-tags" %>
<%
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 'login2.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>
<s:form action = "login2" method = "post">
<s:textfield name = "username" label = "用户名"></s:textfield>
<s:password name = "password" label = "密码"></s:password><br><br>
<s:submit value = "提交"></s:submit>
</s:form>
</body>
</html>


Action:LoginActoin2页面:

package com.action;

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

public class LoginAction2 extends ActionSupport{
private String username;
private String password;

public String execute() {
ActionContext actionContext = ActionContext.getContext(); //静态方法;
if(username.equals("admin") && password.equals("admin")){
actionContext.getSession().put("username", username);
actionContext.getSession().put("password", password);
return SUCCESS;
}
return LOGIN;
}

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;
}

}


welcome2.jsp页面:

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib prefix = "s" uri = "/struts-tags" %>
<%
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 'welcome2.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="
4000
description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->

</head>

<body>
欢迎<s:property value = "#session.username"/>访问此页面!
</body>
</html>


二、直接访问:

方式1):实现相应的接口

ServletContextAware : 实现该接口的Action可以直接访问ServletContext 对象,Action 必须提供该接口的void setServletContext(ServletContext servletcontext)方法

ServletRequestAware :

ServlerResponseAware:

SessionAware:

修改以上Action中的代码:

package com.action;

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

import org.apache.struts2.interceptor.ServletRequestAware;

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

public class LoginAction2 extends ActionSupport implements ServletRequestAware{
private String username;
private String password;
private HttpServletRequest servletRequest;

public String execute() {
HttpSession session = servletRequest.getSession();
if(username.equals("admin") && password.equals("admin")){
session.setAttribute("username", username);
session.setAttribute("password", password);
return SUCCESS;
}
return LOGIN;
}

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 HttpServletRequest getServletRequest() {
return servletRequest;
}

public void setServletRequest(HttpServletRequest servletRequest) {
this.servletRequest = servletRequest;
}//要使用HttpServletRequest对象,必须实现该方法

}


方式2)struts2提供了ServletActionContext类:(直接访问中最常用,代码简洁)

有以下几个静态方法:

static PageContext getPageContext() ;取得Web应用的PageContext对象

static HttpServletRequest getRequest();…………

static HttpServletResponse gerResponse;…………

static ServletContext geteServletContext();…………

修改Action代码:

public class LoginAction2 extends ActionSupport{
private String username;
private String password;

public String execute() {
if(username.equals("admin") && password.equals("admin")){
ServletActionContext.getRequest().getSession().setAttribute("username", username);
return SUCCESS;
}
return LOGIN;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: