Struts2写一个登录验证程序

环境:MyEclipse10
第一步,创建一个Web项目,可命名为Struts2-2,右击项目MyEclipse->Add Struts Capabilities
选择Struts2.1,勾选*.action(代表Struts拦截器拦截后缀为action的请求)
点击/是过滤所有请求,这样会导致在浏览器中无法直接打开JSP文件*

第二步,在Struts.xml文件当中配置我们需要的请求,具体如下:
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>
    <constant name="struts.i18n.encoding" value="UTF-8"></constant>
    <constant name="struts.action.extension" value="action,do"></constant>
    <constant name="struts.serve.static.browserCache" value="false"></constant>
    <constant name="struts.configuration.xml.reload" value="true"></constant>
    <constant name="struts.devMode" value="true"></constant>
    <constant name="struts.ui.theme" value="simple"></constant>

    <package name="test" namespace="/new" extends="struts-default">
        <!-- action相当于以前的servlet的概念,对应一个请求  name为请求的url地址
            localhost:8080/项目名/new/login.do
         -->
        <action name="login" class="com.csdn.action.LoginAction">
            <result name="success">/success.jsp</result>
            <result name="fail">/fail.jsp</result>
        </action>
    </package>
</struts>  

Struts.xml文件非常关键,项目哪里出错应该优先看xml文件是否有错误

第三步,创建Servlet,即com.jikexueyuan.action.LoginAction类:

LoginAction类文件:

package com.csdn.action;

public class LoginAction {
    private String username;
    private String password;

    public String execute() {
        if (username.equals("admin") && password.equals("123")) {
            return "success";
        } else {
            return "fail";
        }
    }

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

}

第四步,写index.jsp并创建fail.jsp和success.jsp文件
分别为:
index.jsp文件:

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
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 %>/new/login.action" method="post">
        用户名:<input type="text" name="username"><br>
        密码:<input type="text" name="password"><br>
        <input type="submit" value="提交">
    </form>
  </body>
</html>

succes.jsp文件

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
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>
  </head>

  <body>
    登陆成功!
  </body>
</html>

fail.jsp文件:

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
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>
  </head>

  <body>
    登录失败!
  </body>
</html>

第五步,验证项目,将项目部署到tomcat服务器,具体方法,选择项目根目录,点击工具栏Deploy MyEclipse J2EE Project to Server,在弹出的菜单点击add,菜单中选择tomcat6.0,点击OK即可。
打开浏览器,输入http://loaclhost/Struts2-2,在文本框输入admin,123,可以成功登录,反之失败。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值