JAVA/Struts2,JS/JQuery,HTML/CSS基礎語法。html
MyEclipse 10java
演示地址sql
預覽截圖(擡擡你的鼠標就能夠看到演示地址哦):apache
關於UI部分請查看下列連接,有詳細製做步驟:ide
前段時間學校剛學完Struts2-Action篇,又自學了一點AJAX/JQuery,到網上看了一些CSS3知識。忽然想要不要乾脆作一個用戶註冊與登入功能。下面是JAVA部分的核心代碼, 若是這樣的邏輯和你們想的頗有出入的話,歡迎拍磚劈斧,呵呵。學習
UserAction.javathis
package action; import java.io.IOException; import java.io.PrintWriter; import java.util.LinkedList; import org.apache.struts2.ServletActionContext; import com.opensymphony.xwork2.ActionContext; import com.opensymphony.xwork2.ActionSupport; import dao.UserDao; import entity.User; public class UserAction extends ActionSupport { private String contentType = "text/html;charset=utf-8"; private User user; private LinkedList<User> users; public LinkedList<User> getUsers() { return users; } public void setUsers(LinkedList<User> users) { this.users = users; } public User getUser() { return user; } public void setUser(User user) { this.user = user; } /** * 查詢用戶 登入驗證 * @return * @throws IOException */ public void select() throws IOException{ //指定輸出內容類型和編碼 ServletActionContext.getResponse().setContentType(contentType); //獲取輸出流,而後使用 PrintWriter out = null; out = ServletActionContext.getResponse().getWriter(); this.user=new UserDao().select(user); //給this.user賦值 if(user==null){ out.print("登入失敗"); }else{ ActionContext actionContext=ActionContext.getContext(); actionContext.getSession().put("user",user); actionContext.getSession().put("users",new UserDao().getList()); out.print("登入成功"); } out.flush(); out.close(); } /** * 添加用戶控制器 * @throws Exception */ public void add() throws IOException{ //指定輸出內容類型和編碼 ServletActionContext.getResponse().setContentType(contentType); //獲取輸出流,而後使用 PrintWriter out = null; out = ServletActionContext.getResponse().getWriter(); int rs=new UserDao().add(this.user); if(rs==1){ ActionContext actionContext=ActionContext.getContext(); actionContext.getSession().put("user",user); actionContext.getSession().put("users",new UserDao().getList()); } out.print(rs); out.flush(); out.close(); //System.out.print(new UserDao().add(this.user)); 這裏不能在用 System.out.print() 不然後臺報錯 } public String upd(){ return null; } public String del(){ return null; } /*@Override public String execute() throws Exception { // TODO Auto-generated method stub return super.execute(); }*/ }
UserDao.java編碼
package dao; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; import java.util.LinkedList; import java.util.List; import tools.ConvertJson; import tools.JDBCUtilSingle; import entity.User; public class UserDao { /** * 插入操做 註冊功能 * @param user 用戶實例 POJO * @return 操做標記 1成功 2郵箱存在 3用戶名存在 */ public int add(User user){ Connection connection=null; PreparedStatement statement=null; ResultSet rs=null; connection=JDBCUtilSingle.getInitJDBCUtil().getConnection(); String sql="select * from form2_user where name=? or email=?"; try { statement=connection.prepareStatement(sql); statement.setString(1,user.getName()); statement.setString(2, user.getEmail()); rs=statement.executeQuery(); if(rs.next()){ if(rs.getString("email").equals(user.getEmail())){return 2;} //2郵箱存在 if(rs.getString("name").equals(user.getName())){return 3;} //3用戶名存在 } sql="INSERT INTO form2_user (`id`, `email`, `name`, `pass`) VALUES (NULL,?,?,?)"; statement=connection.prepareStatement(sql); statement.setString(1,user.getEmail()); statement.setString(2,user.getName() ); statement.setString(3, user.getPass()); statement.executeUpdate(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally{ JDBCUtilSingle.getInitJDBCUtil().closeConnection(rs, statement, connection); } return 1; //1表示成功註冊 } /** * 用戶登陸 放回登入用戶對象信息 * @param user 用戶對象 * @return */ public User select(User user){ Connection connection=null; PreparedStatement statement=null; ResultSet rs=null; User myUser=null; connection=JDBCUtilSingle.getInitJDBCUtil().getConnection(); String sql="select * from form2_user where (name=? or email=?) and pass=?"; try { statement=connection.prepareStatement(sql); statement.setString(1,user.getName()); statement.setString(2,user.getName()); statement.setString(3,user.getPass()); rs=statement.executeQuery(); if(rs.next()){ myUser=new User(); myUser.setName(rs.getString("name")); myUser.setEmail(rs.getString("email")); myUser.setPass(rs.getString("pass")); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ JDBCUtilSingle.getInitJDBCUtil().closeConnection(rs, statement, connection); } return myUser; } /** * 獲取全部用戶信息 * @return 用戶集合 */ public LinkedList<User> getList(){ Connection connection=null; PreparedStatement statement=null; ResultSet rs=null; User myUser=null; LinkedList<User> users=new LinkedList<User>(); connection=JDBCUtilSingle.getInitJDBCUtil().getConnection(); String sql="select * from form2_user"; try { statement=connection.prepareStatement(sql); rs=statement.executeQuery(); while(rs.next()){ myUser=new User(rs.getString("email"),rs.getString("name"), rs.getString("pass")); users.add(myUser); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ JDBCUtilSingle.getInitJDBCUtil().closeConnection(rs, statement, connection); } return users; } }
User.javaspa
package entity; public class User { private String email; private String name; private String pass; public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPass() { return pass; } public void setPass(String pass) { this.pass = pass; } public User(){} public User(String email,String name, String pass){ this.email=email; this.name=name; this.pass=pass; } }
呵呵,又結束了,不知到大家看懂了沒。請原諒童鞋我目前的表述能力只能到這了。歡迎你們來拍磚來劈斧,但願我幼小的心靈能抗得住。code
如以上文章或連接對你有幫助的話,別忘了在文章結尾處輕輕點擊一下 「還不錯」按鈕或到頁面右下角點擊 「贊一個」 按鈕哦。你也能夠點擊頁面右邊「分享」懸浮按鈕哦,讓更多的人閱讀這篇文章。