将用户从jsp传递给servlet

时间:2015-01-23 10:55:11

标签: jsp tomcat servlets

Servlet类:HelloWorld.java

package org.baba.prakash;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;

public class HelloWorld extends HttpServlet { 
  /**
     * 
     */
    private static final long serialVersionUID = 1L;

protected void doGet(HttpServletRequest request, 
      HttpServletResponse response) throws ServletException, IOException 
  {
      response.setContentType("text/html"); 
      PrintWriter out=response.getWriter(); 
      String username=request.getParameter("username"); 
      String password=request.getParameter("password"); 
      out.println(username); 
      out.println("<br>"); 
      out.println(password); 
  }  
}

JSP:order.jsp

&#13;
&#13;
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" 
pageEncoding="ISO-8859-1"%> 
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 
<html> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> 
<title>Insert title here</title> 
</head> 
<body> 
<form method="GET" action="HelloWorld"> 
User Name:<input type="text" name="username"><br> 
Pass Word:<input type="text" name="password"><br> 
<input align="middle" type="submit" value="Submit Form"> 
</form> 
</body> 
</html>
&#13;
&#13;
&#13;

的web.xml

&#13;
&#13;
<servlet> 
<description></description> 
<display-name>HelloWorld</display-name> 
<servlet-name>HelloWorld</servlet-name> 
<servlet-class>org.baba.prakash.HelloWorld</servlet-class> 
</servlet> 
<servlet-mapping> 
<servlet-name>HelloWorld</servlet-name> 
<url-pattern>/HelloWorld</url-pattern> 
</servlet-mapping> 
&#13;
&#13;
&#13;

项目结构

Capacitr_mgnt --------&gt; java Resoureces ---------&gt; src ------&gt; package&gt; Helloworld.java Capacitr_mgnt -----&gt; webcontent ---&gt; order.jsp文件

错误 类型异常报告

message实例化servlet类的错误org.baba.prakash.HelloWorld

说明服务器遇到内部错误,导致无法完成此请求。

例外

输入例外报告

message Error实例化servlet类org.baba.prakash.HelloWorld.HelloWorld

说明服务器遇到内部错误,导致无法完成此请求。

异常追踪:

javax.servlet.ServletException: Error instantiating servlet class org.baba.prakash.HelloWorld.HelloWorld
org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:936)
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1004)
org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310)
java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
java.lang.Thread.run(Unknown Source)

根本原因:

java.lang.ClassNotFoundException: org.baba.prakash.HelloWorld.HelloWorld
org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1714)
org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1559)
org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:936)
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1004)
org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310)
java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
java.lang.Thread.run(Unknown Source)

注意Apache Tomcat / 7.0.34日志中提供了根本原因的完整堆栈跟踪。

2 个答案:

答案 0 :(得分:0)

正如编译器明确指出的那样,

  

java.lang.ClassNotFoundException:org.baba.prakash.HelloWorld.HelloWorld

HelloWorld路径

中找不到

org.baba.prakash.HelloWorld

在您的项目结构中,您没有任何名为org.baba.prakash.HelloWorld的包。因此,请在web.xml下的src中创建一个名为的包。

答案 1 :(得分:0)

检查已部署的war-WEB / INF / classes文件夹中是否存在HelloWorld.class,或者如果您有一个包含HelloWorld.java文件的java项目,那么它是否转换为jar并存在于WEB- INF / lib文件夹。