FirstJavaWeb

(未完成)出错有点多,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);
 }*/ }
上一篇:Spring AOP 基本概念


下一篇:你真的了解装箱(Boxing)和拆箱(Unboxing)吗?