struts2实现登录功能

工程结构

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1">
    <filter>
        <filter-name>struts2</filter-name>
        <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
</web-app>

struts.xml

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
        "http://struts.apache.org/dtds/struts-2.3.dtd">

<struts>
    <package name="login" extends="struts-default">
        <action name="Login" class="com.Action.LoginAction">
            <result name="success">/success.jsp</result>
            <result name="error">/error.jsp</result>
        </action>
    </package>
</struts>

LoginAction

package com.Action;

import com.opensymphony.xwork2.ActionSupport;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

public class LoginAction extends ActionSupport {
   private String username;
   private String userpass;

   public String getUsername() {
      return username;
   }

   public void setUsername(String username) {
      this.username = username;
   }

   public String getUserpass() {
      return userpass;
   }

   public void setUserpass(String userpass) {
      this.userpass = userpass;
   }

   @Override
   public String execute() throws Exception {
      String ret = ERROR;
      String userPwd = "";
      Connection conn = null;
      try {
         String URL = "jdbc:mysql://localhost/******";
         Class.forName("com.mysql.jdbc.Driver");
         conn = DriverManager.getConnection(URL, "username", "userpwd");
         String sql = "SELECT userPwd FROM tb_user WHERE userName = ?";
         PreparedStatement ps = conn.prepareStatement(sql);
         ps.setString(1, username);
         ResultSet rs = ps.executeQuery();
         while (rs.next()) {
            userPwd = rs.getString(1);
         }
         if (userPwd.equals(userpass)) {
            ret = SUCCESS;
         }
      } catch (Exception e) {
         e.printStackTrace();
      } finally {
         if (conn != null) {
            try {
               conn.close();
            } catch (Exception ignored) {
            }
         }
      }
      return ret;
   }

   public String login() throws Exception {
      return execute();
   }
}

index.jsp

<%@ taglib prefix="s" uri="/struts-tags" %>
<%--
  Created by IntelliJ IDEA.
  User: Elijah
  Date: 2018/9/26
  Time: 23:00
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>用户登录</title>
</head>
<body>
<s:form action="Login">
    <s:label value="登录信息"></s:label>
    <s:textfield name="username" label="username"></s:textfield>
    <s:password name="userpass" label="userpass"></s:password>
    <s:submit value="登录"></s:submit>
</s:form>
</body>
</html>

success.jsp

<%@ taglib prefix="s" uri="/struts-tags" %>
<%--
  Created by IntelliJ IDEA.
  User: Elijah
  Date: 2018/9/26
  Time: 23:10
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>登陆成功</title>
</head>
<body>
<s:property value="username"/>登陆成功<br/>
</body>
</html>

error.jsp

<%@ taglib prefix="s" uri="/struts-tags" %>
<%--
  Created by IntelliJ IDEA.
  User: Elijah
  Date: 2018/9/26
  Time: 23:20
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>登陆失败</title>
</head>
<body>
<s:property value="username"/>登陆失败
</body>
</html>

猜你喜欢

转载自blog.csdn.net/weixin_40330033/article/details/82861151
今日推荐