[学习笔记]jsp+javaBean+Servlet 的设计模式MVC 的实例
来源:互联网 发布:在线答题软件大学 编辑:程序博客网 时间:2024/05/23 01:59
//运行结果后显示页面
<%@page import="cn.java.BookBean"%>
<html>
<head>
<title>jjjj</title>
</head>
<body>
<jsp:useBean id="bookBean" type="cn.java.BookBean" scope="request"></jsp:useBean>
<ul>
<li>ÊéÃû£º <jsp:getProperty name="bookBean" property="title"/></li>
<li>ÊéºÅ: <jsp:getProperty name="bookBean" property="isbn"/></li>
<li>×÷Õߣº <jsp:getProperty name="bookBean" property="author"/><li>
<li>³ö°æÉ磺<jsp:getProperty name="bookBean" property="publisher"/></li>
<li>¶¨¼Û: <jsp:getProperty name="bookBean" property="price"/></li>
</ul>
</body>
</html>
<%@page import="cn.java.BookBean"%>
<html>
<head>
<title>jjjj</title>
</head>
<body>
<jsp:useBean id="bookBean" type="cn.java.BookBean" scope="request"></jsp:useBean>
<ul>
<li>ÊéÃû£º <jsp:getProperty name="bookBean" property="title"/></li>
<li>ÊéºÅ: <jsp:getProperty name="bookBean" property="isbn"/></li>
<li>×÷Õߣº <jsp:getProperty name="bookBean" property="author"/><li>
<li>³ö°æÉ磺<jsp:getProperty name="bookBean" property="publisher"/></li>
<li>¶¨¼Û: <jsp:getProperty name="bookBean" property="price"/></li>
</ul>
</body>
</html>
//javaBean 程序
package cn.java;
public class BookBean ...{
//ISBN ��
private String isbn;
//����
private String title;
//����
private String author;
//�����
private String publisher;
//�۸�
private int price;
public BookBean()...{
}
public BookBean(String isbn,String title,String autor,String publisher,int price)...{
this.isbn=isbn;
this.title=title;
this.author=author;
this.publisher=publisher;
this.price=price;
}
public String getAuthor() ...{
return author;
}
public void setAuthor(String author) ...{
this.author = author;
}
public String getIsbn() ...{
return isbn;
}
public void setIsbn(String isbn) ...{
this.isbn = isbn;
}
public int getPrice() ...{
return price;
}
public void setPrice(int price) ...{
this.price = price;
}
public String getPublisher() ...{
return publisher;
}
public void setPublisher(String publisher) ...{
this.publisher = publisher;
}
public String getTitle() ...{
return title;
}
public void setTitle(String title) ...{
this.title = title;
}
}
package cn.java;
public class BookBean ...{
//ISBN ��
private String isbn;
//����
private String title;
//����
private String author;
//�����
private String publisher;
//�۸�
private int price;
public BookBean()...{
}
public BookBean(String isbn,String title,String autor,String publisher,int price)...{
this.isbn=isbn;
this.title=title;
this.author=author;
this.publisher=publisher;
this.price=price;
}
public String getAuthor() ...{
return author;
}
public void setAuthor(String author) ...{
this.author = author;
}
public String getIsbn() ...{
return isbn;
}
public void setIsbn(String isbn) ...{
this.isbn = isbn;
}
public int getPrice() ...{
return price;
}
public void setPrice(int price) ...{
this.price = price;
}
public String getPublisher() ...{
return publisher;
}
public void setPublisher(String publisher) ...{
this.publisher = publisher;
}
public String getTitle() ...{
return title;
}
public void setTitle(String title) ...{
this.title = title;
}
}
//Serlvet 程序
import cn.java.BookBean;
import cn.java.DBUtil;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class QueryBookServlet extends HttpServlet...{
/** *//**
*
*/
@Override
public void doGet(HttpServletRequest request,
HttpServletResponse response)throws ServletException,IOException...{
final String SQL ="select * from BookList where isbn=?";
Connection conn=null;
PreparedStatement stmt=null;
ResultSet rs=null;
String address="unknownBook.jsp";
//����javaBean ����
BookBean book=new BookBean();
try...{
//�����ݿ�l��
conn=DBUtil.getConnection();
stmt=conn.prepareStatement(SQL);
//ָ������
stmt.setString(1,request.getParameter("isbn"));
//ִ�� ��ѯ
rs=stmt.executeQuery();
if(rs!=null)...{
rs.next();
book.setIsbn(rs.getString("isbn"));
book.setTitle(rs.getString("title"));
book.setAuthor(rs.getString("author"));
book.setPublisher(rs.getString("publisher"));
book.setPrice(rs.getInt("price"));
address="displayBook.jsp";
}
}catch(SQLException e)...{
e.printStackTrace();
}
//����
DBUtil.closeResultSet(rs);
DBUtil.closeStatement(stmt);
DBUtil.closeConnection(conn);
request.setAttribute("bookBean",book);
//��ת��jspҳ��
RequestDispatcher dis=request.getRequestDispatcher(address);
dis.forward(request,response);
}
}
import cn.java.BookBean;
import cn.java.DBUtil;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class QueryBookServlet extends HttpServlet...{
/** *//**
*
*/
@Override
public void doGet(HttpServletRequest request,
HttpServletResponse response)throws ServletException,IOException...{
final String SQL ="select * from BookList where isbn=?";
Connection conn=null;
PreparedStatement stmt=null;
ResultSet rs=null;
String address="unknownBook.jsp";
//����javaBean ����
BookBean book=new BookBean();
try...{
//�����ݿ�l��
conn=DBUtil.getConnection();
stmt=conn.prepareStatement(SQL);
//ָ������
stmt.setString(1,request.getParameter("isbn"));
//ִ�� ��ѯ
rs=stmt.executeQuery();
if(rs!=null)...{
rs.next();
book.setIsbn(rs.getString("isbn"));
book.setTitle(rs.getString("title"));
book.setAuthor(rs.getString("author"));
book.setPublisher(rs.getString("publisher"));
book.setPrice(rs.getInt("price"));
address="displayBook.jsp";
}
}catch(SQLException e)...{
e.printStackTrace();
}
//����
DBUtil.closeResultSet(rs);
DBUtil.closeStatement(stmt);
DBUtil.closeConnection(conn);
request.setAttribute("bookBean",book);
//��ת��jspҳ��
RequestDispatcher dis=request.getRequestDispatcher(address);
dis.forward(request,response);
}
}
//数据库连接
package cn.java;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;
public class DBUtil ...{
static String serverName="localhost";
static String sDBDriver="com.mysql.jdbc.Driver";
static String url="jdbc:mysql://127.0.0.1/test";
static String dbUser="root";
static String dbPwd="root";
/** *//**
* �õ�һ��Connection
* */
public static Connection getConnection()...{
Connection conn=null;
try ...{
Class.forName(sDBDriver);
conn=DriverManager.getConnection(url, dbUser,dbPwd);
} catch (SQLException ex) ...{
Logger.getLogger(DBUtil.class.getName()).log(Level.SEVERE, null, ex);
} catch (ClassNotFoundException e) ...{
// TODO Auto-generated catch block
e.printStackTrace();
}
return conn;
}
/** *//**
* �ر�ָ���Ľ��
* @param rs Ҫ�رյĽ��
*
* */
public static void closeResultSet(ResultSet rs)...{
if(rs!=null)...{
try...{
rs.close();
}catch(SQLException e)...{
e.printStackTrace();
}
}
}
public static void closeStatement(Statement stmt)...{
if(stmt!=null)...{
try ...{
stmt.close();
} catch (SQLException e) ...{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public static void closeConnection(Connection conn)...{
if(conn!=null)...{
try ...{
conn.close();
} catch (SQLException e) ...{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public static void main(String args[])...{
System.out.println(getConnection());
}
}
package cn.java;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;
public class DBUtil ...{
static String serverName="localhost";
static String sDBDriver="com.mysql.jdbc.Driver";
static String url="jdbc:mysql://127.0.0.1/test";
static String dbUser="root";
static String dbPwd="root";
/** *//**
* �õ�һ��Connection
* */
public static Connection getConnection()...{
Connection conn=null;
try ...{
Class.forName(sDBDriver);
conn=DriverManager.getConnection(url, dbUser,dbPwd);
} catch (SQLException ex) ...{
Logger.getLogger(DBUtil.class.getName()).log(Level.SEVERE, null, ex);
} catch (ClassNotFoundException e) ...{
// TODO Auto-generated catch block
e.printStackTrace();
}
return conn;
}
/** *//**
* �ر�ָ���Ľ��
* @param rs Ҫ�رյĽ��
*
* */
public static void closeResultSet(ResultSet rs)...{
if(rs!=null)...{
try...{
rs.close();
}catch(SQLException e)...{
e.printStackTrace();
}
}
}
public static void closeStatement(Statement stmt)...{
if(stmt!=null)...{
try ...{
stmt.close();
} catch (SQLException e) ...{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public static void closeConnection(Connection conn)...{
if(conn!=null)...{
try ...{
conn.close();
} catch (SQLException e) ...{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public static void main(String args[])...{
System.out.println(getConnection());
}
}
//运行首页
<html>
<title>
isbn
</title>
<body>
<form action="QueryBookServlet" >
<input type="text" name="isbn">
<input type="submit" name="submit" value="submit">
</form>
</body>
</html>
<html>
<title>
isbn
</title>
<body>
<form action="QueryBookServlet" >
<input type="text" name="isbn">
<input type="submit" name="submit" value="submit">
</form>
</body>
</html>
//数据库表建立
create table BookList(
isbn varchar(30) primary key,
title varchar(30) not null,
author varchar(30) not null,
publisher varchar(30) not null,
price int(4)
);
create table BookList(
isbn varchar(30) primary key,
title varchar(30) not null,
author varchar(30) not null,
publisher varchar(30) not null,
price int(4)
);
- [学习笔记]jsp+javaBean+Servlet 的设计模式MVC 的实例
- JSP + JavaBean + Servlet实现MVC设计模式
- 基于JSP + servlet + javabean的MVC简单验证登录实例
- 基于jsp+servlet+javabean的MVC模式简单应用
- MVC模式:jsp+servlet+javabean
- jsp+javabean+servlet的笔记
- jsp + javabean + servlet写的MVC
- JSP学习总结------MVC模式(JDDC 、Servlet 、Jsp的MVC)
- 学习笔记-----JSP+Servlet+Bean的MVC
- jsp+JavaBean+servlet设计模式
- MVC框架详解--Servlet+JSP+JavaBean模式(MVC)开发复杂的web应用
- Jsp+Servlet+JavaBean简单mvc实例
- Jsp+Servlet+JavaBean简单mvc实例
- Jsp+Servlet+JavaBean简单mvc实例
- Jsp+Servlet+JavaBean简单mvc实例
- Jsp+Servlet+JavaBean简单mvc实例
- 浅谈MVC(jsp+servlet+JavaBean简单实例)
- servlet/jsp/javabean学习笔记
- 华为一年
- BI,还是oracle
- PDF iText 使用简单介绍
- 我对C++ VTable 的理解 - theCFan的专栏 - CSDNBlog
- “爱国主义”高涨的今天
- [学习笔记]jsp+javaBean+Servlet 的设计模式MVC 的实例
- 使用C#创建PowerPoint文档
- Java程序把Word文档直接转换成Html文件-开发频道-Java-天极网
- [JAVA使用技巧]Java抽取Word和PDF格式文件_网络大本营
- 到来
- 函数引用型参数的限制
- openSUSE 11.0 Beta 1
- 超强的VIM
- c# static的全部用法收集整理