Registration and logon judgment --- background code (1), --- background

Source: Internet
Author: User

Registration and logon judgment --- background code (1), --- background

I am a student at this stage. I have no experience writing code. I am writing it through my feelings. Please point out the error. Thank you.

Background code:

Train of Thought: Pass the username and password of the front-end text box to the background Action with parameters, and then query. If the query result is 0, the user does not exist; otherwise, after receiving the values 0 and 1 through Ajax, you can determine

Action

1 package net. xx. controller; 2 3 import java. io. IOException; 4 import java. io. printWriter; 5 import java. io. unsupportedEncodingException; 6 import java.net. URLDecoder; 7 8 import org. apache. struts2.ServletActionContext; 9 import org. springframework. beans. factory. annotation. autowired; 10 import org. springframework. context. annotation. scope; 11 import org. springframework. stereotype. controller; 12 13 import com. opensymphony. xwork2.Action; 14 15 import net. xx. domain. usert; 16 import net. xx. service. IUserService; 17 18 @ Controller19 @ Scope ("prototype") 20 public class LoginAction {21 @ Autowired22 private IUserService userService; 23 private Object data4; 24 private Usert usert; 25 private String username; 26 private String password; 27 28 29 public String getUsername () {30 return username; 31} 32 public void setUsername (String username) {33 this. username = username; 34} 35 36 public Object getData4 () {37 return data4; 38} 39 public void setData4 (Object data4) {40 this. data4 = data4; 41} 42 public Usert getUsert () {43 return usert; 44} 45 public void setUsert (Usert usert) {46 this. usert = usert; 47} 48 49 public String getPassword () {50 return password; 51} 52 public void setPassword (String password) {53 this. password = password; 54} 55 public String execute () {56 boolean flag = userService. findUser (usert); 57 if (flag) {58 return Action. SUCCESS; 59} else {60 return Action. INPUT; 61} 62} 63 public String check () throws IOException {64 username = URLDecoder. decode (username, "UTF-8"); 65 password = URLDecoder. decode (password, "UTF-8"); 66 data4 = userService. checkUser (username, password ). size (); 67 int I = (int) data4; 68 String result = ""; 69 if (I = 0) {70 // the user does not exist 71 result = "0"; 72} else {73 result = "1"; 74} 75 PrintWriter out = ServletActionContext. getResponse (). getWriter (); 76 out. write (result. toString (); 77 return Action. SUCCESS; 78} 79 80 81}

Dao Interface

1 package net. xx. dao; 2 3 import java. util. list; 4 5 import net. xx. domain. usert; 6 7 public interface IUserDao {8 // Add and Search Method 9 public void saveUser (Usert user); 10 public Usert findUser (Usert user ); 11 public List <Usert> getUser (); 12 public List <Usert> getUserByUsername (String username); 13 public List <Usert> check (String username, String password ); 14 public List <Usert> checkPassword (String username); 15}

Dao implementation class

1 package net. xx. dao; 2 3 import java. util. list; 4 5 import org. hibernate. sessionFactory; 6 import org. springframework. beans. factory. annotation. autowired; 7 import org. springframework. stereotype. repository; 8 9 import net. xx. domain. usert; 10 @ Repository11 public class implements IUserDao {12 @ Autowired13 private SessionFactory sessionfactory; 14 @ Override15 public void saveUser (Usert user) {16 // TODO Auto-generated method stub17 sessionfactory. getCurrentSession (). save (user); 18} 19 20 @ Override21 public Usert findUser (Usert user) {22 // TODO Auto-generated method stub23 Usert firstuser = new Usert (); 24 String hql = "from User user where user. username = '"+ user. getUsername () + "'and user. password '"+ user. getPassword () + "'"; 25 // put the query result to List26 List <Usert> userlist = (List <Usert>) sessionfactory. getCurrentSession (). createQuery (hql); 27 if (userlist. size ()> 0) {28 firstuser = userlist. get (0); 29} 30 31 return firstuser; 32} 33 // traverse data in the database 34 @ Override35 public List <Usert> getUser () {36 // TODO Auto-generated method stub37 String hql = "from Usert"; 38 List <Usert> users = sessionfactory. getCurrentSession (). createQuery (hql ). list (); 39 return users; 40} 41 42 @ Override43 public List <Usert> getUserByUsername (String username) {44 // TODO Auto-generated method stub45 String hql = "select * from Usert where username = '" + username + "'"; 46 // input parameters, obtain the user information 47 List <Usert> users = (List <Usert>) sessionfactory. getCurrentSession (). createSQLQuery (hql ). list (); 48 return users; 49} 50 51 @ Override52 public List <Usert> check (String username, String password) {53 // TODO Auto-generated method stub54 String hql = "select * from Usert where username = '" + username + "' and password = '" + password + "'"; 55 List <Usert> users = sessionfactory. getCurrentSession (). createSQLQuery (hql ). list (); 56 return users; 57} 58 59 @ Override60 public List <Usert> checkPassword (String username) {61 // TODO Auto-generated method stub62 String hql = "select password from Usert where username = '" + username + "'"; 63 List <Usert> password = sessionfactory. getCurrentSession (). createSQLQuery (hql ). list (); 64 65 return password; 66} 67}

Service Interface

 1 package net.xx.service; 2  3 import java.util.List; 4  5 import net.xx.domain.Usert; 6  7 public interface IUserService { 8    public void saveUser(Usert user); 9    public boolean findUser(Usert user);10    List list();11    public List getUserByUsername(String username);12    List checkUser(String username,String password);13 }

Service implementation class

 1 package net.xx.service; 2  3 import java.util.List; 4  5 import org.springframework.beans.factory.annotation.Autowired; 6 import org.springframework.stereotype.Service; 7  8 import net.xx.dao.IUserDao; 9 import net.xx.domain.Usert;10 @Service11 public class UserServiceImpl implements IUserService{12      @Autowired13      private IUserDao userDao;14     @Override15     public void saveUser(Usert user) {16         // TODO Auto-generated method stub17         userDao.saveUser(user);18     }19     @Override20     public boolean findUser(Usert user) {21         // TODO Auto-generated method stub22         Usert firstuser = userDao.findUser(user);23         if(firstuser.getUsername()!=null){24             return true; 25         }else{26             return false;27         }         28     }29 30     @Override31     public List list() {32         // TODO Auto-generated method stub33         List<Usert> users = userDao.getUser();34         return users;35     }36 37     @Override38     public List getUserByUsername(String username) {39         // TODO Auto-generated method stub40         List<Usert> users =userDao.getUserByUsername(username);41         return users;42     }43 44     @Override45     public List checkUser(String username,String password) {46         // TODO Auto-generated method stub47         List<Usert> users = userDao.check(username,password);48         return users;49     }     50 }

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.