web添加学生信息(首发web)
Posted yizhixiaozhu
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了web添加学生信息(首发web)相关的知识,希望对你有一定的参考价值。
程序思路,先在JSP上画好页面,然后再创建一Servlet文件用于判断在网页上操作是否正确,还需要与数据库相连接,用DBUtile文件连接数据库,用Dao层来实现数据的增加,用Service来服务于Dao层
其代码如下:
jsp页面代码:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ page language="java" import="java.util.*"%> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>注册</title> </head> <% Object message=request.getAttribute("message"); if(!(message==null||message.equals(""))) { %> <script type="text/javascript">alert(\'<%=message%>\')</script> <% } %> <body style="background:blue" align="center"> <form action="LoginServlet?method=register" method="post"> <p style="padding-left:400px;">登录账号:<input type="text" name="username" ></p> <p style="padding-left:400px;">密 码: <input type="password" name="password"></p> <p style="padding-left:400px;">性 别: <select name=sex> <option value="男">男</option> <option value="女">女</option> </select></p> <p style="padding-left:400px;">姓 名: <input type="text" name="name" ></p> <p style="padding-left:400px;">学 号: <input type="text" name="num"></p> <p style="padding-left:400px;">电子邮件 <input type="text" name="email"></p> <p style="padding-left:400px;">所在学院 <input type="text" name="xueyuan"></p> <p style="padding-left:400px;">所在系 <input type="text" name="xi"></p> <p style="padding-left:400px;">所在班级 <input type="text" name="banji"></p> <p style="padding-left:400px;">入学年份: <select name=ruxue> <option value="2008">2008</option> <option value="2009">2009</option> <option value="2010">2010</option> <option value="2011">2011</option> <option value="2012">2012</option> <option value="2013">2013</option> <option value="2014">2014</option> <option value="2015">2015</option> <option value="2016">2016</option> <option value="2017">2017</option> <option value="2018">2018</option> <option value="2019">2019</option> </select></p> <p style="padding-left:400px;">生源地:<input type="text" name="dizhi"> <p style="padding-left:400px;">备注:<input type="text" name="beizhu"> <p style="padding-left:400px;"><input type="submit" value="注册"></p> </form> </body> </html>
Servletceng代码:
package zzz; import java.io.IOException; import java.util.regex.Pattern; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * Servlet implementation class LoginServlet */ @WebServlet("/LoginServlet") public class LoginServlet extends HttpServlet { public static boolean isMatchString(String str,String pattern) { boolean isMatch = Pattern.matches(pattern, str); return isMatch; } private static final long serialVersionUID = 1L; zhuceService fuwu=new zhuceService(); /** * @see HttpServlet#HttpServlet() */ public LoginServlet() { super(); // TODO Auto-generated constructor stub } @Override protected void service(HttpServletRequest arg0, HttpServletResponse arg1) throws ServletException, IOException { ZhuceDao haha=new ZhuceDao(); // TODO 自动生成的方法存根 arg0.setCharacterEncoding("utf-8"); arg1.setCharacterEncoding("utf-8"); //super.service(arg0, arg1); zhucexinxi sta=new zhucexinxi(); String function=arg0.getParameter("method"); if(function.equals("register")) { String str; str=arg0.getParameter("username"); if(str.length()==0||str==null||str.equals("")) { arg0.setAttribute("message","账号不能为空"); arg0.getRequestDispatcher("Login.jsp").forward(arg0,arg1); return; } if(str.charAt(0)>=\'a\'&&str.charAt(0)<=\'z\'||str.charAt(0)>=\'A\'&&str.charAt(0)<=\'Z\'); else { arg0.setAttribute("message","账号第一位需要是字母"); arg0.getRequestDispatcher("Login.jsp").forward(arg0,arg1); return; } if(str.length()<6||str.length()>12) { arg0.setAttribute("message", "账号需是6-12位"); arg0.getRequestDispatcher("Login.jsp").forward(arg0, arg1); return; } for(int a=0;a<str.length();a++) { if(!(str.charAt(a)>=\'0\'&&str.charAt(a)<=\'9\'||str.charAt(a)>=\'a\'&&str.charAt(a)<=\'z\'||str.charAt(a)>=\'A\'&&str.charAt(a)<=\'Z\'||str.charAt(a)==\'_\')) { arg0.setAttribute("message", "账号只能由字母数字下划线组成"); arg0.getRequestDispatcher("Login.jsp").forward(arg0, arg1); return; } } sta.setYonghuming(str); str=arg0.getParameter("password"); if(str.length()<8||str==null||str.equals("")) { arg0.setAttribute("message", "用户密码需要是8位以上数组字母组成"); arg0.getRequestDispatcher("Login.jsp").forward(arg0,arg1); return; } int flag1=0; int flag2=0; for(int b=0;b<str.length();b++) { if(str.charAt(b)>=\'0\'&&str.charAt(b)<=\'9\') { flag1=1; } if(str.charAt(b)>=\'a\'&&str.charAt(b)<=\'z\'||str.charAt(b)>=\'A\'&&str.charAt(b)<=\'Z\') { flag2=1; } } if(flag1!=1||flag2!=1) { arg0.setAttribute("message", "安全程度低,需要由8位以上的数字和字母组成"); arg0.getRequestDispatcher("Login.jsp").forward(arg0, arg1); return; } sta.setPassword(str); str=arg0.getParameter("sex"); sta.setSex(str); str=arg0.getParameter("name"); if(str.length()==0||str==null) { arg0.setAttribute("message", "姓名不能为空"); arg0.getRequestDispatcher("Login.jsp").forward(arg0, arg1); return; } sta.setMingzi(str); str=arg0.getParameter("num"); if(str.length()!=8) { arg0.setAttribute("message", "学号是由8位数字组成"); arg0.getRequestDispatcher("Login.jsp").forward(arg0, arg1); return; } int flag3=1; if(str.charAt(0)!=\'2\')flag3=0; if(str.charAt(1)!=\'0\')flag3=0; if(str.charAt(2)!=\'1\')flag3=0; if(str.charAt(3)!=\'8\')flag3=0; if(flag3==0) { arg0.setAttribute("message", "学号输入错误"); arg0.getRequestDispatcher("Login.jsp").forward(arg0, arg1); return; } sta.setNum(str); str=arg0.getParameter("email"); boolean nani=LoginServlet.isMatchString(str,"([a-zA-Z0-9])+@([a-zA-Z0-9])+\\\\.([a-zA-Z0-9])+"); if(!nani) { arg0.setAttribute("message", "请输入正确格式的邮箱号码"); arg0.getRequestDispatcher("Login.jsp").forward(arg0, arg1); return; } sta.setEmail(str); str=arg0.getParameter("xueyuan"); sta.setXueyuan(str); str=arg0.getParameter("xi"); sta.setXi(str); str=arg0.getParameter("banji"); sta.setBanji(str); str=arg0.getParameter("ruxue"); sta.setRuxue(str); str=arg0.getParameter("dizhi"); sta.setDizhi(str); str=arg0.getParameter("beizhu"); sta.setBeizhu(str); //判断是否添加数据库成功 if(fuwu.add("tianjiaxuesheng", sta)) { arg0.setAttribute("message", "注册成功"); arg0.getRequestDispatcher("Login.jsp").forward(arg0, arg1); } else { arg0.setAttribute("message", "注册失败"); arg0.getRequestDispatcher("Login.jsp").forward(arg0, arg1); } } } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ /*protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub response.getWriter().append("Served at: ").append(request.getContextPath()); }*/ /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ /*protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doGet(request, response); }*/ }
暂存信息层:
package zzz; public class zhucexinxi { String yonghuming; String password; String sex; String mingzi; String num; String email; String xueyuan; String xi; String banji; String ruxue; String dizhi; String beizhu; public String getXueyuan() { return xueyuan; } public void setXueyuan(String xueyuan) { this.xueyuan = xueyuan; } public String getYonghuming() { return yonghuming; } public void setYonghuming(String yonghuming) { this.yonghuming = yonghuming; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public String getMingzi() { return mingzi; } public void setMingzi(String mingzi) { this.mingzi = mingzi; } public String getNum() { return num; } public void setNum(String num) { this.num = num; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } public String getXi() { return xi; } public void setXi(String xi) { this.xi = xi; } public String getBanji() { return banji; } public void setBanji(String banji) { this.banji = banji; } public String getRuxue() { return ruxue; } public void setRuxue(String ruxue) { this.ruxue = ruxue; } public String getDizhi() { return dizhi; } public void setDizhi(String dizhi) { this.dizhi = dizhi; } public String getBeizhu() { return beizhu; } public void setBeizhu(String beizhu) { this.beizhu = beizhu; } }
数据连接层:
package zzz; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; /** * 数据库连接工具 * @author Hu * */ public class DBUtil { public static String db_url = "jdbc:mysql://localhost:3306/yonghucaozuo?useSSL=false&serverTimezone=UTC&characterEncoding=UTF-8"; public static String db_user = "root"; public static String db_pass = "password"; 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; } /** * 关闭连接 * @param state * @param 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(); } } } public static void main(String[] args) throws SQLException { Connection conn = getConn(); PreparedStatement pstmt = null; ResultSet rs = null; String sql ="select * from course"; pstmt = conn.prepareStatement(sql); rs = pstmt.executeQuery(); if(rs.next()){ System.out.println("空"); }else{ System.out.println("不空"); } } }
Dao层:
package zzz; import java.sql.Connection; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; public class ZhuceDao { public boolean add(String table,zhucexinxi en) { boolean c=false; String sql="insert into "+table+"(username,password,sex,name,num,email,xueyuan,xi,banji,ruxue,dizhi,beizhu) values(\'"+en.getYonghuming()+"\',\'"+en.getPassword()+"\',\'"+en.getSex()+"\',\'"+en.getMingzi()+"\',\'"+en.getNum()+"\',\'"+en.getEmail()+"\',\'"+en.getXueyuan()+"\',\'"+en.getXi()+"\',\'"+en.getBanji()+"\',\'"+en.getRuxue()+"\',\'"+en.getDizhi()+"\',\'"+en.getBeizhu()+"\');"; Connection conn=DBUtil.getConn(); Statement state=null; try { state=conn.createStatement(); int num=state.executeUpdate(sql); if(num!=0) c=true; } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return c; } }
服务层:
package zzz; public class zhuceService { ZhuceDao str=new ZhuceDao(); public boolean add(String table,zhucexinxi en) { return str.add(table, en); } }
以上是关于web添加学生信息(首发web)的主要内容,如果未能解决你的问题,请参考以下文章
HTML5期末大作业:餐饮美食网站设计——咖啡(10页) HTML+CSS+JavaScript 学生DW网页设计作业成品 web课程设计网页规划与设计 咖啡网页设计 美食餐饮网页设计...(代码片段
Java+Servlet+JSP+Mysql+Tomcat实现Web学生选课管理系统
基于javaweb的web资源库项目——后台用户管理demo
基于javaweb的web资源库项目——后台用户管理demo