(未完成)出错有点多,Tomcat的配置也出问题了,Tomcat突然找不到jdk了,好像是我之前下载过一回然后没卸干净有残留文件,还有连接数据库的代码也有问题。
 
 
 
<%@page import="util.DBUtil"%>
<%@page import="dao.AdminDao"%>
<%@page import="Bean.AdminBean"%>
<%@ 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>Insert title here</title>
</head>
<body>
<%
 String platid=request.getParameter("platid");
 String password=request.getParameter("password");
 String sex=request.getParameter("sex");
 String platname=request.getParameter("platname");
 String stunum=request.getParameter("stunum");
 String mail=request.getParameter("mail");
 String colname=request.getParameter("colname");
 String proname=request.getParameter("proname");
 String roomname=request.getParameter("roomname");
 String year=request.getParameter("year");
 String homename=request.getParameter("homename");
 String rename=request.getParameter("rename");
 
 Platform platform =new Platform();
 platform.setPlatformid(platid);
 platform.setPassword(password);
 platform.setSex(sex);
 platform.setPlatformname(platname);
 platform.setStunum(stunum);
 platform.setMail(mail);
 platform.setColname(colname);
 platform.setProname(proname);
 platform.setRoomname(roomname);
 platform.setYear(year);
 platform.setHomename(homename);
 platform.setRename(rename);
 
 PlatformDaoImpl platformDaoImpl=new PlatformDaoImpl();
 try{
  
 platformDaoImpl.add(platform);
 System.out.println("注册成功");
 response.sendRedirect("logininput.jsp");
%>
 
 
<%
 }catch(UserException e){
%>
 <h2 style="color:red ; font-size:50px">发生错误 : <%=e.getMessage() %></h2>
 <%
 }
 %>
</body>
</html>
 
 
 
 
 
//主界面.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>
<br>
<h1 align="center" >添加学生信息</h1>
<hr size="2" width="70%" color="red">
<form action="register.jsp" method="get">
<table align="center" border="1">
<tr>
 <td>登录账号:</td>
 <td><input type="text" name="platid" value="(八位以上的字母、数字、下划线)" onclick="this.value=''" style="color:#888" onblur="if(/[^0-9a-zA-Z]/g.test(value)||this.value.replace(/^ +| +$/g,'')=='')alert('有错!')"></td>
</tr>
<tr>
 <td>登录密码:</td>
 <td><input type="password" name="password" onblur="if(/[^0-9a-zA-Z]/g.test(value)||this.value.replace(/^ +| +$/g,'')=='')alert('有错!')"></td>
</tr>
<tr>
 <td>性别:</td>
 <td>
      <select type="sex"><option value="男"  selected>男</option>
                           <option value="女">女</option>
</tr>
<tr>
 <td>姓名:</td>
 <td><input type="text" name="platname" onblur="if(/[^0-9a-zA-Z]/g.test(value)||this.value.replace(/^ +| +$/g,'')=='')alert('有错!')"></td>
</tr>
<tr>
 <td>学号:</td>
 <td><input type="text" name="stunum" value="(2018开头的八位数字)" onclick="this.value=''" style="color:#888" onblur="if(/[^0-9a-zA-Z]/g.test(value)||this.value.replace(/^ +| +$/g,'')=='')alert('有错!')"></td>
</tr>
<tr>
 <td>电子邮箱:</td>
 <td><input type="text" name="mail" value="(xxxx@xxxx.xxx)" onclick="this.value=''" style="color:#888" onblur="if(/[^0-9a-zA-Z]/g.test(value)||this.value.replace(/^ +| +$/g,'')=='')alert('有错!')"></td>
</tr>
<tr>
 <td>所在学院:</td>
 <td><input type="text" name="colname" onblur="if(/[^0-9a-zA-Z]/g.test(value)||this.value.replace(/^ +| +$/g,'')=='')alert('有错!')"></td>
</tr>
<tr>
 <td>所在系:</td>
 <td><input type="text" name="proname" onblur="if(/[^0-9a-zA-Z]/g.test(value)||this.value.replace(/^ +| +$/g,'')=='')alert('有错!')"></td>
</tr>
<tr>
 <td>所在班级:</td>
 <td><input type="text" name="roomname" onblur="if(/[^0-9a-zA-Z]/g.test(value)||this.value.replace(/^ +| +$/g,'')=='')alert('有错!')"></td>
</tr>
<tr>
 <td>入学年份:</td>
 <td>
      <select type="year"><option value="2017"  selected>2017</option>
                           <option value="2018">2018</option>
</tr>
<tr>
 <td>生源地:</td>
 <td><input type="text" name="homename" value="(省/市/县(区))" onclick="this.value=''" style="color:#888" onblur="if(/[^0-9a-zA-Z]/g.test(value)||this.value.replace(/^ +| +$/g,'')=='')alert('有错!')"></td>
</tr>
<tr>
 <td>备注:</td>
 <td><input type="text" name="rename" value="(无)" onclick="this.value=''" style="color:#888" onblur="if(/[^0-9a-zA-Z]/g.test(value)||this.value.replace(/^ +| +$/g,'')=='')alert('有错!')"></td>
</tr>
<tr>
<td colspan="1" align="center">
  <input  type="submit" value="注册">
</td>
</tr>
</table>
</form>
</body>
</html>
 
 
 
 
 
 
//添加成功界面.jsp
<%@ 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>
<body>
<h1 align="center" >添加成功</h1>
</body>
</html>
 
 
 
 
 
 
 
//Bean文件
package Bean;
public class AdminBean {
 private int id;
 private String password;
 private String sex;
 private String name;
 private int studentnumber;
 private String mailbox;
 private String college;
 private String profession;
 private String classroom;
 private int year;
 private String hometown;
 private String remark;
 
 public int getId() {
  return id;
 }
 public void setId(int id) {
  this.id = id;
 }
 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 getName() {
  return name;
 }
 public void setName(String name) {
  this.name = name;
 }
 public int getStudentnumber() {
  return studentnumber;
 }
 public void setStudentnumber(int studentnumber) {
  this.studentnumber = studentnumber;
 }
 public String getMailbox() {
  return mailbox;
 }
 public void setMailbox(String mailbox) {
  this.mailbox = mailbox;
 }
 
 public String getCollege() {
  return college;
 }
 public void setCollege(String college) {
  this.college = college;
 }
 
 public String getProfession() {
  return profession;
 }
 public void setProfession(String profession) {
  this.profession = profession;
 }
 
 public String getClossroom() {
  return classroom;
 }
 public void setClassroom(String classroom) {
  this.classroom = classroom;
 }
 
 public int getYear() {
  return year;
 }
 
 public void setYear(int year) {
  this.year = year;
 }
 public String getHometown() {
  return hometown;
 }
 public void setHometown(String hometown) {
  this.hometown = hometown;
 }
 
 public String getRemark() {
  return remark;
 }
 public void setRemark(String remark) {
  this.remark = remark;
 }
 
}
 
 
 
 
 
 
 
 
//Dao文件
package dao;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import Bean.AdminBean;
import util.DBUtil;
/**
 * admin表数据库操作
 *
 * @author wjxing
 *
 */
public class AdminDao {
 /**
  * 登录
  *
  * @param username
  * @param password
  * @return
  */
 public AdminBean checkLogin(String username, String password) {
  Connection conn = DBUtil.getConn();
  AdminBean adminBean = null;
  try {
   Statement state = conn.createStatement();
   ResultSet rs = state.executeQuery("select * from admin where username='" + username + "'");
   if (rs.next()) {
    // 如果有结果,是认为是通过验证了
    if (rs.getString("password").equals(MD5.GetMD5Code(password+rs.getString("salt")))) {
     adminBean = new AdminBean();
     adminBean.setId(rs.getInt("id"));     
     adminBean.setPassword(rs.getString("password"));
     adminBean.setSex(rs.getString("sex"));
     adminBean.setName(rs.getString("name"));
     adminBean.setStudentnumber(rs.getInt("studentnumber"));
     adminBean.setMailbox(rs.getString("mailbox"));
     adminBean.setCollege(rs.getString("college"));
     adminBean.setProfession(rs.getString("profession"));
     adminBean.setClassroom(rs.getString("classroom"));
     adminBean.setYear(rs.getInt("year"));
     adminBean.setHometown(rs.getString("hometown"));
     adminBean.setRemark(rs.getString("remark"));
    }
   }
  } catch (Exception e) {
   e.printStackTrace();
  }
  return adminBean;
 }
 /**
  * 检查是否存在此用户
  *
  * @param name
  * @return
  */
 public boolean checkReg(String name) {
  boolean flag = true;
  // 查询用户是否已存在
  Connection connection = DBUtil.getConn();
  Statement statement = null;
  ResultSet rs = null;
  try {
   statement = connection.createStatement();
   rs = statement.executeQuery("select username from admin");
   while (rs.next()) {
    if (name.equals(rs.getString("username"))) {
     flag = false;
    }
   }
  } catch (Exception e) {
   e.printStackTrace();
  } finally {
   DBUtil.close(rs, statement, connection);
  }
  return flag;
 }
 
 
 /**
  * 通过id获取adminBean对象
  *
  * @param id
  * @return
  */
 public AdminBean getById(int id) {
  // TODO Auto-generated method stub
  String sql = "select * from admin where id =" + id;
  Connection connection = DBUtil.getConn();
  Statement statement = null;
  ResultSet resultSet = null;
  AdminBean adminBean = null;
  try {
   statement = connection.createStatement();
   resultSet = statement.executeQuery(sql);
   while (resultSet.next()) {
    adminBean = new AdminBean(); 
    adminBean.setId(resultSet.getInt("id"));     
    adminBean.setPassword(resultSet.getString("password"));
    adminBean.setSex(resultSet.getString("sex"));
    adminBean.setName(resultSet.getString("name"));
    adminBean.setStudentnumber(resultSet.getInt("studentnumber"));
    adminBean.setMailbox(resultSet.getString("mailbox"));
    adminBean.setCollege(resultSet.getString("college"));
    adminBean.setProfession(resultSet.getString("profession"));
    adminBean.setClassroom(resultSet.getString("classroom"));
    adminBean.setYear(resultSet.getInt("year"));
    adminBean.setHometown(resultSet.getString("hometown"));
    adminBean.setRemark(resultSet.getString("remark"));
    
   }
  } catch (Exception e) {
   e.printStackTrace();
  } finally {
   DBUtil.close(resultSet, statement, connection);
  }
  return adminBean;
 }
 /**
  * 通过id删除
  *
  * @param id
  */
 public void delete(int id) {
  // TODO Auto-generated method stub
  String sql = "delete from admin where id = " + id;
  Connection conn = DBUtil.getConn();
  Statement state = null;
  try {
   state = conn.createStatement();
   state.executeUpdate(sql);
  } catch (Exception e) {
   e.printStackTrace();
  } finally {
   DBUtil.close(state, conn);
  }
 }
 /**
  * 获取数据表中数据总量
  * @return
  */
 public int getCount() {
  ResultSet rs = null;
  Statement state = null;
  Connection conn = null;
  int size = 0;
  try {
   conn = DBUtil.getConn();
   state = conn.createStatement();
   rs = state.executeQuery("select count(*) count from admin");
   if (rs.next()) {
    size = rs.getInt("count");
   }
  } catch (SQLException e) {
   e.printStackTrace();
  } finally {
   DBUtil.close(rs, state, conn);
  }
  return size;
 }
 /**
  * 获取每一个分页的数据
  * @param start
  * @param size
  * @return
  */
 public List<AdminBean> getListByPage(int start, int size) {
  String sql = "select * from admin limit " + start + " , " + size;
  Connection connection = DBUtil.getConn();
  Statement statement = null;
  ResultSet resultSet = null;
  List<AdminBean> adminBeans = new ArrayList<AdminBean>();
  try {
   statement = connection.createStatement();
   resultSet = statement.executeQuery(sql);
   AdminBean adminBean;
   while (resultSet.next()) {
    adminBean = new AdminBean();
    adminBean.setId(resultSet.getInt("id"));     
    adminBean.setPassword(resultSet.getString("password"));
    adminBean.setSex(resultSet.getString("sex"));
    adminBean.setName(resultSet.getString("name"));
    adminBean.setStudentnumber(resultSet.getInt("studentnumber"));
    adminBean.setMailbox(resultSet.getString("mailbox"));
    adminBean.setCollege(resultSet.getString("college"));
    adminBean.setProfession(resultSet.getString("profession"));
    adminBean.setClassroom(resultSet.getString("classroom"));
    adminBean.setYear(resultSet.getInt("year"));
    adminBean.setHometown(resultSet.getString("hometown"));
    adminBean.setRemark(resultSet.getString("remark"));
   }
  } catch (Exception e) {
   e.printStackTrace();
  } finally {
   DBUtil.close(resultSet, statement, connection);
  }
  return adminBeans;
 }
}
 
 
 
 
 
 
 
 
//util文件
package util;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
/**
 * 数据库的工具
 * @author zm
 *
 */
public class DBUtil {
 //eshop为数据库名称,db_user为数据库用户名db_password为数据库密码
 public static String db_url = "jdbc:mysql://localhost:3306/firsttest?useUnicode=true&characterEncoding=UTF-8";
 public static String db_user = "root";
 public static String db_password = "root";
 public static Connection getConn() {
  Connection conn = null;
  try {
   Class.forName("com.mysql.jdbc.Driver");
   conn = DriverManager.getConnection(db_url, db_user, db_password);
  } 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();
   }
  }
 }
}
 
 
 
 
 
 
 
//servlet文件
package servlet.Servlet;
import java.io.IOException;
import java.sql.Date;
import java.util.regex.Pattern;
import javax.jws.WebService;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.mysql.fabric.Response;
import Entity.User;
import Dao.Dao;
/**
 * Servlet implementation class UserServlet
 */
@WebService("/UserServlet")
public class UserServlet 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 UserServlet() {
        super();
        // TODO Auto-generated constructor stub
    }
 @Override
 protected void service(HttpServletRequest arg0, HttpServletResponse arg1) throws ServletException, IOException {
  Date haha=new Date(0);
  // 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("id");
   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=UserServlet.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("NewFile.jsp").forward(arg0, arg1);
   }
   else
   {
    arg0.setAttribute("message", "注册失败");
    arg0.getRequestDispatcher("NewFile.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);
 }*/
}

FirstJavaWeb的更多相关文章

随机推荐

  1. 最大流-前置push-relabel算法实现

    Front Push-Relabel Algorithm 接口定义 Input:容量数组vector<vector<int>> capacity ,大小为n:源点int sou ...

  2. RocketMQ重试机制和消息幂等

    一.重试机制 由于MQ经常处于复杂的分布式系统中,考虑网络波动,服务宕机,程序异常因素,很有可能出现消息发送或者消费失败的问题.因此,消息的重试就是所有MQ中间件必须考虑到的一个关键点.如果没有消息重 ...

  3. AD域SSP安全防护

    一.简介 SSP(Security Support Provider)是windows操作系统安全机制的提供者.简单的说,SSP就是DLL文件,主要用于windows操作系统的身份认证功能,例如NTL ...

  4. Python3 (五)函数应用

    一.认识函数 在命令行中查看内置函数的方法: 1.先在命令行里输入python 2.help(函数) 二.函数的定义及运行特点 1.函数基本定义: def funcname(parameter_lis ...

  5. javascript js全部的 全局属性 和 方法-window

    window method: open(URL,窗口名称,窗口风格)//打开一个新的窗口,并在窗口中装载指定URL地址的网页 close()//close方法用于自动关闭浏览器窗口 alert(提示字 ...

  6. pytorch之 CNN

    # library # standard library import os # third-party library import torch import torch.nn as nn impo ...

  7. k8s系列----一个简单的例子

    本实验来自k8s权威指南 解决访问demo出错,关键是靠https://www.cnblogs.com/neutronman/p/8047547.html此链接下面的某个大神的评论 主要yaml文件 ...

  8. yum 升级php版本

    centos默认安装的php都是 5.3的  ,现在需要 5.6以上的版本 手动安装比较麻烦,直接用yum升级了. 一.准备工作 首先检查当前php版本 #php -v 查看安装的php扩展包 #yu ...

  9. PWA(Progressive web apps),渐进式 Web 应用

    学习博客:https://www.jianshu.com/p/098af61bbe04 学习博客:https://www.zhihu.com/question/59108831 官方文档:https: ...

  10. JS高阶编程技巧--compose函数

    先看代码: let fn1 = function (x) { return x + 10; }; let fn2 = function (x) { return x * 10; }; let fn3 ...