spring实现登陆页面(三层)

1.页面效果:

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

错误信息登陆:

2.页面结构

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

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

3.数据库表:

数据库结构:

stuinfo表结构:

4.代码:

jsp文件:

login.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 action="loginServlet" method="post">
账号:<input type="text" name="username"/><br>
密码:<input type="password" name="password"/><br>
<input type="submit" name="submit">
</form>
</body>
</html>

hello.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>
欢迎${user.name }登陆
</body>
</html>

xml文件:

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>spring_login</display-name>
  <!-- 默认加载页面 -->
  <welcome-file-list>
    <welcome-file>login.jsp</welcome-file>
  </welcome-file-list>
  
  <!-- 为applicationContext添加监听 -->
  <listener>   
     <listener-class>   
          org.springframework.web.context.ContextLoaderListener   
     </listener-class>   
  </listener>   
  
  <!-- 配置ioc容器的路径 -->
  <context-param>   
       <param-name>contextConfigLocation</param-name>   
       <param-value>classpath:ioc.xml</param-value>   
</context-param>   
  
  <!-- loginServlet -->
  <servlet>
    <description></description>
    <display-name>LoginServlet</display-name>
    <servlet-name>LoginServlet</servlet-name>
    <servlet-class>com.bd.web.LoginServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>LoginServlet</servlet-name>
    <url-pattern>/loginServlet</url-pattern>
  </servlet-mapping>
</web-app>

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>com.wxl</groupId>
	<artifactId>spring_login</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>war</packaging>

	<dependencies>


		<!-- spring-web -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-web</artifactId>
			<version>4.3.18.RELEASE</version>
		</dependency>

		<!-- spring-jdbc -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-jdbc</artifactId>
			<version>4.3.18.RELEASE</version>
		</dependency>

		<!-- mysql-connector-java -->
		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
			<version>5.1.18</version>
		</dependency>

		<!-- spring-tx -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-tx</artifactId>
			<version>4.3.18.RELEASE</version>
		</dependency>

		<!-- aop -->
		<!-- AspectJ -->
		<dependency>
			<groupId>org.aspectj</groupId>
			<artifactId>aspectjrt</artifactId>
			<version>1.6.11</version>
		</dependency>

		<dependency>
			<groupId>org.aspectj</groupId>
			<artifactId>aspectjweaver</artifactId>
			<version>1.6.11</version>
		</dependency>

		<dependency>
			<groupId>cglib</groupId>
			<artifactId>cglib</artifactId>
			<version>2.1</version>
		</dependency>

		<!-- spring-beans -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-beans</artifactId>
			<version>4.3.18.RELEASE</version>
		</dependency>

		<!-- spring-core -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-core</artifactId>
			<version>4.3.18.RELEASE</version>
		</dependency>


		<!-- spring-context -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-context</artifactId>
			<version>4.3.18.RELEASE</version>
		</dependency>

		<!-- spring-expression -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-expression</artifactId>
			<version>4.3.18.RELEASE</version>
		</dependency>

		<!-- commons-logging -->
		<dependency>
			<groupId>commons-logging</groupId>
			<artifactId>commons-logging</artifactId>
			<version>1.2</version>
		</dependency>

		<!-- junit -->
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>3.8.1</version>
			<scope>test</scope>
		</dependency>

	</dependencies>

</project>

ioc.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

	<!--加载外界文件 -->
	<context:property-placeholder
		location="classpath:db.properties" />
	<!-- 创建数据库的驱动 -->
	<bean name="dirveManage"
		class="org.springframework.jdbc.datasource.DriverManagerDataSource">
		<property name="driverClassName" value="${driverClass}" />
		<property name="url" value="${url}" />
		<property name="username" value="${user}" />
		<property name="password" value="${password}" />
	</bean>

	<!-- 创建jdbcTemplate对象,用来对数据库进行操作 -->
	<bean name="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
		<property name="dataSource" ref="dirveManage"></property>
	</bean>

	<!-- 开启注解 -->
	<context:component-scan base-package="com.bd" />

</beans>

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<User> 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();
	}
	
	
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

靓仔很忙i

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值