tomcat - 在@WebServlet ("Hello"处出现错误)有人对此有任何想法吗?

标签 tomcat servlets annotations

<分区>

如果有人可以,请提供帮助,我在 Tomcat Apache 服务器上使用 eclipse 运行 HTML 文件时遇到错误,我正在与您分享相关代码

WEB.XML

  <?xml version="1.0" encoding="UTF-8"?>
  <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:web="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd http://xmlns.jcp.org/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.4">
 <servlet>
 <servlet-name>Hi</servlet-name>
 <servlet-class>Hello</servlet-class>
 </servlet>
 <servlet-mapping>
<servlet-name>Servlet</servlet-name>
<url-pattern>/hello</url-pattern>
</servlet-mapping>
 </web-app>

你好.java

  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;

   /**
 * Servlet implementation class Hello
  */
  @WebServlet("/Hello") -----------//Here getting an error

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


  /**
  * @see HttpServlet#HttpServlet()
*/
  public Hello() {
   super();
   // TODO Auto-generated constructor stub
   }

     /**
    * @see HttpServlet#doGet(HttpServletRequest request,HttpServletResponse response)
   */
      protected void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
      response.setContentType("text/html");
      PrintWriter pw=response.getWriter();
      String email=request.getParameter("email");
     String password=request.getParameter("password");
     pw.println("<br>your email is "+email);
     pw.println("<br>your password is "+password);

     }

     /**
    * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */

   }

<!DOCTYPE html>
           <!--<script src="http://jqueryvalidation.org/files/dist/additional-methods.min.js"></script>-->

          <html>
           <head>
        <meta charset="utf-8">
        <title>Login Form</title>
  	   <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
    <!--<script src="plugin/validation/dist/additional-methods.min.js">     </script>-->
         <script src="jquery.validate.min.js"></script>
           <script src="validationutils.js"></script>
	<script src="rememberme.js"></script>
	<script src="captcha.js"></script>
</head>
<body><div>
      <form id="formRegistration" method="post" action="hello">
	  
                 <div>
                <label for="email">Email Id*</label>
                <input id="email" type="email" name="email" value="" placeholder="Enter your Email Id" />
                </div>
				
				
				<div>
                <label for="Password">Password*</label>
                <input id="password" type="password" name="password" value="" placeholder=" Enter Password" />
                </div>
				
				<div>
                <input type="submit" name="commit" value="Login" id="publiclogin">
		        </div>
				
				<div>
                Forgot your password? <a href="google.com">Click here to reset it</a>.
                </div>
				
				
                <label class="checkbox">
                <input type="checkbox" value="remember-me" id="remember_me"> Remember me
                </label>
				
				
				
				<label class="" for="captcha">*Please enter the verication code shown below.</label>
                <div id="captcha-wrap">
                <img src="plugin/utilities/img/refresh.jpg" alt="refresh captcha" id="refresh-captcha" /> <img src="plugin/utilities/img/glyphicons-halflings.png" alt="" id="captcha" />
                </div>
                <input class="narrow text input" id="captcha" name="captcha" type="text" placeholder="Verification Code"/>
                
            </form>
             </div> 
           </body>
            </html>

非常感谢任何帮助。

最佳答案

您的 servlet 名称在 servlet 映射中不同。

引用下面提到的片段,

 <servlet>
   <servlet-name>Hi</servlet-name>
   <servlet-class>Hello</servlet-class>
 </servlet>
 <servlet-mapping>
   <servlet-name>Hi</servlet-name>
   <url-pattern>/hello</url-pattern>
 </servlet-mapping>

更多详情:http://javapapers.com/servlet/what-is-servlet-mapping/

关于tomcat - 在@WebServlet ("Hello"处出现错误)有人对此有任何想法吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28425905/

相关文章:

tomcat - 我正在将我的应用程序 Web 逻辑移动到 Tomcat7

java - 我如何在 Web 上下文中使用 Java 类?

Tomcat 线程池使用模式

mysql - 在 JEE6 环境下访问数据库的最佳方式是什么?

java - 在 tomcat Web 服务中生成后台线程

java - 在 gwt 中从 Formpanel 接收数据

java - 何时为映射 servlet 加载 web.xml 文件?

java - 如何使用类加载器和注释

java - 从 Annotation 对象获取原始对象

swift - 将数据从 Annotation 传递到下一个 VC