2015-06-06 34 views
2

这是当我尝试编译我的代码时看到的错误 - 我是否正在通过URL传递?我想从这个示例代码中学习,在浏览器中显示我的servlet。我想在浏览器中加载我的servlet,它给了我这个错误

HTTP Status 404 - /Lesson41/ 

type Status report 

message /Lesson41/ 

description The requested resource is not available. 

Apache Tomcat/8.0.22 

我的servlet代码如下:

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

    @WebServlet("/Lesson41") 
    public class Lesson41 extends HttpServlet { 
     private static final long serialVersionUID = 1L; 

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

     protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 
      String usersName = request.getParameter("YourName"); 

      String theLang = request.getParameter("Language"); 

      int firstNum = Integer.parseInt(request.getParameter("Firstnum")); 
      int secondNum = Integer.parseInt(request.getParameter("secondnum")); 
      int sumONum = firstNum + secondNum; 

      response.setContentType("text/html"); 

      PrintWriter output = response.getWriter(); 

      output.println("<html><body><h3>Hello " + usersName); 

      output.println("</h3><br />" + firstNum + " + " + secondNum); 
      output.println(" = " + sumONum + "<br />Speaks " + theLang + "</body></html>"); 
     } 

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

     protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 
      doGet(request, response); 
     } 

    } 

这是我的XML:

<?xml version="1.0" encoding="UTF-8"?> 
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1"> 
    <display-name>HelloServers</display-name> 
    <servlet> 
     <servlet-name>Lesson41</servlet-name> 
     <servlet-class>HelloServelets-Lesson41</servlet-class> 
     </servlet> 

    <servlet-mapping> 
      <servlet-name>Lesson41</servlet-name> 
      <url-pattern>http://localhost:8080/Lesson41/</url-pattern> 
     </servlet-mapping> 
</web-app> 
+0

“错误”,而不是“时代” –

+0

我不知道,但似乎这样的:通过'http://本地主机:8080/Lesson41 /'URL模式是错误的,心不是URL模式假设是什么像'/ Lesson41 /'? – Lrrr

+0

我已经尝试过,仍然无法工作。 – KIM

回答

0

你必须采取以下步骤:

1. Remove <Servlet> and <servlet-mapping> tags and anything in between them. Since you have used @WebServlet annotation. There is no need to declare those tags in web.XML. 
  1. 在你的表单标签替换操作属性: 行动=“Lesson41”

  2. 不要试图直接通过URL调用servlet,您与您的要求发布的一些数据。首先尝试加载你的HTML页面,然后点击提交按钮来调用Servlet的doPost()方法。

+0

什么是你的HTML/JSP文件有输入标签?什么是项目名称? – AsSiDe

+0

这工作感谢AsSide – KIM

0

似乎有多个问题。由于错误说

无法连接到目的地(本地主机)

最有可能或者您的Web服务器没有运行或者没有侦听端口8080。

其次,你似乎在URL中缺少应用程序上下文名称:

http://localhost:8080/Lesson41 

不知道什么是您的应用程序,但也许HelloServers然后URL应该是:

http://localhost:8080/HelloServers/Lesson41 
+0

我试过/ Lesson41,它给了我同样的时代。我通过网络浏览器加载它。我的服务器已开启。 – KIM

+0

@KIM发布服务器日志。 –

+0

这就是一切。 – KIM

相关问题