使用Apache tomcat v7在Eclipse中的servlet中出现HTTP 404错误

时间:2014-12-07 08:02:22

标签: java tomcat servlets

以下是我的jsp代码。

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@page import="connection.*" %> 
<%@page import="java.sql.*" %> 

    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="ISO-8859-1">
    <title>VIEW REPORTS</title>
    </head>
    <body>
    <div align="center"><b>View Reports</b></div>
    <form action="GetRole" method="get">
    <p>Enter user id:
    <input type="text" name="userid"><br>
    <input type="submit" value="submit"><br>
    </form>
    Choose the type of report:
    <script type="text/javascript" src="list.js"></script>
    <select name="first1" id="firstselect" onchange="first()">

        <option>Select</option>
        <option value="NEFT">NEFT REPORTS</option>
        <option value="LOAN">LOAN REPORTS</option>
        <option value="TRAN">TRANSACTION REPORTS</option>
    </select>

    <script type="text/javascript"></script> 


    </body>
    </html>

以下是我的servlet:

      package all_packages;
      import connection.*;
      import java.io.IOException;
      import java.sql.*;

      import javax.servlet.ServletException;
      //import javax.servlet.annotation.WebServlet;
      import javax.servlet.http.HttpServlet;
      import javax.servlet.http.HttpServletRequest;
      import javax.servlet.http.HttpServletResponse;
      public class GetRole extends HttpServlet {
        private static final long serialVersionUID = 1L;
      protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        try {
            Connection con=getConnection.getConnectionBuilder();
            String userid=request.getParameter("userid");
            System.out.println(" "+userid);
            PreparedStatement pstmt=con.prepareStatement("select role_id from users where role_id=?");
            pstmt.setString(1,"userid");
            ResultSet rs=pstmt.executeQuery();
            while(rs.next())
            {
                System.out.println(" "+rs.getString(1));
            }
        } catch (SQLException e) {
            System.out.println("Connection unsuccessful");
            e.printStackTrace();
        }
      }
      }

以下是我的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/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
    <display-name>Final_Bank</display-name>
    <welcome-file-list>
      <welcome-file>index.html</welcome-file>
      <welcome-file>index.htm</welcome-file>
      <welcome-file>index.jsp</welcome-file>
      <welcome-file>default.html</welcome-file>
      <welcome-file>default.htm</welcome-file>
      <welcome-file>default.jsp</welcome-file>
    </welcome-file-list>
    <servlet>
      <servlet-name>GetRole</servlet-name>
      <servlet-class>GetRole</servlet-class>
    </servlet>
    <servlet-mapping>
      <servlet-name>GetRole</servlet-name>
      <url-pattern>/GetRole</url-pattern>
    </servlet-mapping>
  </web-app>

请让我知道为什么它会抛出404错误。我在网上搜索了一段时间才得到适当的解决方案,但没有解决方案对我有用。我在Eclipse中使用过Apache Tomcat Server。

1 个答案:

答案 0 :(得分:0)

servlet位于包all_packages中,因此web.xml文件中的映射应为:

<servlet>
  <servlet-name>GetRole</servlet-name>
  <servlet-class>all_packages.GetRole</servlet-class>
</servlet>

现在转到404问题,如果jsp名称是sample.jsp且应用程序上下文名称是myapp,那么您需要访问的URL是:

http://localhost:8080/myapp/sample.jsp

假设您的tomcat在端口8080上运行。