我正在编写显示主页的简单 servlet 程序。那里我给了账号,必须点击提交。这必须调用我的 servlet 并显示平衡。我已经在eclipse中写了这个。项目的文件夹结构如下我在webcontent文件夹中有account.html,在web-inf中有web.xml。
当我点击提交时,我得到 404 说/WelcomeServlet not found。请帮我..
**account.html**
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Bank Account</title>
</head>
<body bgcolor="Gold">
<center><h1>Account Enquiry</h1>
<form action="/WelcomeServlet">
Account Number : <input type = "text" Name = "t1"><br><br>
<input type="submit" value="GetBalance">
</form></center>
</body>
</html>
这是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_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>app1</display-name>
<welcome-file-list>
<welcome-file>account.html</welcome-file>
</welcome-file-list>
<servlet>
<display-name>WelcomeServlet</display-name>
<servlet-name>WelcomeServlet</servlet-name>
<servlet-class>com.stc.WelcomeServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>WelcomeServlet</servlet-name>
<url-pattern>/WelcomeServlet</url-pattern>
</servlet-mapping>
</web-app>
--------------
这是 WelcomeServlet:
package com.stc;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class WelcomeServlet
*/
public class WelcomeServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
Connection conn ;
/**
* @see HttpServlet#HttpServlet()
*/
public WelcomeServlet() {
super();
// TODO Auto-generated constructor stub
}
public void init(ServletConfig sc){
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost/test");
}
catch (Exception e) {
// TODO: handle exception
}
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<HTML>");
out.println("<body bgcolor = grey>");
try {
Statement st = conn.createStatement();
ResultSet rs = st.executeQuery("select balance from Account where accno = " +request.getParameter("t1"));
if(rs.next()) {
out.println("<h1>Balance is SAR:" +rs.getFloat(3)+"</h1>");
}else {
out.println("<h1>Account details does not exist</h1>");
}
rs.close();
st.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
out.println("</body></HTML>");
out.close();
}
}