1.struts1 plugin
创建类实现接口org.apache.struts.action.PlugIn;
创建一个空构造函数
实现接口方法
配置struts-config.xml添加<plug-in/>元素
例:
public class WileyPlugin implements PlugIn {
public static final String PROPERTIES = "PROPERTIES";
public WileyPlugin() { }
public void init(ActionServlet servlet, ApplicationConfig applicationConfig) throws javax.servlet.ServletException {
System.err.println("---->The Plugin is starting<----");
Properties properties = new Properties();
try {
File file = new File("/WEB-INF/props.txt");
FileInputStream fis =new FileInputStream(file);
properties.load(fis);
ServletContext context = servlet.getServletContext();
context.setAttribute(PROPERTIES, properties);
Properties rProperties = (Properties)context.getAttribute (PROPERTIES);
System.err.println("---->Prop: LDAP " + rProperties.getProperty("LDAP"));
}
catch (FileNotFoundException fnfe) {
throw new ServletException(fnfe.getMessage());
}
catch (IOException ioe) {
throw new ServletException(ioe.getMessage());
}
}
public void destroy() {
System.err.println("---->The Plugin is stopping<----");
}
}
props文件内容:
LDAP=some ldap server name
struts-config.xml:
<plug-in className="wiley.WileyPlugin"/>
2.通过servlet来实现
新建一个类继承javax.servlet.http.HttpServlet,然后将加载数据的代码放到 init方法就可以了。具体代码如下:
public class StaticClass extends javax.servlet.http.HttpServlet implements javax.servlet.Servlet
{
private static final long serialVersionUID = 1L;
private static String title =" StaticClass ";
public void init(ServletConfig config)
{
System.out.println("public void init");
setdata();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
}
public void setdata()
{
try
{
Properties props = new Properties();
props.setProperty("java.naming.factory.initial",
"org.jnp.interfaces.NamingContextFactory");
props.setProperty("java.naming.provider.url", "192.168.1.17:1099");
props.setProperty("java.naming.factory.url.pkgs",
"org.jboss.naming:org.jnp.interfaces");
InitialContext remotectx = new InitialContext(props);
DeptTempMgr deptTempMgr = (DeptTempMgr)remotectx.lookup("Business/DeptTempMgrBean/remote");
InetAddress inetAddress= deptTempMgr.getLocalIp();
String ipName = inetAddress.getHostAddress();
String homeName = inetAddress.getHostName();
System.out.println("远程调用的主机内网ip是:"+ipName);
System.out.println("远程调用的主机名是:"+homeName);
}
catch (Exception e)
{
System.out.println(e);
}
}
}
然后在web.xml配置一下:
<servlet>
<servlet-name>staticclass</servlet-name>
<servlet-class>
action.util.StaticClass
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>staticclass</servlet-name>
<url-pattern>/servlet/staticclass.jsp</url-pattern>
</servlet-mapping>
其中load-on-startup的含义是:容器启动时加载这个servlet的顺序,正常的取值范围是:负数,0-5,如果是负数或者没有这个标签,则容器在启动时不自动加载 这个servlet,如果是0-5,则按照顺序加载这个servlet,执行初始化方法init()。数字是0-5,加载顺序也是0-5。
3.不管是Struts、Struts2还是其他的web层框架,它们目前基于的技术都是Servlet,只要根据web.xml找到那个启动类,我们就能通过覆盖该类的的init()方法来实现系统的初始化工作。可以写一个listener让它实现ServletContextListener接口,在contextInitialized()方法中做想做的事情。将此listener配置到web.xml中,Servlet容器如tomcat会在启动该web应用程序时调用此方法。
public class InitListener implements ServletContextListener {
public void contextDestroyed(ServletContextEvent sce) {
System.out.println("web exit ... ");
}
public void contextInitialized(ServletContextEvent sce) {
System.out.println("web init ... ");
//系统的初始化工作
// ...
}
}
<?xml version="1.0" encoding="UTF-8"?>
<web-app>
<listener>
<listener-class>fangwei.listener.InitListener</listener-class>
</listener>
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
创建类实现接口org.apache.struts.action.PlugIn;
创建一个空构造函数
实现接口方法
配置struts-config.xml添加<plug-in/>元素
例:
public class WileyPlugin implements PlugIn {
public static final String PROPERTIES = "PROPERTIES";
public WileyPlugin() { }
public void init(ActionServlet servlet, ApplicationConfig applicationConfig) throws javax.servlet.ServletException {
System.err.println("---->The Plugin is starting<----");
Properties properties = new Properties();
try {
File file = new File("/WEB-INF/props.txt");
FileInputStream fis =new FileInputStream(file);
properties.load(fis);
ServletContext context = servlet.getServletContext();
context.setAttribute(PROPERTIES, properties);
Properties rProperties = (Properties)context.getAttribute (PROPERTIES);
System.err.println("---->Prop: LDAP " + rProperties.getProperty("LDAP"));
}
catch (FileNotFoundException fnfe) {
throw new ServletException(fnfe.getMessage());
}
catch (IOException ioe) {
throw new ServletException(ioe.getMessage());
}
}
public void destroy() {
System.err.println("---->The Plugin is stopping<----");
}
}
props文件内容:
LDAP=some ldap server name
struts-config.xml:
<plug-in className="wiley.WileyPlugin"/>
2.通过servlet来实现
新建一个类继承javax.servlet.http.HttpServlet,然后将加载数据的代码放到 init方法就可以了。具体代码如下:
public class StaticClass extends javax.servlet.http.HttpServlet implements javax.servlet.Servlet
{
private static final long serialVersionUID = 1L;
private static String title =" StaticClass ";
public void init(ServletConfig config)
{
System.out.println("public void init");
setdata();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
}
public void setdata()
{
try
{
Properties props = new Properties();
props.setProperty("java.naming.factory.initial",
"org.jnp.interfaces.NamingContextFactory");
props.setProperty("java.naming.provider.url", "192.168.1.17:1099");
props.setProperty("java.naming.factory.url.pkgs",
"org.jboss.naming:org.jnp.interfaces");
InitialContext remotectx = new InitialContext(props);
DeptTempMgr deptTempMgr = (DeptTempMgr)remotectx.lookup("Business/DeptTempMgrBean/remote");
InetAddress inetAddress= deptTempMgr.getLocalIp();
String ipName = inetAddress.getHostAddress();
String homeName = inetAddress.getHostName();
System.out.println("远程调用的主机内网ip是:"+ipName);
System.out.println("远程调用的主机名是:"+homeName);
}
catch (Exception e)
{
System.out.println(e);
}
}
}
然后在web.xml配置一下:
<servlet>
<servlet-name>staticclass</servlet-name>
<servlet-class>
action.util.StaticClass
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>staticclass</servlet-name>
<url-pattern>/servlet/staticclass.jsp</url-pattern>
</servlet-mapping>
其中load-on-startup的含义是:容器启动时加载这个servlet的顺序,正常的取值范围是:负数,0-5,如果是负数或者没有这个标签,则容器在启动时不自动加载 这个servlet,如果是0-5,则按照顺序加载这个servlet,执行初始化方法init()。数字是0-5,加载顺序也是0-5。
3.不管是Struts、Struts2还是其他的web层框架,它们目前基于的技术都是Servlet,只要根据web.xml找到那个启动类,我们就能通过覆盖该类的的init()方法来实现系统的初始化工作。可以写一个listener让它实现ServletContextListener接口,在contextInitialized()方法中做想做的事情。将此listener配置到web.xml中,Servlet容器如tomcat会在启动该web应用程序时调用此方法。
public class InitListener implements ServletContextListener {
public void contextDestroyed(ServletContextEvent sce) {
System.out.println("web exit ... ");
}
public void contextInitialized(ServletContextEvent sce) {
System.out.println("web init ... ");
//系统的初始化工作
// ...
}
}
<?xml version="1.0" encoding="UTF-8"?>
<web-app>
<listener>
<listener-class>fangwei.listener.InitListener</listener-class>
</listener>
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>