日期:2014-05-18  浏览次数:20671 次

如何在运行jsp之前就运行类文件
我要想在login.jsp的代码执行之前做一些工作,比如说,读配置文件,创建连接池,建立连接,写日志文件等等,这些事情都不是通过login.jsp中的代码调用的,而是在显示login.jsp之前做的,请问能否实现

------解决方案--------------------
放在一个bean里,在login.jsp里引用就可以了。
------解决方案--------------------
可以写个监听器或者servlet让它们在容器启动时自动加载,然后进行你需要的操作。
------解决方案--------------------
servlet!
------解决方案--------------------
servlet设置为启动页 (LZ这不可能不会吧)
而不显示内容只有业务逻辑(调用JAVABEAN)
public void doGet or doPost(HttpServletRequest arg0, HttpServletResponse arg1)
throws ServletException, IOException
{
**************你要实现的内容
}
然后在转到login.jsp(这个我认为LZ不可能不会)

RequestDispatcher requestDispatcher = request.getRequestDispatcher( "/login.jsp ");
requestDispatcher.forward(request,response);
或者直接用response.sendRedirect(request.getContextPath()+ "/login.jsp ");

------解决方案--------------------
随服务器启动自动加载servlet:

package com.util.servlet;

import javax.servlet.Servlet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import java.io.IOException;

import com.util.ReadConfigEngine;//读取配置文件的类,根据你的需要自己实现
import com.util.LogEngine;    //记录日志的类,根据你的需要自己实现
//还有其他功能,自己添加实现


/**
*
* 该servlet随系统的启动而自己加载,自动启动一些系统的服务
*/
public class StartService implements Servlet {
private ReadConfigEngine CONFIG_ENGINE = null;
private LogEngine LOG_ENGINE = null;

public StartService() {
}
public void init(ServletConfig parm1) throws javax.servlet.ServletException {
try {
CONFIG_ENGINE = new ReadConfigEngine();
LOG_ENGINE = new LogEngine();

System.out.println( "Starting ReadConfigFile Engine ");
CONFIG_ENGINE.start();
System.out.println( "ReadConfigFile Engine Started ");

System.out.println( "Starting Log Engine ");
LOG_ENGINE.start();
System.out.println( "Auction Log Started ");
} catch (Exception e) {
}
}
public ServletConfig getServletConfig() {
/**@todo Implement this javax.servlet.Servlet method*/
throw new java.lang.UnsupportedOperationException( "Method getServletConfig() not yet implemented. ");
}
public void service(ServletRequest parm1, ServletResponse parm2) throws javax.servlet.ServletException, java.io.IOException {
try {
System.out.println( "Starting ReadConfigFile Engine ");
CONFIG_ENGINE.start();
System.out.println( "ReadConfigFile Engine Started ");

System.out.println( "Starting Log Engine ");
LOG_ENGINE.start();
System.out.println( "Auction Log Started ");
} catch (Exception e) {
}
}
public String getServletInfo() {
/**@todo Implement this javax.servlet.Servlet method*/
throw new java.lang.UnsupportedOperationException( "Method getServletInfo