Java servlet无法正常工作

时间:2014-07-29 00:30:19

标签: java jsp servlets

如何使用 servlet 映射 JSP ?我在 JSP 页面中创建了一个登录表单,然后通过HttpPost将其发送到 servlet ,以便我可以对其进行验证。但是,我一直收到这个错误:

  

javax.servlet.ServletException:实例化servlet类时出错   practicea.OnServletLogin

javax.servlet.ServletException: Error instantiating servlet class practicea.OnServletLogin
org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:501)
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98)
org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:950)
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1040)
org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:607)
org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:313)
java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
java.lang.Thread.run(Thread.java:744)

根本原因

java.lang.ClassNotFoundException: practicea.OnServletLogin
org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1720)
org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1571)
org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:501)
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98)
org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:950)
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1040)
org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:607)
org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:313)
java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
java.lang.Thread.run(Thread.java:744)

不确定原因,但这是我的Home.jsp表格:

<body>
    <form action="login" method="post">
        name: <input type="text" name="userName"><br/>
        password<input type="password" name="userPassword">
        <input type="submit" value="login">
    </form>
</body>

这是我的servlet:

package practicea;

public class OnServletLogin extends HttpServlet{
    protected void doPost(HttpServletRequest req, HttpServletResponse res )
        throws ServletException,IOException
    {
        String user = req.getParameter("userName");
        String pass = req.getParameter("userPassword");

        PrintWriter pw = res.getWriter();
        res.setContentType("text/html");
        if(user.equals("h") && pass.equals("2")) {
            pw.println("Success");
        } else {
            pw.println("fail");
        }
        pw.close();
    }
}

这是我的web.xml,以便我可以执行映射

<display-name>practicea</display-name>

  <servlet>
    <servlet-name>loginServlet</servlet-name>
    <servlet-class>practicea.OnServletLogin</servlet-class>
 </servlet>

 <servlet-mapping>
    <servlet-name>loginServlet</servlet-name>
    <url-pattern>/login</url-pattern>
 </servlet-mapping>

这是我原来的JSP代码:

String action = request.getParameter("login"):
String username = request.getParameter("userName");
String password = request.getParameter("userPassword");

if(action.equals("login") {
    if(username.equals("h") && password.equals("2") {
         out.print("success");        
    } else {
         out.print("fail");
    }
}

<body>  .... </body>

它工作正常,我决定尝试servlet。并且......编译错误......

项目结构:

practicea
:
:....src
:     :..practicea
:            :... OnServletLogin.java
:
:....WebContent
      :..Home.jsp
      :..WEB-INF
         :..web.xml

0 个答案:

没有答案