zoukankan      html  css  js  c++  java
  • 记第一个javaweb网页

     
    <%@ page language="java" contentType="text/html; charset=utf-8"
        pageEncoding="utf-8"%>
    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="utf-8">
    <title>Insert title here</title>
    </head>
    <%
             Object message = request.getAttribute("message");
             if(message!=null && !"".equals(message)){
    %>
             <script type="text/javascript">
                  alert("<%=request.getAttribute("message")%>");
                 
             </script>
    <%} %>
    <body>
            <h1>当前位置:添加学生信息</h1>
            <form action="servlet?method=zhengjia" method="post">                                                                           
                <div class="a">
                    登录账号<input type="text" name="zhanghao" value=""/>    
                          
                </div>
                <div class="a">
                    登录密码<input type="password" name="password"value="" />
                </div>
                <div class="radio">
                <label>
                              性别<input type="radio" name="sex" value="male" checked></label>
                 <label>
                 <input type="radio" name="sex" value="female" checked></label>
                 <div class="a">
                    姓名<input type="text" name="name"value="" />
                </div>
                <div class="a">
                    学号<input type="text" name="stunum"value="2018"maxlength="8"/>
                </div>
                <div class="a">
                    电子邮件<input type="text" name="mailbox"value="" />
                </div>
                <div class="a">
                    所在学院<input type="text" name="xueyuan"value="" />
                </div>
                <div class="a">
                    所在系<input type="text" name="xi"value="" />
                </div>
                <div class="a">
                    所在班级<input type="text" name="classroom"value="" />
                </div>
                </div>
                <div class="a">
                 入学年份<td rowspan="2"> <select name="nian"onkeypress="return handleEnter(this, event)" id="4">
                    <option>2018</option>
                    <option>2017</option>
                    <option>2016</option>    
                </select></td>
                </div>
                <div class="a">
                    生源地<input type="text" name="hometown" value=""/>    
                          
                </div>
                <div class="a">
                    备注<input type="text" name="beizhu"value="" />
                </div>
                
                <div class="a">
                    <button type="submit" class="b">添&nbsp;&nbsp;&nbsp;加</button>
                </div>
            </form>
            
        
    </body>
    </html>
    <script language="JavaScript">
    function check() {
                  var zhanghao = document.form.zhanghao.value;
                  var password = document.form.password.value;
                  var  sex = document.form.sex.value;
                  var  name = document.form.name.value;
                  var  stunum= document.form.stunum.value;
                  var  xueyuan =document. form.xueyuan.value;
                  var xi = document.form.xi.value;
                  var  mailbox =document. form.mailbox.value;
                  var  classroom = document.form.classroom.value;
                  var  nian =document. form.nian.value;
                  var  hometown = document.form.hometown.value;
                  var  beizhu = document.form.beizhu.value;
              
                  //判断用户名长度
                  if (!minLength(zhanghao, 6)) {
                      alert("用户名长度小于6位!");
                      form.zhanghao.focus();
                      return false;
                  }
                  if (!maxLength(zhanghao, 12)) {
                      alert("用户名长度大于12位!");
                      form.zhanghao.focus();
                      return false;
                  }
                  if(!isDigital(zhanghao)){
                      alert("用户名只有字母可以开头");
                      form.username.focus();
                      return false;
                  }
                  
                  //判断口令长度
                  if (!minLength(password, 8)) {
                      alert("密码长度小于8位!");
                      form.password.focus();
                      return false;
                  }
                  if (!minLength(stunum, 4)) {
                      alert("学号长度小于8位!");
                      form.stunum.focus();
                      return false;
                  }
                  if (!isNumber(stunum)) {
                      alert("学号必须是数字");
                      form.stunum.focus();
                      return false;
                  }
    
                  
                  //验证E-mail的格式是否正确
                  if (!isEmail(mailbox)) {
                      alert("E-mail格式不正确!");
                      form.mailbox.focus();
                      return false;
                  }
              
                  document.form.submit();
              
              
              //验证是否满足最小长度
              function minLength(str, length) {
                  if (str.length >= length)
                      return true;
                  else
                      return false;
              }
              //判断是否满足最大长度
              function maxLength(str, length) {
                  if (str.length <= length)
                      return true;
                  else
                      return false;
              }
              //判断首字母
              function isDigital(str) {
                  
                      
                      if (str.charAt(0) >= 'a' && str.charAt(0) <= 'z'||(str.charAt(0) >= 'A' && str.charAt(0) <= 'Z'))
                          return true;
                      else
                          return false;
                  
                  
              }
    
              //判断是否是整数
              function isNumber(str)
              {
              for(i=0;i<str.length;i++)
              {
              //每一位都是0~9的数字,如果是第1位,则可以是“-”号
              if(str.charAt(i)>='0'&&str.charAt(i)<='9'
              ||str.charAt(i)=='-'&&i==0)
              continue;
              else
              return false;
              }
              return true;
              }
              
              //判断是否是E-mail
              function isEmail(mailbox) {
                  if (mailbox.length == 0)
                      return false;
                  index1 = mailbox.indexOf('@');
                  index2 = mailbox.indexOf('.');
                  if (index1 < 1//@符号不存在,或者在第一个位置
                          || index2 < 1//.符号不存在,或者在第一个位置
                          || index2 - index1 < 2//.在@的左边或者相邻
                          || index2 + 1 == email.length)//.符号后面没有东西
                      return false
                  else
                      return true;
              }
              }
    </script>
    package Db;
    
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.sql.Statement;
    
    public class Db {
        public static String db_url = "jdbc:mysql://localhost:3306/system?serverTimezone=UTC";
        public static String db_user = "root";
        public static String db_pass = "vayne";
        
        public static Connection getConn () {
            Connection conn = null;
            
            try {
                Class.forName("com.mysql.cj.jdbc.Driver");
                conn = DriverManager.getConnection(db_url, db_user, db_pass);
            } catch (Exception e) {
                e.printStackTrace();
            }
            return conn;
        }
        
        
        public static void close (Statement state, Connection conn) {
            if (state != null) {
                try {
                    state.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            
            if (conn != null) {
                try {
                    conn.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
        
        public static void close (ResultSet rs, Statement state, Connection conn) {
            if (rs != null) {
                try {
                    rs.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            
            if (state != null) {
                try {
                    state.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            
            if (conn != null) {
                try {
                    conn.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    package Fa;
    
    public class Fa {
        String zhanghao;
        String password;
        String name;
        String sex;
        String stunum;
        String xueyuan;
        String mailbox;
        String xi;
        String classroom;
        String nian;
        String hometown;
        String beizhu;
        public Fa(String zhanghao) {
            this.zhanghao = zhanghao;
        }
    
        public Fa(String zhanghao, String password, String name, String sex,String stunum,String xueyuan,String mailbox, String xi, String classroom, String nian, String hometown, String beizhu) {
    
            this.zhanghao = zhanghao;
            this.password = password;
            this.name = name;
            this.sex = sex;
            this.stunum = stunum;
            this.xueyuan = xueyuan;
            this.mailbox = mailbox;
            this.xi = xi;
            this.classroom = classroom;
            this.nian = nian;
            this.hometown = hometown;
            this.beizhu = beizhu;
        }
    
        public String getzhanghao() {
            return zhanghao;
        }
        public void setzhanghao(String zhanghao) {
            this.zhanghao = zhanghao;
        }
        public String getpassword() {
            return password;
        }
        public void setpassword(String password) {
            this.password = password;
        }
        public String getname() {
            return name;
        }
        public void setage(String name) {
            this.name = name;
        }
        public String getsex() {
            return sex;
        }
        public void setsex(String sex) {
            this.sex = sex;
        }
        public String getstunum() {
            return stunum;
        }
        public void setstunum(String stunum) {
            this.stunum = stunum;
        }
        public String getxueyuan() {
            return xueyuan;
        }
        public void setxueyuan(String xueyuan) {
            this.xueyuan = xueyuan;
        }
        public String getxi() {
            return xi;
        }
        public void setxi(String xi) {
            this.xi = xi;
        }
        public String getclassroom() {
            return classroom;
        }
        public void setclassroom(String classroom) {
            this.classroom = classroom;
        }
        public String getnian() {
            return nian;
        }
        public void setnian(String nian) {
            this.nian = nian;
        }
        public String gethometown() {
            return hometown;
        }
        public void sethometown(String hometown) {
            this.hometown = hometown;
        }
        public String getbeizhu() {
            return beizhu;
        }
        public void setbeizhu(String beizhu) {
            this.beizhu = beizhu;
        }
        public String getmailbox() {
            return sex;
        }
        public void setmailbox(String mailbox) {
            this.mailbox = mailbox;
        }
    }
    package servelt;
    
    import java.io.IOException;
    
    import javax.servlet.ServletException;
    import javax.servlet.annotation.WebServlet;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    import Fa.Fa;
    import dao.dao;
    
    @WebServlet("/servlet")
        public class servelt extends HttpServlet {
            private static final long serialVersionUID = 1L;
            protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
                request.setCharacterEncoding("utf-8");
                add(request, response);
            
            }
            
            
            private void add(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
                // TODO Auto-generated method stub
                request.setCharacterEncoding("utf-8");
                String zhanghao=request.getParameter("zhanghao");
                String password=request.getParameter("password");
                String name=request.getParameter("name");
                String sex=request.getParameter("sex");
                String stunum=request.getParameter("stunum");
                String mailbox=request.getParameter("mailbox");
                String xueyuan=request.getParameter("xueyuan");
                String xi=request.getParameter("xi");
                String classroom=request.getParameter("classroom");
                String nian=request.getParameter("nian");
                String hometown=request.getParameter("hometown");
                String beizhu=request.getParameter("beizhu");
                
                
                
                
                Fa f=new Fa( zhanghao,password,name,sex,stunum,xueyuan,mailbox,xi,classroom,nian,hometown,beizhu);
                if(dao.add(f))
                {
                    request.setAttribute("message", "添加成功");
                    request.getRequestDispatcher("NewFile.jsp").forward(request,response);
                }
                else
                {
                    request.setAttribute("message", "添加失败");
                    request.getRequestDispatcher("NewFile.jsp").forward(request,response);
                }
            }
        }
    package dao;
    
    import java.sql.Connection;
    import java.sql.Statement;
    
    import Db.Db;
    import Fa.Fa;
    
    public class dao {
        public static boolean add(Fa x) {
            String sql = "insert into biao2(zhanghao,password,name,sex,stunum,xueyuan,mailbox,xi,classroom,nian,hometown,beizhu) values('" + x.getzhanghao() + "','" + x.getpassword() +"','" + x.getname() +"','" + x.getsex() +"','"+ x.getstunum() +"','"+ x.getmailbox() +"','"+ x.getxueyuan() +"','"+ x.getxi() +"','"+ x.getclassroom()+"','"+ x.getnian()+"','"+ x.gethometown()+"','"+ x.getbeizhu()+"')";
            Connection conn = Db.getConn();
            Statement state = null;
            boolean f = false;
            int a = 0;
            try {
                state = conn.createStatement();
                a = state.executeUpdate(sql);
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                Db.close(state, conn);
            }
            
            if (a > 0) {
                f = true;
            }
            return f;
        }
    }
  • 相关阅读:
    C#后台调用Delphi 的Ocx
    打包.NET程序
    Tomcat配置虚拟目录、多域名、多个Http监听端口的方式
    检测多个Jar包冲突的class
    SessionFactoryImpl.get错误:java.lang.ArrayIndexOutOfBoundsException: 68问题
    搭建内网搜索平台
    cvs update报错
    混淆和加密.NET开发工具
    CVS报错:could not find desired version问题的解决
    记录一次接口压力测试结果
  • 原文地址:https://www.cnblogs.com/yeyueweiliang/p/11716645.html
Copyright © 2011-2022 走看看