如何从web.xml调用java.class?

时间:2012-05-28 04:14:31

标签: java servlets

我在尝试从web.xml调用java类时出现问题,当我运行我的应用程序时我不能加载我的类/方法?

在我的申请中:
jsf 2.0
主要表面3.1.1
springframework 3.0.2

请帮帮我

的web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" 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_3_0.xsd">

<context-param>
    <param-name>javax.faces.PROJECT_STAGE</param-name>
    <param-value>Production</param-value>
</context-param>

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/app-config.xml</param-value>
</context-param>

<context-param>
    <param-name>javax.faces.FACELETS_LIBRARIES</param-name>
    <param-value>/WEB-INF/springsecurity.taglib.xml</param-value>
</context-param>

<servlet>
    <servlet-name>Spring MVC Dispatcher Servlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/app-config.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>Spring MVC Dispatcher Servlet</servlet-name>
    <url-pattern>*.htm</url-pattern>
</servlet-mapping>

<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<listener>
    <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
</listener>
<listener>
    <listener-class>org.springframework.security.web.session.HttpSessionEventPublisher</listener-class>
</listener>

<filter>
    <filter-name>springSecurityFilterChain</filter-name>
    <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<filter-mapping>
    <filter-name>springSecurityFilterChain</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

<servlet>
    <servlet-name>Faces Servlet</servlet-name>
    <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>Faces Servlet</servlet-name>
    <url-pattern>*.jsf</url-pattern>
</servlet-mapping>

<servlet>
    <servlet-name>Mama</servlet-name>
    <servlet-class>hobic.controller.Mama</servlet-class>
</servlet>

<servlet-mapping>
    <servlet-name>Mama</servlet-name>
    <url-pattern>/hobic.controller</url-pattern>
</servlet-mapping>


</web-app>

Mama.java

public class Mama extends HttpServlet{

public Mama() {
    super();
}

public void metalica() {
    System.out.println("nothing else matter");
    JOptionPane.showMessageDialog(null, "nothing else matter");
}
}

错误

SEVERE: Exception while visiting CryptoMeWindow$1.class of size 122769
java.lang.ArrayIndexOutOfBoundsException: 130207
    at org.objectweb.asm.ClassReader.<init>(Unknown Source)
    at org.objectweb.asm.ClassReader.<init>(Unknown Source)
    at org.objectweb.asm.ClassReader.<init>(Unknown Source)
    at org.glassfish.hk2.classmodel.reflect.Parser$5.on(Parser.java:362)
    at com.sun.enterprise.v3.server.ReadableArchiveScannerAdapter.handleEntry(ReadableArchiveScannerAdapter.java:171)
    at com.sun.enterprise.v3.server.ReadableArchiveScannerAdapter.onSelectedEntries(ReadableArchiveScannerAdapter.java:133)
    at org.glassfish.hk2.classmodel.reflect.Parser.doJob(Parser.java:348)
    at org.glassfish.hk2.classmodel.reflect.Parser.access$300(Parser.java:70)
    at org.glassfish.hk2.classmodel.reflect.Parser$3.call(Parser.java:307)
    at org.glassfish.hk2.classmodel.reflect.Parser$3.call(Parser.java:296)
    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303)
    at java.util.concurrent.FutureTask.run(FutureTask.java:138)
    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
    at java.lang.Thread.run(Thread.java:662)

INFO: SEC1002: Security Manager is OFF.
INFO: SEC1010: Entering Security Startup Service
INFO: SEC1143: Loading policy provider    com.sun.enterprise.security.provider.PolicyWrapper.
INFO: SEC1115: Realm [admin-realm] of classtype  [com.sun.enterprise.security.auth.realm.file.FileRealm] successfully created.
INFO: SEC1115: Realm [file] of classtype [com.sun.enterprise.security.auth.realm.file.FileRealm] successfully created.
INFO: SEC1115: Realm [certificate] of classtype [com.sun.enterprise.security.auth.realm.certificate.CertificateRealm] successfully created.
INFO: SEC1011: Security Service(s) Started Successfully
INFO: Instantiated an instance of org.hibernate.validator.engine.resolver.JPATraversableResolver.
INFO: WEB0169: Created HTTP listener [http-listener-1] on host/port [0.0.0.0:8080]
INFO: WEB0169: Created HTTP listener [http-listener-2] on host/port [0.0.0.0:8181]
INFO: WEB0169: Created HTTP listener [admin-listener] on host/port [0.0.0.0:4848]
INFO: WEB0171: Created virtual server [server]
INFO: WEB0171: Created virtual server [__asadmin]
INFO: WEB0172: Virtual server [server] loaded default web module []
INFO: JTS5014: Recoverable JTS instance, serverId = [3700]
INFO: Portable JNDI names for EJB TestSessionBean : [java:global/Hobic/Hobic-   ejb/TestSessionBean!hobic.view.TestSessionBeanRemote, java:global/Hobic/Hobic- ejb/TestSessionBean]
INFO: Glassfish-specific (Non-portable) JNDI names for EJB TestSessionBean :   [hobic.view.TestSessionBeanRemote#hobic.view.TestSessionBeanRemote,  hobic.view.TestSessionBeanRemote]
INFO: Initializing Mojarra 2.1.3 (FCS b02) for context '/Hobic-war'
INFO: Instantiated an instance of  org.hibernate.validator.engine.resolver.JPATraversableResolver.
WARNING: JSF1074: Managed bean named 'loginBean' has already been registered.     Replacing existing managed bean class type hobic.controller.LoginBean with  hobic.controller.LoginBean.
INFO: Running on PrimeFaces 3.1.1
INFO: PWC1412: WebModule[null] ServletContext.log():Initializing Spring root  WebApplicationContext
SEVERE: log4j:WARN No appenders could be found for logger  (org.springframework.web.context.ContextLoader).
SEVERE: log4j:WARN Please initialize the log4j system properly.
SEVERE: log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more  info.
INFO: Instantiated an instance of  org.hibernate.validator.engine.resolver.JPATraversableResolver.
INFO: PWC1412: WebModule[null] ServletContext.log():Initializing Spring FrameworkServlet 'Spring MVC Dispatcher Servlet'
INFO: Instantiated an instance of org.hibernate.validator.engine.resolver.JPATraversableResolver.
INFO: WEB0671: Loading application [Hobic#Hobic-war.war] at [Hobic-war]
INFO: Hobic was successfully deployed in 45,269 milliseconds.

3 个答案:

答案 0 :(得分:2)

您的web.xml说

<servlet>
    <servlet-name>Mama</servlet-name>
    <servlet-class>hobic.controller.Mama</servlet-class>
</servlet>

<servlet-mapping>
    <servlet-name>Mama</servlet-name>
    <url-pattern>/hobic.controller</url-pattern>
</servlet-mapping>

实际上,您的Mama类只是一个简单的POJO,其中嵌入了一些Swing代码。

为了使Mama成为Servlet,它应该扩展HttpServlet,并覆盖其中一个服务方法。

另请从Mama删除所有Swing代码,Swing用于独立的Java应用程序以用于UI目的,它对于Web应用程序也没有价值,对于驻留在服务器上的代码也是如此。

所以你的Mama应该是这样的:

public class Mama extends HttpServlet {

   // override service method(s) that you intend to support.

}

阅读基本教程here

答案 1 :(得分:0)

在web.xml中,您只能指定扩展HttpServlet的servlet类。你不能在servlet类里面摆动代码。所以请删除挥杆代码。

答案 2 :(得分:0)

您在应用程序中使用了错误的配置样式。请参阅这些链接http://www.mkyong.com/jsf2/jsf-2-0-spring-integration-example/,这可能会对您有所帮助。请不要在Web应用程序中使用swing代码,并且在扩展HttpServlet时必须编写doGet()或doPost()方法。