• 设计简单登录界面(Java web)


    程序设计思想:

       在Input.jsp中创建一个表格里边分别是课程名称,任课老师,教学地点,并分别用三个文本框来接受输入的三个属性,

    并传到另外的Jsp页面中,又来接受三个数据,并判断传来的教师,与教室地点是否符合题目要求,若不符合,则设置

    一个Error,返回到第一个JSP中,并显示在JSP中,允许重新输入,若符合要求,就想数据库中保存输入的是三个属性。

    程序源代码:

    连接数据库的源代码:

    package com.jaovo.msg.Util;
    
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    
    public class DBUtil {
        
        public  static  Connection getConnection() {
            try {
            
                Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver").newInstance();
            } catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            String user = "sa";
            String password = "123456";
            String url = "jdbc:sqlserver://127.0.0.1:1433;DatabaseName=jaovo_msg";
            Connection connection = null;
            try {
                
                 connection = DriverManager.getConnection(url,user,password);
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            return connection;
        }
        
    
        public static void close(Connection connection ) {
            try {
                if (connection != null) {
                    connection.close();
                }
                
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        public static void close(PreparedStatement preparedStatement ) {
            try {
                if (preparedStatement != null) {
                    preparedStatement.close();
                }
                
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        public static void close(ResultSet resultSet ) {
            try {
                if (resultSet != null) {
                    resultSet.close();
                }
                
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        
    }

    自己创建的类的源代码:

    package com.jaovo.msg.model;
    
    public class User {
        
        
        private String  name;
        private String  teacher;
        private String  place;
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        public String getTeacher() {
            return teacher;
        }
        public void setTeacher(String teacher) {
            this.teacher= teacher;
        }
        public String getPlace() {
            return place;
        }
        public void setPlace(String place) {
            this.place = place;
        }
        
        
        
    }

    向数据库添加数据的源代码:

    import com.jaovo.msg.Util.DBUtil;
    //import com.jaovo.msg.Util.UserException;
    import com.jaovo.msg.model.User;
    
    public class UserDaoImpl 
    {
        
        public void add(User user) {
            
            Connection connection = DBUtil.getConnection();
            
    
    
            PreparedStatement preparedStatement = null;
    
                try{
                String sql = "insert into t_teacher(name,teacher,place) values (?,?,?)";
                preparedStatement = connection.prepareStatement(sql);
                preparedStatement.setString(1, user.getName());
                preparedStatement.setString(2, user.getTeacher());
                preparedStatement.setString(3, user.getPlace());
                preparedStatement.executeUpdate();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }finally {
                
                //DBUtil.close(resultSet);
                DBUtil.close(preparedStatement);
                DBUtil.close(connection);
            }
            
        }
        
    
        
    
    }

    刚开始输入时的JSP:

    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>提交作业</title>
    </head>
    <body>
    <%
    String s=(String)request.getAttribute("Error");
    if("".equals(s)||s==null)
    {
        s="";
    }
    %>
    <%=s %>
    <form action="LoginInput.jsp" method="get">
            <table align="center" border="1">
    <tr>
    <td colspan="2">
    课程名称
     <input type="text" name="name"/>
    </td>
    </tr>
    <tr>
       <td colspan="2">
        任课教师
     <input type="text" name="teacher"/>
       </td>
      </tr>
      <tr>
       <td colspan="2">
        上课地点
     <input type="text" name="place"/>
       </td>
      </tr>
     
      <tr>
      <td align="center" colspan="2">
       <input type="submit" value="保存"/></td>
      </tr>
      </table>
      </form>
    </body>
    </html>

    结束数据并判断是否符合要求的JSP:

    <%@page import="com.jaovo.msg.dao.UserDaoImpl"%>
    <%@page import="com.jaovo.msg.model.User"%>
    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <%
             int i=0;
             boolean flag=true;
             boolean flag1=true;
             String s[]={"王建民","刘丹","刘立嘉","杨子光","王辉"};
             String s1[]={"基教","一教","二教","三教"};
             String name=request.getParameter("name");
             String teacher=request.getParameter("teacher");
             String place=request.getParameter("place");
            while(i<s.length)
             {
            if(!s[i].equals(teacher))
         {
          flag=false;
         }
            else
                {
                flag=true;
                break;
                }
          i++;
         }%>
         <%
         if(!flag)
         {
             request.setAttribute("Error", "老师不对");
         
         %>
         <jsp:forward page="Login.jsp"></jsp:forward>
           <% 
         }
         i=0;
         String s3=place.substring(0,2);
     
         while(flag==true&&i<s1.length)
         {
            
        if(!s1[i].equals(s3))
             {
                 flag1=false;
              }
             else
            {
            flag1=true;
            break;
            }
              i++;
           }
         if(!flag1)
         {
             request.setAttribute("Error", "教室不对");
         
         %>
         <jsp:forward page="Login.jsp"></jsp:forward>
           <% 
         }
            UserDaoImpl userdao=new UserDaoImpl();
             User user=new User();
             user.setName(name);
             user.setTeacher(teacher);
             user.setPlace(place);
             userdao.add(user);
             System.out.println(teacher);
    %>
    
    <<jsp:forward page="Login.jsp"></jsp:forward>

    源程序截图:

  • 相关阅读:
    pdf在线转换器
    抖音修复老照片动起来笑起来的程序app的下载地址
    FFmpeg.AutoGen Unable to load DLL 'avutil.56' 解决方法
    Array.prototype.fill 填充值被复用的问题
    Recoil Input 光标位置被重置到末尾的问题
    TypeScript 扩展全局 Window 时报错的解决
    Recoil 中默认值的正确处理
    Recoil 中多级数据联动及数据重置的合理做法
    Recoil 默认值及数据级联的使用
    Recoil 的使用
  • 原文地址:https://www.cnblogs.com/ljysy/p/7911079.html
Copyright © 2020-2023  润新知