spring实现登陆页面(三层)

1.页面效果:

正确信息登陆:(跳转到欢迎页面)

spring实现登陆页面(三层)_第1张图片spring实现登陆页面(三层)_第2张图片

错误信息登陆:

spring实现登陆页面(三层)_第3张图片spring实现登陆页面(三层)_第4张图片

 

2.页面结构

页面使用的是maven和spring开发的,文件结构如下

spring实现登陆页面(三层)_第5张图片

提示错误是因为jdk版本问题,对程序运行结果无影响。

3.数据库表:

数据库结构:

spring实现登陆页面(三层)_第6张图片

stuinfo表结构:spring实现登陆页面(三层)_第7张图片

4.代码:

jsp文件:

login.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>




Insert title here


账号:
密码:

hello.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>




Insert title here


欢迎${user.name }登陆

xml文件:

web.xml



  spring_login
  
  
    login.jsp
  
  
  
     
        
          org.springframework.web.context.ContextLoaderListener   
        
     
  
  
     
       contextConfigLocation   
       classpath:ioc.xml   
   
  
  
  
    
    LoginServlet
    LoginServlet
    com.bd.web.LoginServlet
  
  
    LoginServlet
    /loginServlet
  

pom.xml


	4.0.0
	com.wxl
	spring_login
	0.0.1-SNAPSHOT
	war

	


		
		
			org.springframework
			spring-web
			4.3.18.RELEASE
		

		
		
			org.springframework
			spring-jdbc
			4.3.18.RELEASE
		

		
		
			mysql
			mysql-connector-java
			5.1.18
		

		
		
			org.springframework
			spring-tx
			4.3.18.RELEASE
		

		
		
		
			org.aspectj
			aspectjrt
			1.6.11
		

		
			org.aspectj
			aspectjweaver
			1.6.11
		

		
			cglib
			cglib
			2.1
		

		
		
			org.springframework
			spring-beans
			4.3.18.RELEASE
		

		
		
			org.springframework
			spring-core
			4.3.18.RELEASE
		


		
		
			org.springframework
			spring-context
			4.3.18.RELEASE
		

		
		
			org.springframework
			spring-expression
			4.3.18.RELEASE
		

		
		
			commons-logging
			commons-logging
			1.2
		

		
		
			junit
			junit
			3.8.1
			test
		

	

ioc.xml




	
	
	
	
		
		
		
		
	

	
	
		
	

	
	

properties文件

db.properties

driverClass=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/stuinfomange
user=root
password=123456

web层(servlet)

LoginServlet.java

package com.bd.web;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.bd.domain.User;
import com.bd.service.LoginService;

public class LoginServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	// servlet中不要直接使用注解自动获取对象,会出现空指针。为什么?不知道
	//@Autowired
	//LoginService loginService;

	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// 创建ioc容器
		ApplicationContext ioc = WebApplicationContextUtils.getWebApplicationContext(getServletContext());

		// 获取用户的账号和密码
		String username = request.getParameter("username");
		String password = request.getParameter("password");

		// @Component,@Controller,@Service,@Repository
		// 自动创建对象,使用上述4个标签后,可直接获取对象
		//为什么只能获取loginServiceImp的bean不能获取loginService?
		LoginService loginService = (LoginService) ioc.getBean("loginServiceImp");

		// 验证用户账号和密码
		User user=loginService.select(username, password);
		request.setAttribute("user", user);
		if(user==null) 
			response.sendRedirect("login.jsp");
		else request.getRequestDispatcher("hello.jsp").forward(request, response);

	}

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

}

service层(.java)

LoginService.java

package com.bd.service;

import org.springframework.stereotype.Service;

import com.bd.domain.User;

@Service("loginService")
public interface LoginService {
	User select(String username,String password);
}

LoginServiceImp.java

package com.bd.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.bd.dao.UserDao;
import com.bd.domain.User;

@Service
public class LoginServiceImp implements LoginService{

	@Autowired
	UserDao userDao;
	public User select(String username,String password) {
		
		return userDao.select(username, password);
	}

}

dao层(.java)

UserDao.java

package com.bd.dao;

import org.springframework.stereotype.Component;

import com.bd.domain.User;

@Component
public interface UserDao {
	User select(String username,String password);
}

UserDaoImp.java

package com.bd.dao;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Component;

import com.bd.domain.User;

@Component
public class UserDaoImp implements UserDao{
	@Autowired
	JdbcTemplate jdbcTemplate;
	public User select(String username,String password) {
		
	    RowMapper mapper = new BeanPropertyRowMapper<>(User.class);
	    User user=null;
		try {
			user = jdbcTemplate.queryForObject("select * from stuinfo where id=? and password=?", mapper,username,password);
		} catch (Exception e) {
			user=null;
		}
	    
		return user;
	}

}

domain层(.java)

User.java

package com.bd.domain;

import org.springframework.stereotype.Component;

@Component
public class User {
	String id;
	String name;
	String addr;
	String age;
	String password;
	int balance;
	public User(String id, String name, String addr, String age, String password, int balance) {
		super();
		this.id = id;
		this.name = name;
		this.addr = addr;
		this.age = age;
		this.password = password;
		this.balance = balance;
	}
	public int getBalance() {
		return balance;
	}
	public void setBalance(int balance) {
		this.balance = balance;
	}
	@Override
	public String toString() {
		return "User [id=" + id + ", name=" + name + ", addr=" + addr + ", age=" + age + ", password=" + password
				+ ", balance=" + balance + "]";
	}
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getAddr() {
		return addr;
	}
	public void setAddr(String addr) {
		this.addr = addr;
	}
	public String getAge() {
		return age;
	}
	public void setAge(String age) {
		this.age = age;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	public User(String id, String name, String addr, String age, String password) {
		super();
		this.id = id;
		this.name = name;
		this.addr = addr;
		this.age = age;
		this.password = password;
	}
	public User() {
		super();
	}
	
	
}

 

你可能感兴趣的:(java框架)