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

続いてログインサーブレット..

    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();
}
}

最後にサーブレットを表示します..

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 に答える