SpringMVC与mysql进行登录验证

SpringMVC可以基于Annotation去请求方法,这次我们来实现SpringMVC+mysql的验证与页面跳转。
创建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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
	version="2.5">

<!-- 这里配置springmvc的serlvet -->
	<servlet>
		<servlet-name>spring</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<load-on-startup>1</load-on-startup>
	</servlet>
	
	<servlet-mapping>
		<servlet-name>spring</servlet-name>
		<url-pattern>/</url-pattern>
	</servlet-mapping>

	<welcome-file-list>
		<welcome-file>welcome.jsp</welcome-file>
	</welcome-file-list>

</web-app>

这里创建了Spring提供的servlet,按照约定,要在WEB-INF下建立对应名字-servlet来配置SpringMVC的控制器扫描和跳转页面的配置,spring-servlet如下:

<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="  
        http://www.springframework.org/schema/beans       
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  
        http://www.springframework.org/schema/context   
        http://www.springframework.org/schema/context/spring-context-3.0.xsd">
        <!-- 需要扫描并注册为bean的包 -->
        <context:component-scan base-package="micro.action"></context:component-scan>
        
        <!-- 视图的解析 -->
        <bean id = "viewModel" class = "org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value = "/" />
        <property name="suffix" value = ".jsp" />
        </bean>

</beans>

分别在根目录下建立登录成功和登录失败页面success.jsp error.jsp:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>成功</title>
</head>
<body>
<h1>${username}登录成功</h1>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>错误</title>
</head>
<body>
<h1>${username}登录失败</h1>
</body>
</html>

默认的欢迎页面如下welcome.jsp:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>欢迎页面</title>
</head>
<body>
<h1>欢迎你</h1>
<a href = "spring">点此请求DispatcherServlet并跳转</a>

<form action="login" method = "post">
username:<input type = "text" name = "username">
<br>
password:<input type = "password" name = "password">
<br>
<input type = "submit" value = "提交">
</form>
</body>
</html>

可以用超链接转到show.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>跳转后的页面</title>
</head>
<body>
<h1>成功进行了跳转</h1>
</body>
</html>

控制器bean:

package micro.action;

import javax.servlet.http.HttpServletRequest;

import micro.service.LoginCheck;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;


@Controller
@RequestMapping
public class FirstSpringMVC {
	
	@RequestMapping("/spring")
	public ModelAndView test()
	{
		String str = "this is a SpringMVC instance!";
		return new ModelAndView("show","str",str);
	}
	
	@RequestMapping("/login")
	public ModelAndView check(HttpServletRequest request)
	{
		String name = request.getParameter("username");
		String password = request.getParameter("password");
		//调用业务处理LoginCheck
		if(LoginCheck.check(name, password))
		{
			return new ModelAndView("success","username",name);
		}
		return new ModelAndView("error","username",name);
	}

}

业务处理模块LoginCheck验证用户:

package micro.service;


import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import micro.dao.Dao;

public class LoginCheck {
	
	public static boolean check(String name,String password)
	{
		//处理业务逻辑
		try
		{
			Connection conn = Dao.getConnection();
			PreparedStatement p = conn.prepareStatement("select * from test where username = ? and password = ?");
			p.setString(1, name);
			p.setString(2, password);
			ResultSet rs = p.executeQuery();
			while(rs.next())
			{
				Dao.close(rs, p, conn);
				return true;
			}
			Dao.close(rs, p, conn);
			
		}
		catch(SQLException e)
		{
			e.printStackTrace();
			System.out.println("数据库连接出错");
		}
		return false;
	}
}

持久层Dao获得与关闭链接:

package micro.dao;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class Dao {
	//获得数据库连接
	public static Connection getConnection()
	{
  		Connection conn = null;
		String url = "jdbc:mysql://localhost:3306/micro";
		try
		{
			Class.forName("com.mysql.jdbc.Driver");
			conn = DriverManager.getConnection(url,"root","root");
		}
		catch(ClassNotFoundException e)
		{
			e.printStackTrace();
			System.out.println("数据库驱动加载出错");
		}
		catch(SQLException e)
		{
			e.printStackTrace();
			System.out.println("数据库出错");
		}
		return conn;
	}
	 //关闭相关通道
	public static void close(ResultSet rs,PreparedStatement p,Connection conn)
	{
		try
		{
			rs.close();
			p.close();
			conn.close();
		}
		catch(SQLException e)
		{
			e.printStackTrace();
			System.out.println("数据关闭出错");
		}
	}
}

数据库字段
数据库字段

运行结果:
这里写图片描述

  • 最后附上web与spring的一般配置:
    spring.xml:
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="  
        http://www.springframework.org/schema/beans       
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  
        http://www.springframework.org/schema/context   
        http://www.springframework.org/schema/context/spring-context-3.0.xsd">

</beans>

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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
	version="2.5">


</web-app>

看了此篇文章是不是感觉收获蛮大

  • 1
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 12
    评论
评论 12
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值