我在这里用的不是maven项目,用的一般的web项目,所以需要用到的架包需要自己去下载添加,在项目中,一定注意环境的配置,我用的是jre1.7

1  新建项目

2  建立好MVC的管理包,导入对应的架包servlet

3 建立好与数据库对应的实体类 teacher.java

public class Teacher {
private int tid;
private String tname;
private String tpsw;
public int getTid() {
return tid;
}
public void setTid(int tid) {
this.tid = tid;
}
public String getTname() {
return tname;
}
public void setTname(String tname) {
this.tname = tname;
}
public String getTpsw() {
return tpsw;
}
public void setTpsw(String tpsw) {
this.tpsw = tpsw;
}
public Teacher(String tname, String tpsw) {
super();
this.tname = tname;
this.tpsw = tpsw;
}
public Teacher(int tid, String tname, String tpsw) {
super();
this.tid = tid;
this.tname = tname;
this.tpsw = tpsw;
}
public Teacher() {
super();
}
}

4 在WebContent新建login.jsp文件编写登陆框

 <body>
<form action="login" method="post">
用户名:<input type="text" name="tname">
密码:<input type="text" name="tpsw">
<input type="submit" value="登录">
</form>

5  配置web.xml文件对应表单请求login

web.xml文件

注意:配置时<servlet-name>要在之前,否则会报错<servlet-class>

<!-- 提交登录请求 -->
<servlet>
<servlet-name>login</servlet-name>
<servlet-class>com.zr.controller.LoginController</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>login</servlet-name>
<url-pattern>/login</url-pattern>
</servlet-mapping>

6 编写对应的请求实体类LoginController.java:继承HttpServlet重写doget(),dopost()方法,根据method请求的不同调用doget或者dopost方法

LoginController.java

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import com.zr.model.Teacher;
import com.zr.service.valiDateService;
import com.zr.serviceIm.valiDateServiceImpl;
public class LoginController extends HttpServlet{
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
super.doPost(req, resp);
}
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
//获取前台form表单的input输入框
String tname=req.getParameter("tname");
String tpsw=req.getParameter("tpsw");
Teacher tc=new Teacher();
tc.setTname(tname);
tc.setTpsw(tpsw);
valiDateService vds=new valiDateServiceImpl();
Teacher t= vds.valiDateTeacher(tc);
HttpSession session=req.getSession();
session.setAttribute("teacher", t);
if (t!=null) {
//返回的不是空值,重定向到登录成功界面
req.getRequestDispatcher("main.jsp").forward(req, resp);
} else {
//返回空值,请求转发到登录界面
resp.sendRedirect("login.jsp");
}
}
}

7 从后台dao层写到control层

public interface TeacherDao {

    /**
* 验证老师是否存在
* @param tc
* @return
*/
public Teacher validateTeacher(Teacher tc);
}

8.1 编写封装类建立与数据库的连接JDBCUtil.java

package JDBCUtil;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement; public class JDBCUtil {
//1.数据库地址 (根据不同的数据标准是不一样)
private final static String DBURL = "jdbc:mysql://localhost:3306/student_crm?useUnicode=true&characterEncoding=UTF8";
//2.设置用户和密码
private final static String USERNAME = "root";
private final static String PASSWORD = "root";
//3.设置驱动名称 (根据不同的数据标准是不一样)
private final static String DBDRIVER = "com.mysql.jdbc.Driver";
/**
* 获取数据库连接
* @return 返回数据库连接
*/
public static Connection getConnection(){
Connection con = null;
try {
Class.forName(DBDRIVER);
con = DriverManager.getConnection(DBURL, USERNAME, PASSWORD);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return con;
}
//关闭连接
public static void closeJDBC(Statement st,Connection con) throws SQLException{
if(st!=null){
st.close();
}
if(con!=null){
con.close();
}
} }

8.2 dao层的实现

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import com.zr.dao.TeacherDao;
import com.zr.model.Teacher;
import JDBCUtil.JDBCUtil;
public class TeacherDaoImpl implements TeacherDao{
/**
* 输入老师的对象,返回老师对象
* @param args
*/
public Teacher validateTeacher(Teacher tc) {
Teacher teacher=new Teacher();
//sql语句
StringBuffer sql=new StringBuffer("select * from teacher where tname=? and tpsw=?");
//获取数据库连接
Connection con=JDBCUtil.getConnection();
try {
PreparedStatement pst=con.prepareStatement(sql.toString());
pst.setString(1, tc.getTname());
pst.setString(2, tc.getTpsw());
//返回一个结果集
ResultSet rs=pst.executeQuery();
if (rs.next()) {
//把结果集里面的数据放入对应的teacher对象
teacher=new Teacher(rs.getInt("tid"),rs.getString("tname"),rs.getString("tpsw"));
}
} catch (SQLException e) {
e.printStackTrace();
}
return teacher;
}
}

9 Service层

public interface valiDateService {
/**
* @param tc
* @return 老师对象
* 根据用户输入值验证老师是否存在
*/
public Teacher valiDateTeacher(Teacher tc); }

10 Service层实现ServiceImpl.java

import com.zr.dao.TeacherDao;
import com.zr.daoIm.TeacherDaoImpl;
import com.zr.model.Teacher;
import com.zr.service.valiDateService;
public class valiDateServiceImpl implements valiDateService{
public Teacher valiDateTeacher(Teacher tc) {
//父类的引用指向子类的对象,父类可以直接调用子类的方法
TeacherDao teacherDao=new TeacherDaoImpl();
//调用dao层的方法验证存在
Teacher teacher=teacherDao.validateTeacher(tc);
return teacher;
}
}

11 com.zr.controller层

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import com.zr.model.Teacher;
import com.zr.service.valiDateService;
import com.zr.serviceIm.valiDateServiceImpl;
public class LoginController extends HttpServlet {
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
super.doPost(req, resp);
}
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// 获取前台form表单的input输入框
String tname = req.getParameter("tname");
String tpsw = req.getParameter("tpsw");
// 将前台对象放入tc对象,作为输入参数
Teacher tc = new Teacher();
tc.setTname(tname);
tc.setTpsw(tpsw);
// 调用Service层的方法传入tc对象,并用t接收返回结果
valiDateService vds = new valiDateServiceImpl();
Teacher t = vds.valiDateTeacher(tc);
// 获取JSP作用域session,将老师t对象放入session
HttpSession session = req.getSession();
int a = t.getTid();//最好根据返回的老师的id进行判断
if (a != 0) {
// 返回的有id,重定向到登录成功界面
req.getRequestDispatcher("main.jsp").forward(req, resp);
session.setAttribute("teacher", t);
} else {
// 返回空值,请求转发到登录界面
req.getRequestDispatcher("login.jsp").forward(req, resp);
}
}
}

MVC模式实现登录以及增删改查之登录(一)的更多相关文章

  1. asp.net下利用MVC模式实现Extjs表格增删改查

    在网上看到有很多人写extjs下的表格控件的增删改查,但是大多数都是直接从后台读取数据,很少有跟数据库进行交互的模式. 今天就来写一个这样的例子.欢迎大家交流指正. 首先简单介绍一下MVC模式,MVC ...

  2. MVC模式的学生信息增删改查

    准备:建一个名为 userdb的数据库.建一个student表,有stuid,stuname,gender三个字段.其中stuid为主键.j加入相应的驱动包,相应的JSTL标签 先看目录结构 代码: ...

  3. SSH登录与增删改查demo详解+源代码

    点击下载,测试绝对可用SSH整合框架登录加增删改查demo 下载地址:http://download.csdn.net/detail/qq_33599520/9784679   一.框架概述 spri ...

  4. 05_Elasticsearch 单模式下API的增删改查操作

    05_Elasticsearch 单模式下API的增删改查操作 安装marvel 插件: zjtest7-redis:/usr/local/elasticsearch-2.3.4# bin/plugi ...

  5. Elasticsearch 单模式下API的增删改查操作

    <pre name="code" class="html">Elasticsearch 单模式下API的增删改查操作 http://192.168. ...

  6. Elasticsearch学习系列之单模式下API的增删改查操作

    这里我们通过Elasticsearch的marvel插件实现单模式下API的增删改查操作 索引的初始化操作 创建索引之前可以对索引进行初始化操作,比如先指定shard数量以及replicas的数量 代 ...

  7. 招新系统(jsp+servlet,实现简略前端网页注册登录+后台增删改查,分学生和管理员,Java语言,mysql数据库连接,tomcat服务器)

    生活不只是眼前的苟且,还有诗和远方. 架构说明: 要求是采用MVC模式,所以分了下面的几个包,但是由于是第一次写,可能分的也不是很清楚: 这个是后台部分的架构: 这个是前端的的展示: (那个StuLo ...

  8. MVC中的Ajax与增删改查

    自入手新项目以来,一直处于加班状态,博客也有两周没更,刚刚完成项目的两个模组,稍有喘息之机,写写关于项目中 的增删改查,这算是一个老生常谈的问题了,就连基本的教材书上都有.刚看书的时候,以为 没什么可 ...

  9. MVC中的Ajax与增删改查(一)

    自入手新项目以来,一直处于加班状态,博客也有两周没更,刚刚完成项目的两个模组,稍有喘息之机,写写关于项目中 的增删改查,这算是一个老生常谈的问题了,就连基本的教材书上都有.刚看书的时候,以为 没什么可 ...

随机推荐

  1. js字符串倒序

    有的时候我们需要把字符串倒序. 比如“范坚强”的倒序就是“强坚范”. 如何对字符串进行倒序呢?你首先想到的方法就是生成一个栈,从尾到头依次取出字符串中的字符压入栈中,然后把栈连接成字符串. var r ...

  2. Android JNI和NDK关系

    1.什么JNI Java Native Interface(JNI)标准是java平台的一部分,它允许Java代码和其他语言写的代码进行交互.JNI 是本地编程接口,它使得在 Java 虚拟机(VM) ...

  3. 在Linux终端执行clear或top命令时出现:'xterm': unknown terminal type

    在Linux终端执行clear或top命令时出现:'xterm': unknown terminal type的错误. 例如: [root@localhost phpmyadmin]# clear ' ...

  4. EntityFramework - Migrations

    EntityFramework  - Migrations 對項目進行EF的數據庫升級操作.分爲開發環境與部署環境.上的操作總結. 引用: Command說明https://coding.abel.n ...

  5. Bow模型(解释的很好)

    Bag-of-words model (BoW model) 最早出现在NLP和IR领域. 该模型忽略掉文本的语法和语序, 用一组无序的单词(words)来表达一段文字或一个文档. 近年来, BoW模 ...

  6. windows系统安装ubuntu后,grub中没有windows启动项

    我的问题: 安装系统时候,选择grub安装在sdb磁盘 http://forum.ubuntu.org.cn/viewtopic.php?f=139&t=474289&start=15 ...

  7. iscc2016-basic-明察秋毫

    查看源代码,找到maybe not flag : Jr1p0zr2VfPp 移位密码,注意判断字母大小写,并且数字无变化 s = "Jr1p0zr2VfPp" p = list(s ...

  8. 关于Verilog 中的for语句的探讨

    在C语言中,经常用到for循环语句,但在硬件描述语言中for语句的使用较C语言等软件描述语言有较大的区别. 在Verilog中除了在Testbench(仿真测试激励)中使用for循环语句外,在Test ...

  9. PHP+Mysql-表单数据插入数据库及数据提取完整过程

    网站在进行新用户注册时,都会将用户的注册信息存入数据库中,需要的时候再进行提取.今天写了一个简单的实例. 主要完成以下几点功能: (1)用户进行注册,实现密码重复确认,验证码校对功能. (2)注册成功 ...

  10. Integer Intervals(贪心)

    Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 12123   Accepted: 5129 Description An i ...