jsp–MVC案例

jsp--MVC案例
jsp--MVC案例
三层优化
1.加入接口
建议面向接口开发:先接口-再实现类
service、dao加入接口
–接口与实现类的命名规范
接口:interface, 起名 I实体类Service IStudentService
IStudentDao
实现类:implements 起名 实体类ServiceImpl StudentServiceImpl
StudentDaoImpl
接口: I实体类层所在包名 IStudentService、IStudentDao
接口所在的包: xxx.service xx.dao

	实现类:	 实体类层所在包名Impl	StudentServiceImpl、StudentDaoImpl
		实现类所在的包:xxx.service.impl		xx.dao.impl

以后使用接口/实现类时,推荐写法:
接口 x = new 实现类();
IStudentDao studentDao = new StudentDaoImpl();

案例:实现用户登录验证
login.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>登录</title>
</head>
<body>
	<form action="LoginServlet" method="post">
		用户名:<input type="text" name="uname"><br>
		密码:<input type="password" name="upwd"><br>
		<input type="submit" value="登录">
	</form>
</body>
</html>

welcome.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>
	登录成功!
</body>
</html>

com.xdr.entity–Login.java

package com.xdr.entity;

public class Login {
	private int id;
	private String uname;
	private String upwd;
	public Login() {
		
	}
	
	public Login(String uname, String upwd) {
		super();
		this.id = id;
		this.uname = uname;
		this.upwd = upwd;
	}
	
	public Login(int id, String uname, String upwd) {
		super();
		this.id = id;
		this.uname = uname;
		this.upwd = upwd;
	}
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getUname() {
		return uname;
	}
	public void setUname(String uname) {
		this.uname = uname;
	}
	public String getUpwd() {
		return upwd;
	}
	public void setUpwd(String upwd) {
		this.upwd = upwd;
	}
}

com.xdr.dao–loginDao.java

package com.xdr.dao;

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

import com.xdr.entity.Login;

//模型层:用于处理登录(查询数据库)
public class LoginDao {
	public static int login(Login login) {//登录
		//boolean flag = false;//登录成功与否标识
		int flag = -1; //-1系统异常 ,0用户名或密码有误 1登陆成功
		int result = -1;
		Connection connection = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		try {
		Class.forName("com.mysql.jdbc.Driver");
		 connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/jsp01","root","123456");
		
		String sql = "select count(*) from login where uname=? and upwd=?";
		 pstmt = connection.prepareStatement(sql);
		pstmt.setString(1, login.getUname());
		pstmt.setString(2, login.getUpwd());
		 rs = pstmt.executeQuery();
			if(rs.next()) {
				result = rs.getInt(1);
			}
			if(result>0) {
				return 1;
			}else {
				return 0;//登录失败(用户名或密码有误!)
			}
		}catch(ClassNotFoundException e) {
			e.printStackTrace();
			return -1;//登录失败(系统异常!)
		}catch(SQLException e) {
			e.printStackTrace();
			return -1;
		}catch(Exception e) {
			e.printStackTrace();
			return -1;
		}finally {
			try {
				if(rs!=null) rs.close();
				if(pstmt!=null) pstmt.close();
				if(connection!=null) connection.close();
			}catch(SQLException e) {
				e.printStackTrace();
			}catch(Exception e) {
				e.printStackTrace();
			}
		}
		
	}
}

com.xdr.servlet–LoginServlet.java

package com.xdr.servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.xdr.dao.LoginDao;
import com.xdr.entity.Login;

//控制层:接受view请求,并分发给model
public class LoginServlet extends HttpServlet {

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//处理登录
		request.setCharacterEncoding("utf-8");
		String name = request.getParameter("uname");
		String pwd = request.getParameter("upwd");
		Login login = new Login(name,pwd);//用户名,密码
		
		//调用陪你过模型层的 登录功能
		int result = LoginDao.login(login);
		if(result>0) {//成功
			response.sendRedirect("welcome.jsp");
		}else {//登录失败
			response.sendRedirect("login.jsp");
		}
		
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}

web.xml配置添加,就能直接从根目录下访问项目了
jsp--MVC案例
数据库信息如下:
jsp--MVC案例
jsp--MVC案例
输入正确的用户信息后:
jsp--MVC案例
输入错误的信息会重新跳转到登录面:
jsp--MVC案例

文章版权声明

 1 原创文章作者:4234,如若转载,请注明出处: https://www.52hwl.com/35199.html

 2 温馨提示:软件侵权请联系469472785#qq.com(三天内删除相关链接)资源失效请留言反馈

 3 下载提示:如遇蓝奏云无法访问,请修改lanzous(把s修改成x)

 免责声明:本站为个人博客,所有软件信息均来自网络 修改版软件,加群广告提示为修改者自留,非本站信息,注意鉴别

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023年7月15日 下午5:46
下一篇 2023年7月15日 下午5:46