一個簡單的java web項目 僅實現添加

鏈接數據庫已經進行判斷javascript

要求:html

1登陸帳號:要求由6到12位字母、數字、下劃線組成,只有字母能夠開頭;(1分)java

2登陸密碼:要求顯示「• 」或「*」表示輸入位數,密碼要求八位以上字母、數字組成。(1分)mysql

3性別:要求用單選框或下拉框實現,選項只有「男」或「女」;(1分)正則表達式

4學號:要求八位數字組成,前四位爲「2018」開頭,輸入本身學號;(1分)sql

5姓名:輸入本身的姓名;數據庫

5電子郵箱:要求判斷正確格式xxxx@xxxx.xxxx;(1分)jsp

6點擊「添加」按鈕,將學生我的信息存儲到數據庫中。(3分)post

7能夠演示鏈接上數據庫。(2分)this

bean包

package text.jsp.bean;
public class UserBean {
	private String id;
	private String password;
	private String sex;
	private String name;
	private String number;
	private String mail;
	private String yuan;
	private String xi;
	private String classes;
	private String time;
	private String place;
	public String getId() {
		return id;
	}
	public void setId(String 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 String getNumber() {
		return number;
	}
	public void setNumber(String number) {
		this.number = number;
	}
	public String getMail() {
		return mail;
	}
	public void setMail(String mail) {
		this.mail = mail;
	}
	public String getYuan() {
		return yuan;
	}
	public void setYuan(String yuan) {
		this.yuan = yuan;
	}
	public String getXi() {
		return xi;
	}
	public void setXi(String xi) {
		this.xi = xi;
	}
	public String getClasses() {
		return classes;
	}
	public void setClasses(String classes) {
		this.classes = classes;
	}
	public String getTime() {
		return time;
	}
	public void setTime(String time) {
		this.time = time;
	}
	public String getPlace() {
		return place;
	}
	public void setPlace(String place) {
		this.place = place;
	}
	public UserBean(String id,String password,String sex,String name,String number,String mail,String yuan,String xi,String classes,String time,String place){
		this.id=id;
		this.password=password;
		this.sex=sex;
		this.name=name;
		this.number=number;
		this.mail=mail;
		this.yuan=yuan;
		this.xi=xi;
		this.classes=classes;
		this.time=time;
		this.place=place;
	}
}

  dao包

package text.jsp.dao;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import text.jsp.bean.UserBean;
import text.jsp.util.DBUtil;

public class UserDao {
	public boolean add(UserBean user) {
		String sql = "insert into text1021(id,password,sex,name,number,mail,yuan,xi,classes,time,place) values('" + user.getId() + "','" + user.getPassword() + "','" + user.getSex()  + "','" + user.getName()  + "','" + user.getNumber()  + "','" + user.getMail() + "','" + user.getYuan() +"','"+user.getXi() + "','" +user.getClasses() + "','" +user.getTime() + "','" + user.getPlace()+"')";
		DBUtil  db=new DBUtil();
		Connection conn =  db.getCon();//   ÷           
		Statement state = null;
		boolean f = false;
		int a = 0 ;
		
		try {         
			state = conn.createStatement();
			a = state.executeUpdate(sql);
		} catch (Exception e) {           
			e.printStackTrace();
		} finally {
				    
			DBUtil.close(state, conn);
		}
		
		if (a > 0) {
			f = true;
		}
		return f;
	}		
	
}

  util包

package text.jsp.util;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class DBUtil
{   
	private static Connection con;
    private static Statement stm;
    private static ResultSet rs;
	private static String classname="com.mysql.cj.jdbc.Driver";
	private static String url="jdbc:mysql://localhost:3306/gs?useUnicode=true&characterEncoding=UTF-8&serverTimezone=GMT&&useSSL=false&&allowPublicKeyRetrieval=true";
	public  Connection getCon(){    	  
    	try{
    		Class.forName(classname);
    		System.out.println("驅動加載成功");
    	}
    	catch(ClassNotFoundException e){
    		e.printStackTrace();
    	}
    	try{
    	    con=DriverManager.getConnection(url,"root","123456");
    	    System.out.println("數據庫鏈接成功");
    	    
    	}
    	catch(Exception e){
    	    e.printStackTrace(System.err);
    		con=null;
    	}
    	return con;
    }
	public static void close(Statement stm, Connection conn) {
		if(stm!=null) {
			try {
				stm.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		if(conn!=null) {
			try {
				conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}
	
	public static void close(ResultSet rs, Statement stm, Connection con) {
		if(rs!=null) {
			try {
				rs.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		if(stm!=null) {
			try {
				stm.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		if(con!=null) {
			try {
				con.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}
}

  servlet包

package text.jsp.servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import text.jsp.dao.UserDao;
import text.jsp.bean.UserBean;

/**
 * Servlet implementation class textservlet
 */
@WebServlet("/textservlet")
public class textservlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public textservlet() {
        super();
        // TODO Auto-generated constructor stub
    }
    UserDao userDao=new UserDao();
	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		response.setContentType("textml;charset=UTF-8");
		response.setHeader("content-type", "textml;charset=UTF-8");
		 String id=request.getParameter("gs1");
		 String password=request.getParameter("gs2");
		 String sex=request.getParameter("p");
		 String name=request.getParameter("gs4");
		 String number=request.getParameter("gs5");
		 String mail=request.getParameter("gs6");
		 String yuan=request.getParameter("gs7");
		 String xi=request.getParameter("gs8");
		 String classes=request.getParameter("gs9");
		 String time=request.getParameter("p2");
		 String place=request.getParameter("gs11");
		 UserBean userbean=new UserBean( id, password, sex, name, number, mail, yuan, xi, classes, time, place);
		 if(userDao.add(userbean)) {
				request.getRequestDispatcher("success.jsp").forward(request,response);
			}
			else {
				request.getRequestDispatcher("fail.jsp").forward(request,response);
			}
	}

	/**
	 * @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);
	}

}

  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>
<form  method="post" action="textservlet" onsubmit="return check()">
<div > 
<label for="gs1">登陸帳號:</label>
<input type="text" id="gs1" placeholder="請輸入用戶名"  name="gs1"> 
</div>
<div > 
<label for="gs2">登陸密碼:</label>
<input type="password" id="gs2" placeholder="請輸入密碼" class="input-text input-long" name="gs2"> 
</div>
<div > 
<label for="gs3">姓   別:</label>
<select name="p">
  <option value ="nan">男</option>
  <option value ="nv">女</option>
<select>
</div>
<div > 
<label for="gs4"  >姓   名:</label>
<input type="text" id="gs4" placeholder="請輸入姓名" class="input-text input-long" name="gs4"> 
</div>
<div > 
<label for="gs5"  >學   號:</label>
<input type="text" id="gs5" placeholder="請輸入學號" class="input-text input-long" name="gs5"> 
</div>
<div > 
<label for="gs6"  >電子郵件:</label>
<input type="text" id="gs6" placeholder="請輸入郵件" class="input-text input-long" name="gs6"> 
</div>
<div > 
<label for="gs7"  >所在學院:</label>
<input type="text" id="gs7" placeholder="請輸入所在學院" class="input-text input-long" name="gs7"> 
</div>
<div > 
<label for="gs8"  >所在系:</label>
<input type="text" id="gs8" placeholder="請輸入所在系" class="input-text input-long" name="gs8"> 
</div>
<div > 
<label for="gs9"  >所在班級:</label>
<input type="text" id="gs9" placeholder="請輸入所在班級" class="input-text input-long" name="gs9"> 
</div>
<div > 
<label for="gs10">入學年份(屆):</label>
<select name="p2">
  <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>
</div>
<div > 
<label for="gs11"  >生源地:</label>
<input type="text" id="gs11"  name="gs11"> 
</div>
<div > 
<label for="gs12"  >備註:</label>
<input type="text" id="gs12"  name="gs12"> 
</div>
<div>
<input type="submit" id="xuan" name="xuan" value="添加">
</div>
</form>
<!-- 輸入字段驗證部分 -->
<script type="text/javascript">
function check(){
	var username=document.getElementById("gs1");
	var password=document.getElementById("gs2");
	var number=document.getElementById("gs5");
	var mail=document.getElementById("gs6");
	var sReg = /[_a-zA-Z\d\-\.]+@[_a-zA-Z\d\-]+(\.[_a-zA-Z\d\-]+)+$/; //正則表達式
	//判斷用戶名位數
	if((username.value).length<6||(username.value).length>12){
		alert('帳號請輸入6到12位英文字符或數字,以英文字母開頭');
		gs1.focus();
		return false;
	}
	//判斷用戶名是否包含漢字
	if(/.*[\u4e00-\u9fa5]+.*$/.test(username.value)){
		alert('帳號用戶名不能包含漢字');
		gs1.focus();
		return false;
	}
	//判斷用戶名是否以英文字母開頭
	if(!isNaN(username.value[0])){
		alert('登陸帳號請以英文字母開頭');
		gs1.focus();
		return false;
	}
	//判斷密碼位數
	if((password.value).length!=8){
		alert('密碼應爲8位英文或數字');
		gs2.focus();
		return false;
	}
	//判斷學號是否以2018開頭
     if(number.value<"20180000"|| number.value>"20189999")
				{
				 alert(" 學號由2018開頭的八位組成");		        
	             gs5.focus();
	             return false;
				}

	//驗證手機號是否合法
	
	//判斷郵箱格式是否正確
	if(! sReg.test(mail.value)){	
		alert('郵箱格式錯誤');
		gs6.focus();
		return false;
	}	
}
</script>
<!-- 驗證結束 -->
</body>
</html>
相關文章
相關標籤/搜索