关于servlet执行时的错误

时间:2012-07-15 14:51:20

标签: java tomcat servlets

我用servlet开发了下面的应用程序,但是在输入用户名和密码后我没有得到第二页..请告知...

第一个servlet是..

package Demo;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


public class SendRedirectServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    protected void doPost(HttpServletRequest request, HttpServletResponse
            response)throws ServletException, IOException {
          response.setContentType("text/html");
          PrintWriter pw = response.getWriter();
          String name = request.getParameter("username");
          String password = request.getParameter("password");
          if(name.equals("james")&& password.equals("abc")){
          response.sendRedirect("Demo/ValidUserServlet");
          }
          else{
          pw.println("u r not a valid user");
          }
          }
}

第二个servlet是..

package Demo;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


public class ValidUserServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    protected void doGet(HttpServletRequest request, HttpServletResponse
              response)throws ServletException, IOException {
              PrintWriter pw = response.getWriter();
              pw.println("Welcome to roseindia.net " + " ");
              pw.println("how are you");
            }

}

并且login.html文件是......

<html>

<head>
<title>New Page 1</title>
</head>

<body>

<form method="post" action="SendRedirectServlet">
  <p>Enter your name&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
   <input type="text" name="username" size="20"/></p>
  <p>Enter your password&nbsp; <input type="text" name="password"
   size="20"/></p>
  <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
  &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
  &nbsp;&nbsp;
  <input type="submit" value="Submit" name="B1"/></p>
</form>

</body>

</html>

最后web.xml是..

<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  <servlet>
    <servlet-name>Zulfiqar</servlet-name>
    <servlet-class>Demo.SendRedirectServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>Zulfiqar</servlet-name>
    <url-pattern>/SendRedirectServlet</url-pattern>
  </servlet-mapping>
  <servlet>
    <servlet-name>Hello</servlet-name>
    <servlet-class>Demo.ValidUserServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>Hello</servlet-name>
    <url-pattern>/ValidUserServlet</url-pattern>
  </servlet-mapping>
</web-app>

我得到第一页(http:// localhost:9050 / SendRedirect / Login.html)输入名称和密码但是点击提交按钮后我收到以下错误..

INFO: Marking servlet Zulfiqar as unavailable
15 Jul, 2012 8:14:56 PM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Allocate exception for servlet Zulfiqar
java.lang.ClassNotFoundException: Demo.SendRedirectServlet
    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.StandardWrapper.loadServlet(StandardWrapper.java:1136)
    at org.apache.catalina.core.StandardWrapper.allocate(StandardWrapper.java:857)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:136)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:169)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:168)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:927)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:999)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:565)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:309)
    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 8:20:26 PM org.apache.catalina.core.StandardWrapperValve invoke
INFO: Servlet Zulfiqar is currently unavailable

完成建议的更改,但仍然收到此错误...

INFO: Marking servlet Zulfiqar as unavailable
15 Jul, 2012 9:11:48 PM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Allocate exception for servlet Zulfiqar
java.lang.ClassNotFoundException: Demo.SendRedirectServlet
    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.StandardWrapper.loadServlet(StandardWrapper.java:1136)
    at org.apache.catalina.core.StandardWrapper.allocate(StandardWrapper.java:857)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:136)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:169)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:168)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:927)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:999)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:565)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:309)
    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 10:06:42 PM org.apache.catalina.core.StandardWrapperValve invoke
INFO: Servlet Zulfiqar is currently unavailable

2 个答案:

答案 0 :(得分:0)

response.sendRedirect("Demo/ValidUserServlet");

应改为

response.sendRedirect("/ValidUserServlet");

只有这个url模式会调用servlet,如web.xml文件部分所述:

<servlet>     
    <servlet-name>Hello</servlet-name>     
    <servlet-class>Demo.ValidUserServlet</servlet-class>   
</servlet>   
<servlet-mapping>     
    <servlet-name>Hello</servlet-name>     
    <url-pattern>/ValidUserServlet</url-pattern>   
</servlet-mapping>

但是按下“提交”按钮后,您的错误似乎来自其他servlet吗? 检查servlet类是否在Hardik提到的WEB-INF/classes中的Demo文件夹中退出。

如果从Eclipse运行项目,请执行清理和重建项目。固定类在大多数时间都没有找到错误。

答案 1 :(得分:0)

首先,不鼓励在资本/标题案例中使用包名称。始终使用小写字母来定义包名称。

第二,改变

response.sendRedirect("Demo/ValidUserServlet");

response.sendRedirect("/ValidUserServlet");

应该按照web.xml中提到的那样工作。

Tomcat存在一些热部署问题,因为它有时无法加载实际存在于类路径中的类。

因此,检查您的tomcat是否已加载新的servlet类。 再次编译和发布Web项目。

java.lang.ClassNotFoundException: Demo.SendRedirectServlet

检查您的目录结构。

SendRedirectServlet

Demo目录下是否存在/WEB-INF/class/Demo/SendRedirectServlet