(未完成)出错有点多,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. 深入JVM垃圾回收机制,值得你收藏

    JVM可以说是为了Java开发人员屏蔽了很多复杂性,让Java开发的变的更加简单,让开发人员更加关注业务而不必关心底层技术细节,这些复杂性包括内存管理,垃圾回收,跨平台等,今天我们主要看看JVM的垃圾 ...

  2. ORB-SLAM2 论文&代码学习 —— 单目初始化

    转载请注明出处,谢谢 原创作者:Mingrui 原创链接:https://www.cnblogs.com/MingruiYu/p/12358458.html 本文要点: ORB-SLAM2 单目初始化 ...

  3. backgroud图片充满元素的方法

    background-image: url("img/headimg.png"); height: 219px; background-size: 100% 100%; backg ...

  4. A——大整数加法(HDU1002)

    题目: I have a very simple problem for you. Given two integers A and B, your job is to calculate the S ...

  5. 解决IIS程序池回收webapi定时程序造成的影响

    问题描述: webapi中有一个定时器线程,在iis程序池在1740分钟回收后,定时器中止 问题解决步骤: 1.设置程序池定期回收,设置每天定时回收 2.在windows自带的任务计划中,添加一条任务 ...

  6. 多线程共享变量和 AsyncLocal

    >>返回<C# 并发编程> 1. 简介 2. 异步下的共享变量 3. 解析 AsyncLocal 3.1. IAsyncLocalValueMap 的实现 3.2. 结论 1. ...

  7. 简化MVVM属性设置和修改 - .NET CORE(C#) WPF开发

    微信公众号:Dotnet9,网站:Dotnet9,问题或建议:请网站留言, 如果对您有所帮助:欢迎赞赏. 简化MVVM属性设置和修改 - .NET CORE(C#) WPF开发 阅读导航 常用类属性设 ...

  8. 零基础Python应该怎样学习呢?(附视频教程)

    Python应该怎样学习呢? 阶段一:适合自己的学习方式 对于零基础的初学者来说,最迷茫的是不知道怎样开始学习?那这里小编建议可以采用视频+书籍的方式进行学习.看视频学习可以让你迅速掌握编程的基础语法 ...

  9. 【React Native】某个页面禁用物理返回键

    1.引入组件 import { BackHandler, } from 'react-native'; 2.添加监听 componentDidMount(): void { BackHandler.a ...

  10. javaConfig&springBoot入门

    javaConfig&springBoot入门 1. javaConfig基础 1.1 为什么要学习javaConfig 因为:Springboot原理基于它的!!!(为学习springBoo ...