以下是我的jsp代碼。使用Apache tomcat v7在Eclipse中的servlet中發生HTTP 404錯誤
<%@ 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服務器。
何時發生錯誤?哪個網址?提交表單後? – andih 2014-12-07 08:29:41
@andih,提交表單後回覆。 Aftrer提交,URL即將到來:'http:// localhost:8080/Final_Bank/GetRole?userid = wcw' – Mistu4u 2014-12-07 09:07:06
您可以更新您的文章中的'web.xml'來表示您當前的本地版本。 – andih 2014-12-07 09:14:24