Action直接访问Servlet API

一 Action控制器

package org.crazyit.app.action;

import com.opensymphony.xwork2.*;
import org.apache.struts2.interceptor.*;

import javax.servlet.http.*;

import java.util.Map;



public class LoginAction
    implements Action,ServletResponseAware
{
    private String username;
    private String password;
    private HttpServletResponse response;

    // 重写实现ServletResponseAware接口必须实现的方法
    public void setServletResponse(HttpServletResponse response)
    {
        this.response = response;
    }

    // username的setter和getter方法
    public void setUsername(String username)
    {
        this.username = username;
    }
    public String getUsername()
    {
        return this.username;
    }

    // password的setter和getter方法
    public void setPassword(String password)
    {
        this.password = password;
    }
    public String getPassword()
    {
        return this.password;
    }

    public String execute() throws Exception
    {
        ActionContext ctx = ActionContext.getContext();
        // 通过ActionContext访问application范围的属性值
        Integer counter = (Integer)ctx.getApplication()
            .get("counter");
        if (counter == null)
        {
            counter = 1;
        }
        else
        {
            counter = counter + 1;
        }
        // 通过ActionContext设置application范围的属性
        ctx.getApplication().put("counter" , counter);
        // 通过ActionContext设置session范围的属性
        ctx.getSession().put("user" , getUsername());
        if (getUsername().equals("crazyit.org")
            && getPassword().equals("leegang") )
        {
            // 通过response添加Cookie
            Cookie c = new Cookie("user" , getUsername());
            c.setMaxAge(60 * 60);
            response.addCookie(c);
            // 通过ActionContext设置request范围的属性
            ctx.put("tip" , "服务器提示:您已经成功的登录");
            return SUCCESS;
        }
        // 通过ActionContext设置request范围的属性
        ctx.put("tip" , "服务器提示:登录失败");
        return ERROR;
    }
}

二 配置文件

<?xml version="1.0" encoding="GBK"?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
    "http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
    <constant name="conststruts.devMode" value="true"/>
    <!-- Struts2的所有Action都需位于package下 -->
    <package name="lee" extends="struts-default">
        <!-- 定义名为login的Action,其实现类为LoginAction类 -->
        <action name="login" class="org.crazyit.app.action.LoginAction">
            <!-- 处理结果返回error,对应/WEB-INF/content/error.jsp视图资源 -->
            <result name="error">/WEB-INF/content/error.jsp</result>
            <!-- 处理结果返回success,对应/WEB-INF/content/welcome.jsp视图资源 -->
            <result>/WEB-INF/content/welcome.jsp</result>
        </action>
        <action name="*">
            <result>/WEB-INF/content/{1}.jsp</result>
        </action>
    </package>
</struts>

三 视图

1 loginForm.jsp

<%@ page contentType="text/html; charset=GBK" language="java" errorPage="" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>登录页面</title>
</head>
<body>
<form action="login.action" method="post">
<table align="center">
    <caption><h3>用户登录</h3></caption>
    <tr>
        <td>用户名:<input type="text" name="username"/></td>
    </tr>
    <tr>
        <td>密&nbsp;&nbsp;码:<input type="text" name="password"/></td>
    </tr>
    <tr align="center">
        <td><input type="submit" value="登录"/><input type="reset" value="重填" /></td>
    </tr>
</table>
</form>
</body>
</html>

2 welcome.jsp

<%@ page contentType="text/html; charset=GBK" language="java" errorPage="" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>成功页面</title>
</head>
<body>
    本站访问次数为:${applicationScope.counter}<br/>
    ${sessionScope.user},您已经登录!<br/>
    ${requestScope.tip}<br/>
    从系统读取Cookie值:${cookie.user.value}
</body>
</html>

3 error.jsp

<%@ page contentType="text/html; charset=GBK" language="java" errorPage="" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>错误页面</title>
</head>
<body>
    本站访问次数为:${applicationScope.counter}<br/>
    ${sessionScope.user},您不能登录!<br/>
    ${requestScope.tip}
</body>
</html>

四 测试

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值