日期:2014-05-16 浏览次数:20531 次
package demo.web; import java.io.IOException; import javax.servlet.ServletConfig; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.hsqldb.Server; public class PathServlet extends HttpServlet { private Server server = new Server(); public void destroy() { server.shutdown(); super.destroy(); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { } public void init(ServletConfig config) throws ServletException { String args[] = new String[0]; String path=config.getServletContext().getRealPath("/WEB-INF/lib/db/"); server.setDatabaseName(0, "hsqlDemo"); server.setDatabasePath(0, path); server.setPort(9001); server.setSilent(true); Thread thread=new Thread(new Runnable(){ public void run() { server.start(); } }); thread.start(); } }
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <servlet> <servlet-name>PathServlet</servlet-name> <servlet-class>demo.web.PathServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>PathServlet</servlet-name> <url-pattern>/PathServlet</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>