Struts2登陆的Demo

原创 2015年07月08日 21:37:10

通过Struts2实现登陆的小案例
这里写图片描述

源码下载地址:https://coding.net/u/gxs1225/p/Struts2_Login/git

代码如下:

struts.xml

<code class="hljs xml has-numbering"><span class="hljs-pi"><?xml version="1.0" encoding="UTF-8" ?></span>
<span class="hljs-doctype"><!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd"></span>
<span class="hljs-tag"><<span class="hljs-title">struts</span>></span>

   <span class="hljs-tag"><<span class="hljs-title">package</span> <span class="hljs-attribute">name</span>=<span class="hljs-value">"mypackage"</span> <span class="hljs-attribute">namespace</span>=<span class="hljs-value">""</span> <span class="hljs-attribute">extends</span>=<span class="hljs-value">"struts-default"</span>></span>
      <span class="hljs-tag"><<span class="hljs-title">action</span> <span class="hljs-attribute">name</span>=<span class="hljs-value">"login"</span> <span class="hljs-attribute">class</span>=<span class="hljs-value">"com.bzu.gxs.LoginAction"</span>></span>
         <span class="hljs-tag"><<span class="hljs-title">result</span> <span class="hljs-attribute">name</span>=<span class="hljs-value">"success"</span>></span>success.jsp<span class="hljs-tag"></<span class="hljs-title">result</span>></span>
         <span class="hljs-tag"><<span class="hljs-title">result</span> <span class="hljs-attribute">name</span>=<span class="hljs-value">"error"</span>></span>error.jsp<span class="hljs-tag"></<span class="hljs-title">result</span>></span>
      <span class="hljs-tag"></<span class="hljs-title">action</span>></span>
   <span class="hljs-tag"></<span class="hljs-title">package</span>></span>

<span class="hljs-tag"></<span class="hljs-title">struts</span>></span></code>
<code class="hljs xml has-numbering"><span class="hljs-tag"></span></code> 
<code class="hljs xml has-numbering"><span class="hljs-tag"></span></code> 
LoginAction.java
<code class="hljs java has-numbering"><span class="hljs-keyword">package</span> com.bzu.gxs;

<span class="hljs-keyword">import</span> com.sun.net.httpserver.Authenticator.Success;

<span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">LoginAction</span> {</span>
    <span class="hljs-keyword">private</span> String uname;

    <span class="hljs-keyword">private</span> String upass;

    <span class="hljs-keyword">public</span> String <span class="hljs-title">getUname</span>() {
        <span class="hljs-keyword">return</span> uname;
    }

    <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">setUname</span>(String uname) {
        <span class="hljs-keyword">this</span>.uname = uname;
    }

    <span class="hljs-keyword">public</span> String <span class="hljs-title">getUpass</span>() {
        <span class="hljs-keyword">return</span> upass;
    }

    <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">setUpass</span>(String upass) {
        <span class="hljs-keyword">this</span>.upass = upass;
    }

    <span class="hljs-comment">// 默认执行action操作</span>
    <span class="hljs-keyword">public</span> String <span class="hljs-title">execute</span>(){

        <span class="hljs-keyword">if</span>(getUname().equals(<span class="hljs-string">"gxs"</span>) && getUpass().equals(<span class="hljs-string">"123"</span>)){
            <span class="hljs-keyword">return</span> <span class="hljs-string">"success"</span>;
        }<span class="hljs-keyword">else</span>{
            <span class="hljs-keyword">return</span> <span class="hljs-string">"error"</span>;
        }
    }


}
</code>

 

login.jsp
<code class="hljs xml has-numbering"><span class="vbscript"><%@ page language=<span class="hljs-string">"java"</span> import=<span class="hljs-string">"java.util.*"</span> pageEncoding=<span class="hljs-string">"gbk"</span>%></span>
<span class="vbscript"><%
<span class="hljs-built_in">String</span> path = <span class="hljs-built_in">request</span>.getContextPath();
<span class="hljs-built_in">String</span> basePath = <span class="hljs-built_in">request</span>.getScheme()+<span class="hljs-string">"://"</span>+<span class="hljs-built_in">request</span>.getServerName()+<span class="hljs-string">":"</span>+<span class="hljs-built_in">request</span>.getServerPort()+path+<span class="hljs-string">"/"</span>;
%></span>

<span class="hljs-doctype"><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"></span>
<span class="hljs-tag"><<span class="hljs-title">html</span>></span>
  <span class="hljs-tag"><<span class="hljs-title">head</span>></span>
    <span class="hljs-tag"><<span class="hljs-title">base</span> <span class="hljs-attribute">href</span>=<span class="hljs-value">"<%=basePath%>"</span>></span>

    <span class="hljs-tag"><<span class="hljs-title">title</span>></span>My JSP 'login.jsp' starting page<span class="hljs-tag"></<span class="hljs-title">title</span>></span>

    <span class="hljs-tag"><<span class="hljs-title">meta</span> <span class="hljs-attribute">http-equiv</span>=<span class="hljs-value">"pragma"</span> <span class="hljs-attribute">content</span>=<span class="hljs-value">"no-cache"</span>></span>
    <span class="hljs-tag"><<span class="hljs-title">meta</span> <span class="hljs-attribute">http-equiv</span>=<span class="hljs-value">"cache-control"</span> <span class="hljs-attribute">content</span>=<span class="hljs-value">"no-cache"</span>></span>
    <span class="hljs-tag"><<span class="hljs-title">meta</span> <span class="hljs-attribute">http-equiv</span>=<span class="hljs-value">"expires"</span> <span class="hljs-attribute">content</span>=<span class="hljs-value">"0"</span>></span>    
    <span class="hljs-tag"><<span class="hljs-title">meta</span> <span class="hljs-attribute">http-equiv</span>=<span class="hljs-value">"keywords"</span> <span class="hljs-attribute">content</span>=<span class="hljs-value">"keyword1,keyword2,keyword3"</span>></span>
    <span class="hljs-tag"><<span class="hljs-title">meta</span> <span class="hljs-attribute">http-equiv</span>=<span class="hljs-value">"description"</span> <span class="hljs-attribute">content</span>=<span class="hljs-value">"This is my page"</span>></span>

  <span class="hljs-tag"></<span class="hljs-title">head</span>></span>

  <span class="hljs-tag"><<span class="hljs-title">body</span>></span>
    <span class="hljs-tag"><<span class="hljs-title">form</span> <span class="hljs-attribute">name</span>=<span class="hljs-value">"frm"</span> <span class="hljs-attribute">action</span>=<span class="hljs-value">"login.action"</span> <span class="hljs-attribute">method</span>=<span class="hljs-value">"post"</span>></span>
        <span class="hljs-tag"><<span class="hljs-title">table</span> <span class="hljs-attribute">align</span>=<span class="hljs-value">"center"</span> <span class="hljs-attribute">border</span>=<span class="hljs-value">"1"</span>></span>

              <span class="hljs-tag"><<span class="hljs-title">tr</span>></span>
                <span class="hljs-tag"><<span class="hljs-title">td</span>></span>用户名:<span class="hljs-tag"></<span class="hljs-title">td</span>></span>
                <span class="hljs-tag"><<span class="hljs-title">td</span>></span><span class="hljs-tag"><<span class="hljs-title">input</span> <span class="hljs-attribute">type</span>=<span class="hljs-value">"text"</span> <span class="hljs-attribute">id</span>=<span class="hljs-value">"uname"</span> <span class="hljs-attribute">name</span>=<span class="hljs-value">"uname"</span>/></span><span class="hljs-tag"></<span class="hljs-title">td</span>></span>
              <span class="hljs-tag"></<span class="hljs-title">tr</span>></span>

              <span class="hljs-tag"><<span class="hljs-title">tr</span>></span>
                <span class="hljs-tag"><<span class="hljs-title">td</span>></span>密码:<span class="hljs-tag"></<span class="hljs-title">td</span>></span>
                <span class="hljs-tag"><<span class="hljs-title">td</span>></span><span class="hljs-tag"><<span class="hljs-title">input</span> <span class="hljs-attribute">type</span>=<span class="hljs-value">"password"</span> <span class="hljs-attribute">id</span>=<span class="hljs-value">"upass"</span> <span class="hljs-attribute">name</span>=<span class="hljs-value">"upass"</span>/></span><span class="hljs-tag"></<span class="hljs-title">td</span>></span>
              <span class="hljs-tag"></<span class="hljs-title">tr</span>></span>

              <span class="hljs-tag"><<span class="hljs-title">tr</span>></span>
                <span class="hljs-tag"><<span class="hljs-title">td</span> <span class="hljs-attribute">align</span>=<span class="hljs-value">"center"</span> <span class="hljs-attribute">colspan</span>=<span class="hljs-value">"2"</span>></span>
                <span class="hljs-tag"><<span class="hljs-title">input</span> <span class="hljs-attribute">type</span>=<span class="hljs-value">"submit"</span> <span class="hljs-attribute">value</span>=<span class="hljs-value">"登陆"</span> /></span>
                <span class="hljs-tag"><<span class="hljs-title">input</span> <span class="hljs-attribute">type</span>=<span class="hljs-value">"reset"</span>  <span class="hljs-attribute">value</span>=<span class="hljs-value">"重置"</span>></span>
                <span class="hljs-tag"></<span class="hljs-title">td</span>></span>
              <span class="hljs-tag"></<span class="hljs-title">tr</span>></span>

        <span class="hljs-tag"></<span class="hljs-title">table</span>></span>
    <span class="hljs-tag"></<span class="hljs-title">form</span>></span>
    <span class="hljs-tag"></<span class="hljs-title">br</span>></span>
    <span class="hljs-tag"><<span class="hljs-title">hr</span>></span>
    注意:<span class="hljs-tag"></<span class="hljs-title">br</span>></span>
    测试的用户名:gxs<span class="hljs-tag"></<span class="hljs-title">br</span>></span>
    测试的 密  码:123
  <span class="hljs-tag"></<span class="hljs-title">body</span>></span>
<span class="hljs-tag"></<span class="hljs-title">html</span>></span>
</code>
success.jsp
<code class="hljs xml has-numbering"><span class="vbscript"><%@ page language=<span class="hljs-string">"java"</span> import=<span class="hljs-string">"java.util.*"</span> pageEncoding=<span class="hljs-string">"gbk"</span>%></span>
<span class="vbscript"><%
<span class="hljs-built_in">String</span> path = <span class="hljs-built_in">request</span>.getContextPath();
<span class="hljs-built_in">String</span> basePath = <span class="hljs-built_in">request</span>.getScheme()+<span class="hljs-string">"://"</span>+<span class="hljs-built_in">request</span>.getServerName()+<span class="hljs-string">":"</span>+<span class="hljs-built_in">request</span>.getServerPort()+path+<span class="hljs-string">"/"</span>;
%></span>

<span class="hljs-doctype"><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"></span>
<span class="hljs-tag"><<span class="hljs-title">html</span>></span>
  <span class="hljs-tag"><<span class="hljs-title">head</span>></span>
    <span class="hljs-tag"><<span class="hljs-title">base</span> <span class="hljs-attribute">href</span>=<span class="hljs-value">"<%=basePath%>"</span>></span>

    <span class="hljs-tag"><<span class="hljs-title">title</span>></span>My JSP 'success.jsp' starting page<span class="hljs-tag"></<span class="hljs-title">title</span>></span>

    <span class="hljs-tag"><<span class="hljs-title">meta</span> <span class="hljs-attribute">http-equiv</span>=<span class="hljs-value">"pragma"</span> <span class="hljs-attribute">content</span>=<span class="hljs-value">"no-cache"</span>></span>
    <span class="hljs-tag"><<span class="hljs-title">meta</span> <span class="hljs-attribute">http-equiv</span>=<span class="hljs-value">"cache-control"</span> <span class="hljs-attribute">content</span>=<span class="hljs-value">"no-cache"</span>></span>
    <span class="hljs-tag"><<span class="hljs-title">meta</span> <span class="hljs-attribute">http-equiv</span>=<span class="hljs-value">"expires"</span> <span class="hljs-attribute">content</span>=<span class="hljs-value">"0"</span>></span>    
    <span class="hljs-tag"><<span class="hljs-title">meta</span> <span class="hljs-attribute">http-equiv</span>=<span class="hljs-value">"keywords"</span> <span class="hljs-attribute">content</span>=<span class="hljs-value">"keyword1,keyword2,keyword3"</span>></span>
    <span class="hljs-tag"><<span class="hljs-title">meta</span> <span class="hljs-attribute">http-equiv</span>=<span class="hljs-value">"description"</span> <span class="hljs-attribute">content</span>=<span class="hljs-value">"This is my page"</span>></span>

  <span class="hljs-tag"></<span class="hljs-title">head</span>></span>

  <span class="hljs-tag"><<span class="hljs-title">body</span>></span>
    <span class="hljs-tag"><<span class="hljs-title">h1</span>></span>查询成功!<span class="hljs-tag"></<span class="hljs-title">h1</span>></span>
  <span class="hljs-tag"></<span class="hljs-title">body</span>></span>

<span class="hljs-tag"></<span class="hljs-title">html</span>></span>
</code>

 

error.jsp
<code class="hljs xml has-numbering"><span class="vbscript"><%@ page language=<span class="hljs-string">"java"</span> import=<span class="hljs-string">"java.util.*"</span> pageEncoding=<span class="hljs-string">"gbk"</span>%></span>
<span class="vbscript"><%
<span class="hljs-built_in">String</span> path = <span class="hljs-built_in">request</span>.getContextPath();
<span class="hljs-built_in">String</span> basePath = <span class="hljs-built_in">request</span>.getScheme()+<span class="hljs-string">"://"</span>+<span class="hljs-built_in">request</span>.getServerName()+<span class="hljs-string">":"</span>+<span class="hljs-built_in">request</span>.getServerPort()+path+<span class="hljs-string">"/"</span>;
%></span>

<span class="hljs-doctype"><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"></span>
<span class="hljs-tag"><<span class="hljs-title">html</span>></span>
  <span class="hljs-tag"><<span class="hljs-title">head</span>></span>
    <span class="hljs-tag"><<span class="hljs-title">base</span> <span class="hljs-attribute">href</span>=<span class="hljs-value">"<%=basePath%>"</span>></span>

    <span class="hljs-tag"><<span class="hljs-title">title</span>></span>My JSP 'error.jsp' starting page<span class="hljs-tag"></<span class="hljs-title">title</span>></span>

    <span class="hljs-tag"><<span class="hljs-title">meta</span> <span class="hljs-attribute">http-equiv</span>=<span class="hljs-value">"pragma"</span> <span class="hljs-attribute">content</span>=<span class="hljs-value">"no-cache"</span>></span>
    <span class="hljs-tag"><<span class="hljs-title">meta</span> <span class="hljs-attribute">http-equiv</span>=<span class="hljs-value">"cache-control"</span> <span class="hljs-attribute">content</span>=<span class="hljs-value">"no-cache"</span>></span>
    <span class="hljs-tag"><<span class="hljs-title">meta</span> <span class="hljs-attribute">http-equiv</span>=<span class="hljs-value">"expires"</span> <span class="hljs-attribute">content</span>=<span class="hljs-value">"0"</span>></span>    
    <span class="hljs-tag"><<span class="hljs-title">meta</span> <span class="hljs-attribute">http-equiv</span>=<span class="hljs-value">"keywords"</span> <span class="hljs-attribute">content</span>=<span class="hljs-value">"keyword1,keyword2,keyword3"</span>></span>
    <span class="hljs-tag"><<span class="hljs-title">meta</span> <span class="hljs-attribute">http-equiv</span>=<span class="hljs-value">"description"</span> <span class="hljs-attribute">content</span>=<span class="hljs-value">"This is my page"</span>></span>

  <span class="hljs-tag"></<span class="hljs-title">head</span>></span>

  <span class="hljs-tag"><<span class="hljs-title">body</span>></span>
    登陆失败,请重新 <span class="hljs-tag"><<span class="hljs-title">a</span> <span class="hljs-attribute">href</span>=<span class="hljs-value">"login.jsp"</span>></span>登陆<span class="hljs-tag"></<span class="hljs-title">a</span>></span>
  <span class="hljs-tag"></<span class="hljs-title">body</span>></span>
<span class="hljs-tag"></<span class="hljs-title">html</span>></span>
</code>
    
版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

struts2入门例子 登陆demo!非常详细的图解

1.下载所需的jar包 下载地址:https://struts.apache.org/download.cgi#struts25101

Struts2登陆的Demo

通过Struts2实现登陆的小案例 源码下载地址:https://coding.net/u/gxs1225/p/Struts2_Login/git代码如下:struts.xml <!DOCTYPE ...

Struts2实现登陆功能Demo

最近在学习SSH框架,通过用struts实现一个简单的登陆功能,来学习struts相关知识,着重了解了struts的配置。

struts2登陆

  • 2014-03-18 21:55
  • 24KB
  • 下载

struts2实现登陆

  • 2014-07-12 14:28
  • 3.26MB
  • 下载

运用struts2框架实现登陆注册

刚刚学完struts2框架,趁热打铁,做了个struts2MVC小项目练练手。主要用到了struts2的机制实现了登陆注册, 1.字符串到POJO类型的转换, 2.利用i18n拦截器实现了中英文国际化...

struts2注解登陆

  • 2015-08-26 22:46
  • 4.00MB
  • 下载

struts2简单登陆

  • 2012-06-11 13:03
  • 1.19MB
  • 下载

Struts2自定义拦截器,实现发表文章检查是否已登陆功能

老师布置了个
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)