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

解决方案 »

  1.   

    放在一个bean里,在login.jsp里引用就可以了。
      

  2.   

    可以写个监听器或者servlet让它们在容器启动时自动加载,然后进行你需要的操作。
      

  3.   

    第一次来这个区,第一次学习JSP
    顶顶顶。
      

  4.   

    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");
      

  5.   

    随服务器启动自动加载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() not yet implemented.");
      }
      public void destroy() {
        try {
          CONFIG_ENGINE.stop();
          LOG_ENGINE.stop();      CONFIG_ENGINE = null;
          LOG_ENGINE = null;
        }catch (Exception e) {    }
      }}在web.xml文件中加入如下配置:
      <servlet>
        <servlet-name>StartPTCService</servlet-name>
        <servlet-class>com.util.servlet.StartService</servlet-class>
        <load-on-startup>3</load-on-startup>
      </servlet>
      <servlet-mapping>
        <servlet-name>StartService</servlet-name>
        <url-pattern>/StartService</url-pattern>
      </servlet-mapping>
      

  6.   

    同意yanhuaxie(IT Farmer
    你可以设置为随服务器启动的
      

  7.   

    是不是这个意思,服务器启动后首先运行的一定是servlet,可以通过调用这个servlet来实现读配置文件等功能,而这个servlet是通过web.xml来指定的