1.Application
实现了用户间数据的共享,可存放全局变量。用于保存整个WebApplication的生命周期内都可以访问的数据,类似浏览量。在用户的前后连接或不同用户之间的连接中,可以对application对象的同一属性进行操作。在任何地方对application对象属性的操作,都将影响到其他用户对此的访问,application开始于服务器启动,终止于服务器的关闭。
在API中表现为ServletContext(每个Webapplication都有很多Servlet再运行,servlet运行的上下文环境),
通过HttpServlet方法可以拿到,通过ServletContext的
get/setAttribute方法取得/设置相关属性。
Enumeration getAttributeNames()
String getServerInfo()
public class TestServletContext extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html;charset=gb2312");
PrintWriter out = response.getWriter();
// Returns a reference to the ServletContext in which this servletis running.
ServletContext application = this.getServletContext();
// 从当前application中读取属性accessCount的值
Integer accessCount = (Integer) application.getAttribute("accessCount");
if (accessCount == null) {
accessCount = new Integer(0);
} else {
accessCount = new Integer(accessCount.intValue() + 1);
}
// 向当前application中插入键(key,属性)值(value)对
application.setAttribute("accessCount", accessCount);
out.println("<html><head><title>ServletContext测试</title></head><br>"
+ "<body><td>" + accessCount + "</td>\n"
+ "</body></html>");
}
}
结果:在打开的窗口中,会出现数字0,刷新增加,不论在打开新窗口还是会增加,不会从0开始。
放在包里面的Servlet类,在Servlet配置中要把包里面的情况写清楚。
2.JavaBean(组件)
广义javabean=普通的java类
狭义javabean=符合sun javabean标准的类
JavaBean可分为两种:一种是有用户界面(UI,UserInterface)的JavaBean;还有一种是没有用户界面,主要负责处理事务(如数据运算,操纵数据库)的JavaBean。JSP通常访问的是后一种JavaBean。JavaBean是可复用的平台独立的软件组件,开发者可以在软件构造器工具中对其直接进行可视化操作。
在servlet中使用Bean和通常程序中使用Bean类似:属性名称第一个字母必须小写,比如:productId,一般具有gettersand setters,但Bean不应该具有GUI表现,一般是用来实现某一业务逻辑或取得特定结果。
javabean设计原则:公有类,属性私有,无参的公有构造方法,getter和setter方法
如何使用javabeans:
1.先使用普通的java类一样创建javabean实例
2.在jsp页面中通常使用jsp动作标签使用javabeans
<jsp:useBeans>
在jsp页面中实例化或者在指定范围内使用javabean
<jsp:useBean id="标识符" class="java类名" scope="作用范围(page)">
scope属性:
<jsp:getProperty>
作用:获取指定javabean对象的属性值
<jsp:getProperty name="javaBean实例名" property=“属性名”/>
示例:
直接连接数据库showRs.java
在servlet中使用数据库连接bean
连接数据库的程序:
public class ShowRs extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)throws ServletException,IOException{
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
resp.setContentType("text/html");
resp.setCharacterEncoding("gb2312");
PrintWriter out = resp.getWriter();
out.println("<tableborder=1>");
out.println("<tr><td>Content:</td></tr>");
try {
Class.forName("org.gjt.mm.mysql.Driver");
//需要将mysql-connector-java-5..3-bin.jar包放到WebRoot\WEB-INF\lib下
//org.gjt.mm.mysql.Driver是com.mysql.jdbc.Driver的前身,
//如果你的项目中用到的是org.gjt.mm.mysql.Driver驱动,
//只是为了兼容性。和用com.mysql.jdbc.Driver没区别。
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "root");
//包括数据库的相关数据(test为文件名),数据库名,数据库密码
stmt = conn.createStatement();
rs = stmt.executeQuery("select * from tablename1");//tablename1为表名
while (rs.next()) {
out.println("<tr>");
out.println("<td>" + rs.getString("name") + "</td>");
out.println("</tr>");
}
out.println("</table>");
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} finally {
if (rs != null) {
try {
rs.close();
rs = null;
} catch (SQLException sqlEx) {
sqlEx.printStackTrace();
}
}
if (stmt != null) {
try {
stmt.close();
stmt = null;
} catch (SQLException sqlEx) {
sqlEx.printStackTrace();
}
}
if (conn != null) {
try {
conn.close();
conn = null;
} catch (SQLException sqlEx) {
sqlEx.printStackTrace();
}
}
}
}
}
结果会在网页上显示表tablename1中的name项的值。
所谓bean就是在一个程序中调用其他的类。
下面程序为专门跟数据库打交道的类 ,可以在其他类中调用,便是所谓的bean
public class DB {
//获取连接
public static Connection getConnection() {
Connection conn = null;
try {
Class.forName("org.gjt.mm.mysql.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "root");
} catch (ClassNotFoundException e) {
System.out.println("驱动程序未找到,请加入mysql.jdbc的驱动包。。。");
e.printStackTrace();
} catch (SQLException e) {
System.out.println("执行数据库连接过程中出现了错误。。。");
e.printStackTrace();
}
return conn;
}
//获取表达式语句
public static Statement getStatement(Connection conn) {
Statement stmt = null;
try {
if (conn != null) {
stmt = conn.createStatement();
}
} catch (SQLException e) {
System.out.println("执行获取表达式语句过程中出现了错误。。。");
e.printStackTrace();
}
return stmt;
}
//获取查询的结果集
public static ResultSet getResultSet(Connection conn, String sql) {
Statement stmt = getStatement(conn);
ResultSet rs = getResultSet(stmt, sql);
close(stmt);
return rs;
}
public static ResultSet getResultSet(Statement stmt, String sql) {
ResultSet rs = null;
try {
if (stmt != null) {
rs = stmt.executeQuery(sql);
}
} catch (SQLException e) {
System.out.println("执行查询过程中出现了错误。。。");
e.printStackTrace();
}
return rs;
}
//关闭连接
public static void close(Connection conn) {
try {
if (conn != null) {
conn.close();
conn = null;
}
} catch (SQLException e) {
System.out.println("执行关闭数据库连接过程中出现了错误。。。");
e.printStackTrace();
}
}
//关闭表达式语句
public static void close(Statement stmt) {
try {
if (stmt != null) {
stmt.close();
stmt = null;
}
} catch (SQLException e) {
System.out.println("执行关闭表达式语句过程中出现了错误。。。");
e.printStackTrace();
}
}
//关闭结果集
public static void close(ResultSet rs) {
try {
if (rs != null) {
rs.close();
rs = null;
}
} catch (SQLException e) {
System.out.println("执行关闭结果集过程中出现了错误。。。");
e.printStackTrace();
}
}
}
通过ShowRsUseBean利用DB中的bean:
public class ShowRsUseBean extends HttpServlet {
protected void doGet(HttpServletRequest req, HttpServletResponse resp)throws ServletException,IOException{
resp.setContentType("text/html");
resp.setCharacterEncoding("gb2312");
PrintWriter out = resp.getWriter();
out.println("<tableborder=1>");
out.println("<tr><td>Content:</td></tr>");
Connection conn = DB.getConnection();
Statement stmt = DB.getStatement(conn);
String sql = "select * from tablename1";
ResultSet rs = DB.getResultSet(stmt, sql);
try {
while (rs.next()) {
out.println("<tr>");
out.println("<td>" + rs.getString("name") + "</td>");
out.println("</tr>");
}
out.println("</table>");
} catch (SQLException e) {
e.printStackTrace();
} finally {
DB.close(rs);
DB.close(stmt);
DB.close(conn);
}
}
}
结果同ShowRs