前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >servlet中使用db4o

servlet中使用db4o

作者头像
全栈程序员站长
发布2022-09-14 21:39:31
1730
发布2022-09-14 21:39:31
举报
文章被收录于专栏:全栈程序员必看

大家好,又见面了,我是你们的朋友全栈君。

在web的工程如果想使用db4o,是不能直接使用的,如果想使用的就得做一些初始化的工作,以便在servlet中使用,具体可以看下面的两个网页有详细的介绍:

http://java.dzone.com/articles/db4o-java

http://community.versant.com/documentation/reference/db4o-8.1/java/reference/Content/platform_specific_issues/web/servlets.htm

可以如此使用:

Db4oServletContextListener.java

代码语言:javascript
复制
import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import com.db4o.Db4oEmbedded;
import com.db4o.EmbeddedObjectContainer;
import com.db4o.ObjectContainer;
import com.db4o.config.EmbeddedConfiguration;

public class Db4oServletContextListener 
	implements ServletContextListener {

	public static final String PREFIX_PATH = "E:/Pattern/PDFBox-v2/";
	public static final String KEY_DB4O_FILE_NAME = "db4oFileName";
	public static final String KEY_DB4O_SERVER = "db4oServer";
	
	
	@Override
	public void contextInitialized(ServletContextEvent event) {
		System.out.println("context oK");
	    ServletContext context = event.getServletContext();
	    String filePath = PREFIX_PATH + context.getInitParameter(KEY_DB4O_FILE_NAME);
	    EmbeddedConfiguration configEmbedded = Db4oEmbedded.newConfiguration();
	    EmbeddedObjectContainer rootContainer = Db4oEmbedded.openFile(configEmbedded,
				filePath);
	    context.setAttribute(KEY_DB4O_SERVER, rootContainer);
	   /* ArrayList<exam> result = new ArrayList<exam>();
		ObjectSet<exam> objectSet = rootContainer.query(exam.class);
		while (objectSet.hasNext()) {
			result.add((exam) objectSet.next());
		}
		exam[] exams = result.toArray(new exam[result.size()]);
		System.out.println(exams.length);*/
	    context.log("db4o startup on " + filePath);
	}


	@Override
	public void contextDestroyed(ServletContextEvent event) {
	    ServletContext context = event.getServletContext();
	    ObjectContainer rootContainer = (ObjectContainer) context.getAttribute(KEY_DB4O_SERVER);
	    context.removeAttribute(KEY_DB4O_SERVER);
	    close(rootContainer);
	    context.log("db4o shutdown");
	}
	
	private void close(ObjectContainer container) {
		if(container != null) {
			container.close();
		}
	}
}

自己的servlet代码:

代码语言:javascript
复制
import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.db4o.EmbeddedObjectContainer;

public class CategoryServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		System.out.println("OK");
		EmbeddedObjectContainer container = (EmbeddedObjectContainer) 
				request.getServletContext().getAttribute("db4oServer");
	}}

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/162583.html原文链接:https://javaforall.cn

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

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

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

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

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