前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >jsp--MVC案例

jsp--MVC案例

作者头像
兮动人
发布2021-06-11 11:21:27
3810
发布2021-06-11 11:21:27
举报
文章被收录于专栏:兮动人的博客

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

代码语言:javascript
复制
	实现类:	 实体类层所在包名Impl	StudentServiceImpl、StudentDaoImpl
		实现类所在的包:xxx.service.impl		xx.dao.impl

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

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

代码语言:javascript
复制
<%@ 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

代码语言:javascript
复制
<%@ 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

代码语言:javascript
复制
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

代码语言:javascript
复制
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

代码语言:javascript
复制
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配置添加,就能直接从根目录下访问项目了

数据库信息如下:

输入正确的用户信息后:

输入错误的信息会重新跳转到登录面:

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2019/10/13 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档