Using MVC framework to develop a small project for users to log in, register and log out

introduction

This year, due to the epidemic, the school didn't start. All the final exams were online exams, and the students were also happy to open their flowers. With the conclusion of Java Web, the teacher also arranged a big assignment to develop a project with MVC architecture. Dragon Boat Festival special to write a blog to record my first MVC project, new arrival, if there are shortcomings, please forgive.

About MVC

MVC is just introduced here, which is simply a layered design concept. It can be roughly divided into three layers: M for Model, V for View, and C for controller. Why use the concept of layering? Because when we develop a large project, if there is no layering, you need to change the whole project when you modify some requirements. After the concept of layering appears, you only need to change a small part.

About my project

This project implements three functions: registration, login and logout. This logout does not delete data from the database, but exits, that is, logout session.
The effect is as follows:
main interface:

Login interface:

Login success interface

Registration interface

Basic structure


This is the general frame of our teacher's painting:

Let's start with the view, main page index.jsp , the login registration is in the JSP folder, login.jsp,register.jsp . cn.mvc.code Responsible for verification code, cn.mvc.control Responsible for the controller, cn.mvc.service It is the interface from the controller to the business logic layer, cn.mvc.Impl It is responsible for implementing the business logic layer, cn.mvc.D is the interface between business logic layer and data access layer, cn.mvc.Dao.Impl is responsible for data access, cn.mvc.domain It's responsible for data encapsulation, cn.mvc.Util Is the basic responsible document of the toolkit Reading of content.

Core code

controller:

public class UserServlet2 extends HttpServlet {
	
	private UserService us;
	
	/***
	 * Constructor is used to retrieve the information in the file
	 * Then instantiate the class
	 * Give this class to us
	 */
	public UserServlet2(){
		super();
		InputStream in = this.getClass().getClassLoader().getResourceAsStream("interface-info.properties");
		Properties pro = new Properties();
		try {
			pro.load(in);//Read the value of the list
			String classname = pro.getProperty("UserService");//Take out the value of the corresponding key
			try {
				us = (UserService) Class.forName(classname).newInstance();//Instantiate this class
			} catch (Exception e) {		
				e.printStackTrace();
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	/***
	 * Overridden doGet method
	 * Feedback the request from the main page to the doGet method
	 * login is returned and register is returned. logout returned
	 * Pass parameters through request and redirect various pages from different situations
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		request.setCharacterEncoding("gb2312");
		System.out.println("servlet Connection succeeded!");
		String action = request.getParameter("action");
	
		if("login".equals(action)){//Login request
			System.out.println("You're asking for a command....");

			String username = request.getParameter("username");
			String password = request.getParameter("password");
			
			try {
				if(us.login(username, password)){
					 HttpSession session = request.getSession();//Create a session object
					    session.setAttribute("name", username);//assignment
					response.sendRedirect(request.getContextPath()+"/index.jsp");
				}else{
					//request.setAttribute("loginerro", "user name input error, please login again");
					request.getRequestDispatcher("/JSP/user/login.jsp").forward(request, response);//Request forwarding
				}
			} catch (SQLException e) {
				
				e.printStackTrace();
			}
			
		}else if(action.equals("register")){//Registration request
			System.out.println("What you're issuing is a processing command....");

			UserRegister user = new UserRegister(); 
			
			String username = request.getParameter("username");
			String userpet  = request.getParameter("userpet");
			String password1 = request.getParameter("password1");
			String password2 = request.getParameter("password2");
			String useryear = request.getParameter("useryear");
			String  usersex = request.getParameter("usersex");
			//System.out.println(usersex);
			
			//Determine whether the two passwords entered are consistent
			if(password1.equals(password2)){
				
				user.setUsername(username);
				user.setPassword1(password1);
				user.setUserpet(userpet);
				user.setUsersex(usersex);
				user.setUseryear(useryear);
				
				try {
					if(us.register(user)){
						request.getRequestDispatcher("/JSP/user/login.jsp").forward(request, response);//forward				
					}else{
						request.getRequestDispatcher("/JSP/user/register.jsp").forward(request, response); //forward
					}
				} catch (SQLException e) {
					e.printStackTrace();
				}				
			}else{
				request.getRequestDispatcher("/JSP/user/register.jsp").forward(request, response); //forward
			}
			
		}else if(action.equals("logout")){//Logout request
			System.out.println("You are issuing a logout command....");
			HttpSession session = request.getSession();
			Enumeration<String> em = session.getAttributeNames();//Get all the values in the session
			while(em.hasMoreElements()){//Traverse all values
				session.removeAttribute(em.nextElement().toString());//Invalidate all session s
			}			
			response.sendRedirect(request.getContextPath()+"/index.jsp");//redirect
		}	
			
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
       this.doGet(request, response);
	}

}

Business logic layer code:

public class UserServiceImpl implements UserService {
	
	/***
	 * Take the information out of the file first
	 * Then instantiate the ud
	 */
		
  private UserDao ud;	

  public UserServiceImpl() {
		super();
		InputStream in = this.getClass().getClassLoader().getResourceAsStream("interface-info.properties");
		Properties pro = new Properties();
		
		try {
			pro.load(in);
			String classname = pro.getProperty("UserDao");		
			try {
				ud = (UserDao) Class.forName(classname).newInstance();
			} catch (Exception e) {			
				e.printStackTrace();
			}
			
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

  @Override
  public boolean login(String username, String password) throws SQLException {
		
		/**
		 * Ask Dao to check whether there is data in the database
		 * If it returns true, if it does not return false 
		 */
		
		try {
			if(ud.findUser(username, password)){
				return true;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return false;
	}


  @Override  
  public boolean register(UserRegister user) throws SQLException {
		
		/***
		 * Take username out of user first
		 * Comparing with data in database
		 * If there is no username in the database, it can be saved
		 * If the username is in the database, the user name is registered
		 */
	    String username = user.getUsername();
	    //System.out.println(username);
		if(ud.findUser(username)){
			ud.addUser(user);//Deposit		
			return true;
		}else{		
			return false;
		}	
	}
}

Two interface Codes:

public interface UserService {
	
	/***
	 * 
	 * @param username : user name
	 * @param password : password
	 * @return   If true is returned correctly, false is returned otherwise
	 * register Method is used for registration. First, verify whether the user name is unique. If it is not unique, return false. If it is unique, save the data
	 */
	boolean login(String username,String password) throws SQLException;//Sign in

	boolean register(UserRegister user) throws SQLException;//register
}

public interface UserDao {

	/***
	 * The first findUser method is used to process the login and compare the user name and password with the database
	 * The second findUser method is used for registration to verify that the user name is unique
	 * addUser The method is to pass the user registration information to UserDaoImplMysql
	 */
	public boolean findUser (String name, String password) throws SQLException;  //Process login
	public boolean findUser (String name) throws SQLException;  //Process registration, user name uniqueness
	public User addUser(UserRegister user) throws SQLException;
}

summary

The above is the basic content of this MVC project. If you want a complete package, you can send it to me personally. I wish you a happy Dragon Boat Festival~

Tags: JSP Session Database Java

Posted on Thu, 25 Jun 2020 01:36:36 -0400 by Threepwood