2015-02-10 69 views
-1

請幫助,如果任何人都可以,我同時使用日食相關的代碼,我與你分享在@WebServlet(「你好」)得到錯誤任何人都可以有任何想法嗎?

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> 

你好Tomcat的Apache服務器上運行的HTML文件得到一個錯誤的.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> 
 
    \t <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> 
 
\t <script src="rememberme.js"></script> 
 
\t <script src="captcha.js"></script> 
 
</head> 
 
<body><div> 
 
     <form id="formRegistration" method="post" action="hello"> 
 
\t 
 
       <div> 
 
       <label for="email">Email Id*</label> 
 
       <input id="email" type="email" name="email" value="" placeholder="Enter your Email Id" /> 
 
       </div> 
 
\t \t \t \t 
 
\t \t \t \t 
 
\t \t \t \t <div> 
 
       <label for="Password">Password*</label> 
 
       <input id="password" type="password" name="password" value="" placeholder=" Enter Password" /> 
 
       </div> 
 
\t \t \t \t 
 
\t \t \t \t <div> 
 
       <input type="submit" name="commit" value="Login" id="publiclogin"> 
 
\t \t   </div> 
 
\t \t \t \t 
 
\t \t \t \t <div> 
 
       Forgot your password? <a href="google.com">Click here to reset it</a>. 
 
       </div> 
 
\t \t \t \t 
 
\t \t \t \t 
 
       <label class="checkbox"> 
 
       <input type="checkbox" value="remember-me" id="remember_me"> Remember me 
 
       </label> 
 
\t \t \t \t 
 
\t \t \t \t 
 
\t \t \t \t 
 
\t \t \t \t <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>

任何幫助,高度讚賞。

回答

1

您的servlet-name在servlet-mapping中不同。

請參閱下文提到的片段,

<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/

+0

代碼正在工作,但沒有解決錯誤:( – CandleCoder 2015-02-10 08:44:21

+0

您提到了servlet-class是否包含完整的軟件包詳細信息?此外,url-pattern是/ hello而不是/ Hello – 2015-02-10 10:26:51

0

當定義在web.xml的servlet,必須創建兩件事情:

  1. 寄存器的servlet
  2. 這些servlets的寄存器映射

在你的servlet的例子中,你沒有定義請求映射。而對於/ hello路徑,沒有名爲'Servlet'的servlet。

<servlet> 
     <servlet-name>Hi</servlet-name> // name of your servlet in this context 
     <servlet-class>com.example.web.servlets.Hello</servlet-class> // your servlet class(should include package) 
    </servlet> 
    <servlet-mapping> 
     <servlet-name>Hi</servlet-name> // reference to your servlet name 
     <url-pattern>/hello</url-pattern> // mapped path 
    </servlet-mapping> 

我們可以着手行動,告訴我們,它這個解決您的問題。

+0

您的代碼本身有錯誤...正如顯示eclipse – CandleCoder 2015-02-10 07:46:33

+0

你能分享這個錯誤嗎?只要類存在於給定路徑下,代碼是正確的根據JEE標準。 – Beri 2015-02-10 07:49:46

+0

cvc-complex-type.2.4.a:發現無效內容以元素'servlet-name'開頭。 '{「http://java.sun.com/xml/ns/j2ee":description,」http:// java.sun.com/xml/ns/j2ee":display-name,「http:/ /java.sun.com/xml/ns/j2ee":icon,「http://java.sun.com/xml/ns/j2ee":distributable」,http://java.sun.com/xml/ns/ j2ee「:context-param,」http://java.sun.com/xml/ns/j2ee":filter,「http://java.sun.com/xml/ns/j2ee「:filter-mapping,」http://java.sun.com/xml/ns/j2ee":listener, – CandleCoder 2015-02-10 08:37:49

相關問題