在text.jsp中画出界面,以及设置提交选项的限制

  1. <%@ page language="java" contentType="text/html; charset=UTF-8"
  2. pageEncoding="UTF-8"%>
  3. <!DOCTYPE html>
  4. <html>
  5. <head>
  6. <meta charset="UTF-8">
  7. <title>当前位置:添加学生信息</title>
  8. </head>
  9. <body>
  10. <%
  11. Object message = request.getAttribute("message");
  12. if (message != null && !"".equals(message)) {
  13. %>
  14. <script type="text/javascript">
  15. alert("<%=request.getAttribute("message")%>");
  16. var asd=request.getAttribute("username");
  17. </script>
  18. <%
  19. }
  20. %>
  21. <form action="Servlet?method=add" method="post" onsubmit="return check(this)">
  22.  
  23. <table >
  24. <tr>
  25. <th>登录账号:</th>
  26. <td>
  27.  
  28. <input name="username" type="text" />
  29. </td>
  30. </tr>
  31. <tr>
  32. <th>登录密码:</th>
  33. <td>
  34. <input name="password" type="password" />
  35. </td>
  36. </tr>
  37. <tr>
  38. <th>性别:</th>
  39. <td>
  40. <select name="sex">
  41. <option></option>
  42. <option></option>
  43. </select>
  44. </td>
  45. </tr>
  46. <tr>
  47. <th>姓名:</th>
  48. <td>
  49. <input name="name" type="text">
  50. </td>
  51. </tr>
  52. <tr>
  53. <th>学号:</th>
  54. <td>
  55. <input name="sno" type="text">
  56. </td>
  57. </tr>
  58. <tr>
  59. <th>电子邮件:</th>
  60. <td>
  61. <input name="email" type="text">
  62. </td>
  63. </tr>
  64. <tr>
  65. <th>所在学校:</th>
  66. <td>
  67. <input name="school" type="text">
  68. </td>
  69. </tr>
  70. <tr>
  71. <th>所在系:</th>
  72. <td>
  73. <input name="college" type="text">
  74. </td>
  75. </tr>
  76. <tr>
  77. <th>所在班级:</th>
  78. <td>
  79. <input name="class1" type="text">
  80. </td>
  81. </tr>
  82. <tr>
  83. <th>入学年份(届):</th>
  84. <td>
  85. <select name="time">
  86. <option>2015</option>
  87. <option>2016</option>
  88. <option>2017</option>
  89. <option>2018</option>
  90. <option>2019</option>
  91. </select>
  92. </td>
  93. </tr>
  94.  
  95. <tr>
  96. <th>生源地:</th>
  97. <td>
  98. <input name="ofstudents" type="text">
  99. </td>
  100. </tr>
  101.  
  102. <tr>
  103. <th>备注:</th>
  104. <td>
  105. <input name="remarks" type="text">
  106. </td>
  107. </tr>
  108. <tr>
  109. <td >
  110. <input type="submit" value="提交" />
  111.  
  112. </td>
  113. </tr>
  114. </table>
  115. </form>
  116.  
  117. </body>
  118. <script type="text/javascript">
  119. function check(form){
  120. if(form.username.value.length<6 || form.username.value.length>12 || new RegExp("[^a-zA-Z0-9_\u4e00-\u9fa5]").test(form.username.value)){
  121. alert("登陆账号必须由6-12位英文字符或数字组成!");
  122. form.username.focus();
  123. return false;
  124. }
  125. if(new RegExp("[^a-zA-Z]").test(form.username.value.substring(0,1))){
  126. alert("登陆账号必须以英文字母开头!");
  127. form.username.focus();
  128. return false;
  129. }
  130. if(form.password.value.length < 8 || new RegExp("[^0-9a-zA-Z]").test(form.password.value)){
  131. alert("密码由八位以上数字或字母组成!");
  132. form.password.focus();
  133. return false;
  134. }
  135. var re = /^\d{4}/;
  136. if(form.sno.value.length != 8 || form.sno.value.match(re)[0]!='2018'){
  137. alert("学号由八位组成,前四位是2018");
  138. form.password.focus();
  139. return false;
  140. }
  141. //以数字字母开头,中间可以是多个数字字母或下划线;然后是“@”;然后是数字字母;然后是“.”;最后是2-4个字母结尾
  142. var regex = /^([a-zA-Z]|[0-9])(\w|\-)+@[a-zA-Z0-9]+\.([a-zA-Z]{2,4})$/;
  143. if(!regex.test(form.email.value)){
  144. alert("邮箱格式错误!");
  145. form.email.focus();
  146. return false;
  147. }
  148. return true;
  149. }
  150. </script>
  151. </html>

创建4个包,在每个包里面创建一个.Java文件,分别是Dao、DBUtil、User、Servlet,用来连接数据库,以及向数据库中添加信息

Dao:

  1. package Dao;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.Statement;
  5.  
  6. import DBUtil.DBUtil;
  7.  
  8. import Entity.User;
  9.  
  10. public class Dao {
  11.  
  12. public boolean add(User user) {
  13. // TODO Auto-generated method stub
  14. String sql = "insert into kao(username,password,sex,name,sno,email,school,college,class1,time,ofstudents,remarks) values('"+ user.getUsername() + "','"+ user.getPassword() +"','"+ user.getSex() +"','" + user.getName() +"','"+ user.getSno() +"','"+ user.getEmail() +"','"+ user.getSchool() +"','"+ user.getCollege() +"','"+ user.getClass1() +"','"+ user.getTime() +"','"+ user.getOfstudents()+"','" +user.getRemarks()+"')";
  15. Connection conn = DBUtil.getConn();
  16. Statement state = null;
  17. boolean f = false;
  18. int a = 0;
  19.  
  20. try {
  21. state = conn.createStatement();
  22. a=state.executeUpdate(sql);
  23. } catch (Exception e) {
  24. e.printStackTrace();
  25. } finally {
  26.  
  27. DBUtil.close(state, conn);
  28. }
  29.  
  30. if (a > 0) {
  31. f = true;
  32. }
  33. return f;
  34.  
  35. }
  36. }

DBUtil:

  1. package DBUtil;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.PreparedStatement;
  6. import java.sql.ResultSet;
  7. import java.sql.SQLException;
  8. import java.sql.Statement;
  9.  
  10. public class DBUtil {
  11.  
  12. public static String db_url = "jdbc:mysql://localhost:3306/xinxi";
  13. public static String db_user = "root";
  14. public static String db_pass = "wyp6666";
  15.  
  16. public static Connection getConn () {
  17. Connection conn = null;
  18.  
  19. try {
  20. Class.forName("com.mysql.jdbc.Driver");
  21. conn = DriverManager.getConnection(db_url, db_user, db_pass);
  22. } catch (Exception e) {
  23. e.printStackTrace();
  24. }
  25.  
  26. return conn;
  27. }
  28.  
  29. public static void close (Statement state, Connection conn) {
  30. if (state != null) {
  31. try {
  32. state.close();
  33. } catch (SQLException e) {
  34. e.printStackTrace();
  35. }
  36. }
  37.  
  38. if (conn != null) {
  39. try {
  40. conn.close();
  41. } catch (SQLException e) {
  42. e.printStackTrace();
  43. }
  44. }
  45. }
  46.  
  47. public static void close (ResultSet rs, Statement state, Connection conn) {
  48. if (rs != null) {
  49. try {
  50. rs.close();
  51. } catch (SQLException e) {
  52. e.printStackTrace();
  53. }
  54. }
  55.  
  56. if (state != null) {
  57. try {
  58. state.close();
  59. } catch (SQLException e) {
  60. e.printStackTrace();
  61. }
  62. }
  63.  
  64. if (conn != null) {
  65. try {
  66. conn.close();
  67. } catch (SQLException e) {
  68. e.printStackTrace();
  69. }
  70. }
  71. }
  72.  
  73. public static void main(String[] args) throws SQLException {
  74. Connection conn = getConn();
  75. PreparedStatement pstmt = null;
  76. ResultSet rs = null;
  77. String sql ="select * from users";
  78. pstmt = conn.prepareStatement(sql);
  79. rs = pstmt.executeQuery();
  80. if(rs.next()){
  81. System.out.println("成攻");
  82. }else{
  83. System.out.println("失败");
  84. }
  85. }
  86. }

Servlet:

  1. package Servlet;
  2. import java.io.IOException;
  3.  
  4. import javax.servlet.ServletException;
  5. import javax.servlet.annotation.WebServlet;
  6. import javax.servlet.http.HttpServlet;
  7. import javax.servlet.http.HttpServletRequest;
  8. import javax.servlet.http.HttpServletResponse;
  9.  
  10. import Dao.Dao;
  11.  
  12. import Entity.User;
  13.  
  14. @WebServlet("/Servlet")
  15. public class Servlet extends HttpServlet {
  16. private static final long serialVersionUID = 1L;
  17.  
  18. public Servlet() {
  19. super();
  20.  
  21. }
  22. protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
  23. req.setCharacterEncoding("utf-8");
  24. String method = req.getParameter("method");
  25. if ("add".equals(method)) {
  26. add(req, resp);
  27. }
  28. }
  29.  
  30. private void add(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException{
  31. req.setCharacterEncoding("utf-8");
  32. String username = req.getParameter("username");
  33. String password = req.getParameter("password");
  34. String name = req.getParameter("sex");
  35. String sex = req.getParameter("name");
  36. String sno = req.getParameter("sno");
  37. String email = req.getParameter("email");
  38. String school = req.getParameter("school");
  39. String college = req.getParameter("college");
  40. String class1 = req.getParameter("class1");
  41. String time= req.getParameter("time");
  42. String ofstudents = req.getParameter("ofstudents");
  43. String remarks = req.getParameter("remarks");
  44. User user = new User(username,password,sex,name,sno,email,school,college,class1,time,ofstudents,remarks);
  45.  
  46. Dao dao =new Dao();
  47. boolean f=dao.add(user);
  48.  
  49. if(f) {
  50. req.setAttribute("message", "注册成功!");
  51. req.getRequestDispatcher("text.jsp").forward(req,resp);
  52. } else {
  53. req.setAttribute("message", "注册失败!");
  54. req.getRequestDispatcher("text.jsp").forward(req,resp);
  55. }
  56. }
  57. }

javeweb_学生信息添加系统的更多相关文章

  1. 学习完vue指令 做的一个学生信息录入系统

    一.demo实现原理 输入完个人信息后  点击创建用户  数据就会显示在下面的表格中 用到了vue中的数据双向绑定 v-model v-for 还要js正则 数组的unshift splice 等方法 ...

  2. Vue实现一个学生信息录入系统,实现录入和删除

    效果如下: 代码如下: <!DOCTYPE html> <html lang="en"> <head> <meta charset=&qu ...

  3. javaweb简单的学生信息录入系统

    讲一下思路,主界面的设计就是用html表单元素,百度查找各个元素的用法,按照自己的想法摆放即可,表单提交后会把数据交给serverlet去处理,在那里定义几个字符串变量来储存获取到的数据,然后按照项目 ...

  4. Django 学生信息 添加 功能 遇到的问题.

    1  添加 班级信息时的问题 (grade为外键) 原因是 grade 必需接收 一个 实例, 而我交是一个 str字符串, if request.method == 'POST': data = { ...

  5. Linux下基于shell脚本实现学生信息管理系统

    #该管理系统是参考两位博主(时间有点远了,我忘了,请博主看到后联系我)后自行修改添加的.登录过程还有很多不完善,我就抛砖引玉啦. 废话不多,直接上码! #!/bin/bash# 学生管理系统# @ve ...

  6. Java基础知识强化之IO流笔记51:IO流练习之 键盘录入学生信息按照总分排序写入文本文件中的案例

    1.  键盘录入学生信息(姓名,语文成绩,数学成绩,英语成绩),按照总分排序写入文本文件中 分析:   A:创建学生类   B:创建集合对象      TreeSet<Student>   ...

  7. 用List传递学生信息

    集合在程序开发中经常用到,例如,在业务方法中将学生信息.商品信息等存储到集合中,然后作为方法的返回值返回给调用者,以此传递大量的有序数据. 本实例将使用List集合在方法之间传递学生的信息.实例效果如 ...

  8. 学生信息管理小系统(以XML为存储方式)

    为了更好地应用XML,就写了这个小项目. 下面是我的项目的目录结构 项目思路 dao是Date Access Object 数据访问层,主要是负责操作数据 domain是实体层,类似于bean层,放置 ...

  9. Jmeter接口测试实例2-获取所有学生信息

    Jmeter实例2:获取所有学生信息 添加http协议—添加IP.路径.方法,添加信息头管理器,察看结果树,运行 如下图所示,响应结果中获取到所有学生信息

随机推荐

  1. LeetCode112 Path Sum

    Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all ...

  2. idea java内存分析工具

    https://blog.csdn.net/qq_22194659/article/details/83829891 https://www.ej-technologies.com/products/ ...

  3. @hdu - 5960@ Subsequence

    目录 @description@ @solution@ @accepted code@ @details@ @description@ 给定如下计算序列权值的函数: 对于一个由三元组 (cost0, ...

  4. 公司安装mariaDB-5.5.52和Jdk 7

    转自:http://www.cnblogs.com/kgdxpr/p/3209009.html vi /etc/yum.repos.d/MariaDB.repo 加入下面内容 [mariabd]nam ...

  5. jsp页面关建字查询出记录后,点下一页关键字会清空,怎么保持关键字不变而进行下一页操作?

    解决方案一: 1 把关键字带回后台,从后台再次传入! 2 把关键字传入cookie,从cookie获取 3 把表格一栏放在iframe中,搜索时,刷新iframe即可 解决方案二: 用2个div分开就 ...

  6. @codechef - RNG@ Random Number Generator

    目录 @description@ @solution@ @part - 1@ @part - 2@ @part - 3@ @accepted code@ @details@ @description@ ...

  7. 获取checkbox返回值

    <div class="checkbox"> <label> <input type="checkbox" value=" ...

  8. tp3.2.3 解决http://lx.com/后必须加index.php才能访问的问题,配置文件中忘了加index index.php index.html 等默认文件

    server { listen 80; server_name lx.com; root "D:\phpstudy\PHPTutorial\WWW\liuxue"; locatio ...

  9. SuperSocket通过本地证书仓库的证书来启用 TLS/SSL

    你也可以通过本地证书仓库的证书,而不是使用一个物理文件. 你只需要在配置中设置你要使用的证书的storeName和thumbprint: <server name="EchoServe ...

  10. H3C PAP验证