-1

我正在开发一个计数器的应用程序,下面是我的应用程序的结构..

首先是监听器类

    package com.saral;

import javax.servlet.*;

public class AppListener implements ServletContextListener
{
ServletContext ctx;
public void contextInitialized(ServletContextEvent e)
{
ctx=e.getServletContext();
ctx.setAttribute("hitCount",new Integer(0));
}
public void contextDestroyed(ServletContextEvent e)
{}
}

然后过滤类..

    package com.saral;

import javax.servlet.*;

public class CounterFilter implements Filter
{
ServletContext ctx;
public void init(FilterConfig config){
ctx=config.getServletContext();
}
public void destroy(){}
public void doFilter(ServletRequest request,ServletResponse response,FilterChain chain)
{
try
{
Integer ct=(Integer)ctx.getAttribute("hitCount");
int c=ct.intValue();
c++;
ctx.setAttribute("hitCount",new Integer(c));
chain.doFilter(request,response);
}catch(Exception e)
{
System.out.println(e);
}
}
}

其次是登录servlet ..

    package com.saral;

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;
public class LoginServlet extends HttpServlet
{
public void doGet(HttpServletRequest request,HttpServletResponse response) 
{
try{
String name=request.getParameter("txtName");
String pass=request.getParameter("txtPass");
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe");
PreparedStatement stmt=con.prepareStatement("select * from logininfo where username=? and password=?");
stmt.setString(1,name);
stmt.setString(2,pass);
ResultSet rset=stmt.executeQuery();
response.setContentType("text/html");
PrintWriter out=response.getWriter();
if (rset.next())
{
request.getSession().setAttribute("username",name);
RequestDispatcher rd=request.getRequestDispatcher("aa");
rd.forward(request,response);
}
else
{
System.out.println("in else");
out.println("<b>Invalid user name or password.</b><br>");
RequestDispatcher rd=request.getRequestDispatcher("home.html");
rd.include(request,response);
}
out.close();
con .close();
}catch(Exception e)
{
System.out.println(e);
}
}
}

其次是servlet2 ..

package com.saral;

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class ServletTwo extends HttpServlet
{
public void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException
{
HttpSession ses=request.getSession();
String name=(String)ses.getAttribute("username");
response.setContentType("text/html");
PrintWriter out=response.getWriter();
out.println("welcome,"+name);
out.println("<br> <a href=viewServlet>View Hit Count</a>");
out.close();
}
}

最后查看servlet ..

package com.saral;

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;

public class ViewServlet extends HttpServlet
{
public void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException
{
ServletContext ctx=getServletContext();
int c=((Integer)ctx.getAttribute("hitCount")).intValue();
response.setContentType("text/html");
PrintWriter out=response.getWriter();
out.println("hit count is:"+c);
out.close();
}
}

我以这种方式编写了web.xml..

    <web-app>

<listener>
<listener-class>AppListener</listener-class>
</listener>

<filter>
<filter-name>counter</filter-name>
<filter-class>com.saral.CounterFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>counter</filter-name>
<url-pattern>*</url-pattern>
</filter-mapping>
<servlet>
<servlet-name>hello</servlet-name>
<servlet-class>com.saral.LoginServlet</servlet-class>
<init-param>
<param-name>driverClass</param-name>
<param-value>oracle.jdbc.driver.OracleDriver</param-value>
</init-param>
<init-param>
<param-name>connectionString</param-name>
<param-value>jdbc:oracle:thin:@localhost:1521:xe</param-value>
</init-param>
<init-param>
<param-name>userName</param-name>
<param-value>saral</param-value>
</init-param>
<init-param>
<param-name>password</param-name>
<param-value>saral</param-value>
</init-param>

</servlet>
<servlet-mapping>
<servlet-name>hello</servlet-name>
<url-pattern>/loginServlet</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>two</servlet-name>
<servlet-class>com.saral.ServletTwo</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>two</servlet-name>
<url-pattern>/aa</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>three</servlet-name>
<servlet-class>com.saral.ViewServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>three</servlet-name>
<url-pattern>/viewServlet</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>home.html</welcome-file>
</welcome-file-list>
</web-app>

我的主要html是..

<html>
<head>
<title> A Simple web Application</title>
</head>
<body>
<form method="get" action="loginServlet">
Name<input type="text" name="txtName"/>
Password<input type="password" name="txtPass"/>
<input type="submit" value="Login"/>
</form>
</body>
</html>

但是在 tomcat 7 上部署此应用程序时,它会引发错误。

NFO: Starting Servlet Engine: Apache Tomcat/7.0.27
15 Jul, 2012 12:05:14 PM org.apache.catalina.core.StandardContext listenerStart
SEVERE: Error configuring application listener of class AppListener
java.lang.ClassNotFoundException: AppListener
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1711)
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1556)
    at org.apache.catalina.core.DefaultInstanceManager.loadClass(DefaultInstanceManager.java:525)
    at org.apache.catalina.core.DefaultInstanceManager.loadClassMaybePrivileged(DefaultInstanceManager.java:507)
    at org.apache.catalina.core.DefaultInstanceManager.newInstance(DefaultInstanceManager.java:124)
    at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4715)
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5273)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1566)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1556)
    at java.util.concurrent.FutureTask$Sync.innerRun(Unknown Source)
    at java.util.concurrent.FutureTask.run(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)
15 Jul, 2012 12:05:14 PM org.apache.catalina.core.StandardContext listenerStart
SEVERE: Skipped installing application listeners due to previous error(s)
15 Jul, 2012 12:05:14 PM org.apache.catalina.core.StandardContext startInternal
SEVERE: Error listenerStart
15 Jul, 2012 12:05:14 PM org.apache.catalina.core.StandardContext startInternal
SEVERE: Context [/Counter] startup failed due to previous errors

请告知申请中有什么问题

按照 web.xml 中的建议完成此更改

<listener>
    <listener-class>com.saral.AppListener</listener-class>
</listener>

但仍然出现错误..

SEVERE: Error configuring application listener of class com.saral.AppListener
java.lang.ClassNotFoundException: com.saral.AppListener
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1711)
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1556)
    at org.apache.catalina.core.DefaultInstanceManager.loadClass(DefaultInstanceManager.java:525)
    at org.apache.catalina.core.DefaultInstanceManager.loadClassMaybePrivileged(DefaultInstanceManager.java:507)
    at org.apache.catalina.core.DefaultInstanceManager.newInstance(DefaultInstanceManager.java:124)
    at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4715)
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5273)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1566)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1556)
    at java.util.concurrent.FutureTask$Sync.innerRun(Unknown Source)
    at java.util.concurrent.FutureTask.run(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)
15 Jul, 2012 12:21:13 PM org.apache.catalina.core.StandardContext listenerStart
SEVERE: Skipped installing application listeners due to previous error(s)

请指教

4

1 回答 1

3

在您的 web.xml 中,您有此侦听器配置

<listener>
    <listener-class>AppListener</listener-class>
</listener>

AppListener找不到类,必须设置全名(包括包名):

<listener>
    <listener-class>com.saral.AppListener</listener-class>
</listener>
于 2012-07-15T06:46:00.753 回答