在eclipse中找不到java servlet错误404

时间:2014-07-01 09:42:33

标签: java eclipse servlets

我在Ubuntu 14.04上使用Eclipse 4.3 kepler并使用Apache tomcat 7,我已经在我的项目中成功创建了.jsp和.html文件,但我无法运行servlet,我在库中添加了所有jar文件。我已阅读有关此错误404的每篇文章,我在启动以下servlet时会收到该文章

package 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;




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


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

/**
 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
 */
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // TODO Auto-generated method stub
    String s=request.getParameter("t1");
    int i=Integer.parseInt(s);
    String s1=request.getParameter("t2");
    int a=Integer.parseInt(s1);
    int t=0;
    if(i>=500000)
    {
        t=i*(30/100);
    }else if(i>=300000)
    {
        t=i*(20/100);
    }else if(i>=200000)
    {
        t=i*(10/100);
    }
    if(a>=60)
    {
        t=t-(i*(10/100));
    }
PrintWriter out=response.getWriter();
out.println("Thank you for Visiting");
out.println("Your Tax Laibality is"+t+"\n");
out.close();
}

/**
 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
 */
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // TODO Auto-generated method stub
}

}

我的web.xml文件是

enter image description here

我得到的就是这个 enter image description here

2 个答案:

答案 0 :(得分:0)

在web.xml中给/newtax/..../taxProcessor。给予/newtax/..../taxProcessor.java并通过给出网址http://localhost:<port-number>/newtax.taxProcessor从浏览器中检查。这里给你的tomcat服务器端口提供端口号。默认为8080

答案 1 :(得分:0)

而不是web.servlets.newtax.taxProcessor.java在web.xml中使用web.servlets.newtax.taxProcessor

BTW你的包名是servlet那你怎么在servlet-class标签中给web.servlets.newtax

示例如果你的eclipse中你的servlet类在src/somePackageName/ServletClassName,那么你的web.xml必须包含这样的条目

    <servlet>
        <servlet-name>anyName</servlet-name>
        <servlet-class>somePackageName.ServletClassName</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>anyName</servlet-name>
        <url-pattern>/taxProcessor</url-pattern>
    </servlet-mapping>

在您指定web.xml中的条目时,还可以删除注释@WebServlet("/taxProcessor")

最后以http://localhost:8080/YourProjectName/taxProcessor

运行servlet