- 浏览: 46033 次
- 性别:
- 来自: 西安
文章分类
- 全部博客 (46)
- 下载收藏 (1)
- 测试 (0)
- jdbc (0)
- jsp jdbc (1)
- 记些东西。 (0)
- afadfadf (0)
- javascript (1)
- ssh (0)
- extjs (2)
- test (0)
- jquery (1)
- jqmobile (0)
- 123 (0)
- 微博 (1)
- 日语 (0)
- drupal (0)
- java基础 (1)
- java面试 (0)
- shell (1)
- css (1)
- hcl取件 (0)
- php (2)
- yii (0)
- javascript html5 (3)
- photoshop (0)
- 非技术 (1)
- 吉他 (0)
- 健康 (0)
- 微信 (0)
- reactjs (0)
- php linux (0)
- nodejs (0)
- es6 (1)
- angular (0)
- webpack (0)
- storybook react组件化 (0)
- webstorm (0)
- h5 (0)
- php nginx (0)
- 兼容性 (0)
- karma phantom mocha chai 单元测试 (0)
- 开发 (0)
- 面试 (0)
- php mac (0)
- git (0)
- 博客 (0)
- js (0)
- https ssl (0)
- react redux es6 (0)
- 111 (0)
- promise javascript (0)
- fis3 构建 打包 (0)
- es7 async (0)
- flex (0)
- react-router (0)
- npm (0)
- react (0)
- guitar (0)
- rem (0)
- 设计模式 (0)
- 原型 prototype (0)
- web知识 (0)
- withCredentials (0)
- css flex (0)
- vue vuex (0)
- charles (0)
- babel (0)
- koa2 mock (0)
- 柯里化 (0)
- 正则 (0)
- restful (0)
- web (0)
- xss csrf (1)
- webpack rollup (1)
- redux-saga (0)
- 原理 (0)
- 1111 (0)
最新评论
-
eddie_520:
特意登录上来发表一句,牛逼
Joomla中jQuery和Mootools解决冲突 -
353386051:
ExtJs菜鸟,表示不会写这样的代码,只会create/new ...
extjs4.1 mvc
[转载] JDBC+Servlet+JSP整合开发之-JSP项目实战
–Servlet 的优势与弊端
–JSP 的优势与弊端
–MVC 设计模式
–实例
?使用MVC实现学生信息的添加、显示
-----------------------------START-----------------------------------
? Servlet 的优势与弊端
–优势
?功能强大,可以调用任意的Java JDK API
?能够实现很多高级特征
?成熟
–弊端
?逻辑处理和内容展示很难分离
?开发效率低
–out.println(“”);
?维护成本高
? JSP 的优势与弊端
–优势
?可以直接嵌入静态HTML
?可以直接写代码
?开发效率高
–弊端
?如果直接在JSP页面中写代码
–程序可读性差
–维护困难
? MVC设计模式
–MVC设计模式早在面向对象语言Smalltalk-80中就被提出并在此后得到业界的广泛接受
–它包括三类对象
?模型(Model)对象
–是应用程序的主体部分
?视图(View)对象
–是应用程序中负责生成用户界面的部分
?控制器(Control)对象
–是根据用户的输入,控制用户界面数据显示及更新Model对象状态的部分
–MVC设计模式的好处
? MVC模式不仅实现了功能模块和显示模块的分离
?同时它还提高了应用系统的
–可维护性
–可扩展性
–可移植性
–组件的可复用性
–JSP 的两种实现模式
–具体实现
? 实例
–使用MVC实现学生信息的添加、显示
Datebase
StudentDao.java
StudentDaoImpl.java
package com.michael.dao.impl;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import com.michael.dao.StudentDao;
import com.michael.util.ConnectionUtil;
import com.michael.util.SQLConstants;
import com.michael.vo.Student;
public class StudentDaoImpl implements StudentDao,SQLConstants {
public void add(Student stu) {
Connection conn = new ConnectionUtil().openConnection();
//String sql = "insert into StudentTbl(name,age,email) values(?,?,?)";
try {
PreparedStatement pstmt = conn.prepareStatement(ADD_STUDENT_SQL);
pstmt.setString(1, stu.getName());
pstmt.setInt(2, stu.getAge());
pstmt.setString(3, stu.getEmail());
pstmt.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}finally{
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
public List listStudent() {
Connection conn = new ConnectionUtil().openConnection();
//String sql = "insert into StudentTbl(name,age,email) values(?,?,?)";
try {
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(QUERY_STUDENT_SQL);
List list = new ArrayList();
while(rs.next()){
int id = rs.getInt(1);
String name = rs.getString(2);
int age = rs.getInt(3);
String email = rs.getString(4);
Student stu = new Student();
stu.setId(id);
stu.setName(name);
stu.setAge(age);
stu.setEmail(email);
list.add(stu);
}
return list;
} catch (SQLException e) {
e.printStackTrace();
}finally{
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
return null;
}
}
ConnectionUtil.java
package com.michael.util;
import java.sql.Connection;
import java.sql.DriverManager;
import java.util.Properties;
public class ConnectionUtil {
/**
* @param args
*/
public static void main(String[] args) {
ConnectionUtil cu = new ConnectionUtil();
System.out.println(cu.openConnection());
}
public Connection openConnection() {
String url = "";
String driver = "";
String user = "";
String password = "";
Properties prop = new Properties();
try {
prop.load(this.getClass().getClassLoader().getResourceAsStream("DBConfig.properties"));
driver = prop.getProperty("driver");
url = prop.getProperty("url");
user = prop.getProperty("user");
password = prop.getProperty("password");
Class.forName(driver);
Connection conn = DriverManager.getConnection(
url, user, password);
return conn;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public Connection getConnection(String driver, String url, String user,
String password) {
// Class.forName()
try {
Class.forName(driver);
// DriverManager get connection
Connection conn = DriverManager.getConnection(url, user, password);
return conn;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public Connection getConnection() {
// Class.forName()
try {
Class.forName("com.mysql.jdbc.Driver");
// DriverManager get connection
Connection conn = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/jsp_db", "root", "963963");
return conn;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
SQLConstants.java
DBConfig.properties
Student.java
StudentDaoImplTest.java
下面进行单元测试
数据库添加成功!
下面继续哈~
stu.jsp
My JSP 'stu.jsp' starting page
姓名:
年龄:
电邮:
IDNameAgeEmail${s.id }
${s.name }
${s.age }
${s.email }
StuServlet.java
package com.michael.servlet;
import java.io.IOException;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.michael.dao.StudentDao;
import com.michael.dao.impl.StudentDaoImpl;
import com.michael.vo.Student;
public class StuServlet extends HttpServlet {
/**
* Constructor of the object.
*/
public StuServlet() {
super();
}
/**
* Destruction of the servlet.
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}
/**
* The doGet method of the servlet.
*
* This method is called when a form has its tag value method equals to get.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request,response);
}
/**
* The doPost method of the servlet.
*
* This method is called when a form has its tag value method equals to post.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String methodName = request.getParameter("methodName");
if(methodName!=null&&methodName.equals("add")){
add(request,response);
}else{
query(request,response);
}
/*
//响应用户请求
String name = request.getParameter("name");
String age = request.getParameter("age");
String email = request.getParameter("email");
//调用后台逻辑
StudentDao dao = new StudentDaoImpl();
Student stu = new Student();
stu.setName(name);
stu.setAge(new Integer(age));
stu.setEmail(email);
dao.add(stu);
List list = dao.listStudent();
request.setAttribute("StuList", list);
//数据处理后跳转
request.getRequestDispatcher("/stu.jsp").forward(request,response);
*/
}
public void add(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//响应用户请求
String name = request.getParameter("name");
String age = request.getParameter("age");
String email = request.getParameter("email");
//调用后台逻辑
StudentDao dao = new StudentDaoImpl();
Student stu = new Student();
stu.setName(name);
stu.setAge(new Integer(age));
stu.setEmail(email);
dao.add(stu);
query(request,response);
}
public void query(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//调用后台逻辑
StudentDao dao = new StudentDaoImpl();
List list = dao.listStudent();
request.setAttribute("StuList", list);
// 跳转
request.getRequestDispatcher("/stu.jsp").forward(request, response);
}
/**
* Initialization of the servlet.
*
* @throws ServletException if an error occurs
*/
public void init() throws ServletException {
// Put your code here
}
}
原文 《JDBC+Servlet+JSP整合开发之30-JDBC、Servlet、JSP的MVC》
http://redking.blog.51cto.com/27212/316441/
–JSP 的优势与弊端
–MVC 设计模式
–实例
?使用MVC实现学生信息的添加、显示
-----------------------------START-----------------------------------
? Servlet 的优势与弊端
–优势
?功能强大,可以调用任意的Java JDK API
?能够实现很多高级特征
?成熟
–弊端
?逻辑处理和内容展示很难分离
?开发效率低
–out.println(“”);
?维护成本高
? JSP 的优势与弊端
–优势
?可以直接嵌入静态HTML
?可以直接写代码
?开发效率高
–弊端
?如果直接在JSP页面中写代码
–程序可读性差
–维护困难
? MVC设计模式
–MVC设计模式早在面向对象语言Smalltalk-80中就被提出并在此后得到业界的广泛接受
–它包括三类对象
?模型(Model)对象
–是应用程序的主体部分
?视图(View)对象
–是应用程序中负责生成用户界面的部分
?控制器(Control)对象
–是根据用户的输入,控制用户界面数据显示及更新Model对象状态的部分
–MVC设计模式的好处
? MVC模式不仅实现了功能模块和显示模块的分离
?同时它还提高了应用系统的
–可维护性
–可扩展性
–可移植性
–组件的可复用性
–JSP 的两种实现模式
–具体实现
? 实例
–使用MVC实现学生信息的添加、显示
Datebase
StudentDao.java
StudentDaoImpl.java
package com.michael.dao.impl;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import com.michael.dao.StudentDao;
import com.michael.util.ConnectionUtil;
import com.michael.util.SQLConstants;
import com.michael.vo.Student;
public class StudentDaoImpl implements StudentDao,SQLConstants {
public void add(Student stu) {
Connection conn = new ConnectionUtil().openConnection();
//String sql = "insert into StudentTbl(name,age,email) values(?,?,?)";
try {
PreparedStatement pstmt = conn.prepareStatement(ADD_STUDENT_SQL);
pstmt.setString(1, stu.getName());
pstmt.setInt(2, stu.getAge());
pstmt.setString(3, stu.getEmail());
pstmt.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}finally{
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
public List listStudent() {
Connection conn = new ConnectionUtil().openConnection();
//String sql = "insert into StudentTbl(name,age,email) values(?,?,?)";
try {
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(QUERY_STUDENT_SQL);
List list = new ArrayList();
while(rs.next()){
int id = rs.getInt(1);
String name = rs.getString(2);
int age = rs.getInt(3);
String email = rs.getString(4);
Student stu = new Student();
stu.setId(id);
stu.setName(name);
stu.setAge(age);
stu.setEmail(email);
list.add(stu);
}
return list;
} catch (SQLException e) {
e.printStackTrace();
}finally{
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
return null;
}
}
ConnectionUtil.java
package com.michael.util;
import java.sql.Connection;
import java.sql.DriverManager;
import java.util.Properties;
public class ConnectionUtil {
/**
* @param args
*/
public static void main(String[] args) {
ConnectionUtil cu = new ConnectionUtil();
System.out.println(cu.openConnection());
}
public Connection openConnection() {
String url = "";
String driver = "";
String user = "";
String password = "";
Properties prop = new Properties();
try {
prop.load(this.getClass().getClassLoader().getResourceAsStream("DBConfig.properties"));
driver = prop.getProperty("driver");
url = prop.getProperty("url");
user = prop.getProperty("user");
password = prop.getProperty("password");
Class.forName(driver);
Connection conn = DriverManager.getConnection(
url, user, password);
return conn;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public Connection getConnection(String driver, String url, String user,
String password) {
// Class.forName()
try {
Class.forName(driver);
// DriverManager get connection
Connection conn = DriverManager.getConnection(url, user, password);
return conn;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public Connection getConnection() {
// Class.forName()
try {
Class.forName("com.mysql.jdbc.Driver");
// DriverManager get connection
Connection conn = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/jsp_db", "root", "963963");
return conn;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
SQLConstants.java
DBConfig.properties
Student.java
StudentDaoImplTest.java
下面进行单元测试
数据库添加成功!
下面继续哈~
stu.jsp
My JSP 'stu.jsp' starting page
姓名:
年龄:
电邮:
IDNameAgeEmail${s.id }
${s.name }
${s.age }
${s.email }
StuServlet.java
package com.michael.servlet;
import java.io.IOException;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.michael.dao.StudentDao;
import com.michael.dao.impl.StudentDaoImpl;
import com.michael.vo.Student;
public class StuServlet extends HttpServlet {
/**
* Constructor of the object.
*/
public StuServlet() {
super();
}
/**
* Destruction of the servlet.
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}
/**
* The doGet method of the servlet.
*
* This method is called when a form has its tag value method equals to get.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request,response);
}
/**
* The doPost method of the servlet.
*
* This method is called when a form has its tag value method equals to post.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String methodName = request.getParameter("methodName");
if(methodName!=null&&methodName.equals("add")){
add(request,response);
}else{
query(request,response);
}
/*
//响应用户请求
String name = request.getParameter("name");
String age = request.getParameter("age");
String email = request.getParameter("email");
//调用后台逻辑
StudentDao dao = new StudentDaoImpl();
Student stu = new Student();
stu.setName(name);
stu.setAge(new Integer(age));
stu.setEmail(email);
dao.add(stu);
List list = dao.listStudent();
request.setAttribute("StuList", list);
//数据处理后跳转
request.getRequestDispatcher("/stu.jsp").forward(request,response);
*/
}
public void add(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//响应用户请求
String name = request.getParameter("name");
String age = request.getParameter("age");
String email = request.getParameter("email");
//调用后台逻辑
StudentDao dao = new StudentDaoImpl();
Student stu = new Student();
stu.setName(name);
stu.setAge(new Integer(age));
stu.setEmail(email);
dao.add(stu);
query(request,response);
}
public void query(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//调用后台逻辑
StudentDao dao = new StudentDaoImpl();
List list = dao.listStudent();
request.setAttribute("StuList", list);
// 跳转
request.getRequestDispatcher("/stu.jsp").forward(request, response);
}
/**
* Initialization of the servlet.
*
* @throws ServletException if an error occurs
*/
public void init() throws ServletException {
// Put your code here
}
}
原文 《JDBC+Servlet+JSP整合开发之30-JDBC、Servlet、JSP的MVC》
http://redking.blog.51cto.com/27212/316441/
- 页面.rar (1.1 MB)
- 下载次数: 23
相关推荐
Java EE WEB 工程师培训------JDBC+Servlet+JSP整合开发
基于JDBC+MySQL+Servlet+JSP+Java实现简单校园论坛系统 基于JDBC+MySQL+Servlet+JSP+Java实现简单校园论坛系统 基于JDBC+MySQL+Servlet+JSP+Java实现简单校园论坛系统 基于JDBC+MySQL+Servlet+JSP+Java实现简单校园...
基于JDBC+JSP+Servlet的图书管理系统基于JDBC+JSP+Servlet的图书管理系统 基于JDBC+JSP+Servlet的图书管理系统基于JDBC+JSP+Servlet的图书管理系统 基于JDBC+JSP+Servlet的图书管理系统基于JDBC+JSP+Servlet的图书...
一个完整的jdbc+servlet+jsp+mysql框架所包含的jar包。
Java+Servlet+Jdbc+Jsp+Mysql实现Web学生信息管理系统,系统经多次测试,运行无误,请大家放心下载
数据库课程作业,基于jsp + jdbc + servlet + javabean的学生管理系统 数据库课程作业,基于jsp + jdbc + servlet + javabean的学生管理系统 数据库课程作业,基于jsp + jdbc + servlet + javabean的学生管理系统 ...
基于JSP+Servlet+JavaBean+JDBC+DAO的Web架构设计该系统,进一步了解并掌握如何对数据库进行操作,以及如何分析、设计一个应用系统。 需求要求: 该系统的基本需求是,系统要实现如下的基本管理功能: (1)用户分为...
基于JSP+JDBC+Servlet的一个简单的客户管理系统 基于JSP+JDBC+Servlet的一个简单的客户管理系统 基于JSP+JDBC+Servlet的一个简单的客户管理系统 基于JSP+JDBC+Servlet的一个简单的客户管理系统 基于JSP+JDBC+Servlet...
基于jsp+jdbc+servlet的个人CMS系统,课程大作业 基于jsp+jdbc+servlet的个人CMS系统,课程大作业 基于jsp+jdbc+servlet的个人CMS系统,课程大作业 基于jsp+jdbc+servlet的个人CMS系统,课程大作业 基于jsp+jdbc+...
本人博客Web基础之Servlet+JDBC+JSP项目实战完整源代码
JSP+Servlet + JDBC+MySQL 注册和登录
适合Java初学者JDBC+Servlet+jsp
基于jsp+js+servlet+jdbc+mysql开发产品后台管理系统,实现用户增删改查、分页,登录,注册,图片上传等功能。 包括以下5个功能: 1、登录 用户默认主页index.jsp,可选择【登录】功能,若登录成功,则进入产品管理中...
jsp+servlet+jdbc实现简单的购物车实例,购物数据通过session存储 1、加入购物车时,把数据保存在session中 2、点击系统退出时,购物车中的数据插入到数据库购物车表 3、再次登录时,查询数据库购物车表的数据加载到...
Java EE WEB工程师培训-JDBC+Servlet+JSP整合开发之09.JDBC DAO设计模式
基于JSP+Servlet+JDBC+MySQL+Bootstrap的考勤管理系统 基于JSP+Servlet+JDBC+MySQL+Bootstrap的考勤管理系统 基于JSP+Servlet+JDBC+MySQL+Bootstrap的考勤管理系统 基于JSP+Servlet+JDBC+MySQL+Bootstrap的考勤管理...
基于Jsp+Servlet+JavaBean+JDBC实现登录功能,简单实例
jsp+servlet+jdbc+mysql项目练习资源,里面包含所有项目所需.jar,bootstrap,jquery,和数据库sql文件