我正在编写简单的servlet程序,其中显示主页。 我已经给了帐户号,并且必须点击提交。 这必须调用我的servlet并显示平衡。 我在日食中写过这个。 项目的文件夹结构如下 我在webcontent文件夹中有account.html,web-inf内有web.xml。
当我点击提交时,我得到404说/找不到WelcomeServlet。 请帮帮我..
**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();
}
}
答案 0 :(得分:1)
以下servlet为我工作(Tomcat 7.0.40,java版本“1.6.0_45”)。请注意:
<url-pattern>
的web.xml中需要前导'/' - 我测试了它。而我的Head First Servlets书中说,需要领先的斜线(2004年,第586页)。新语法似乎也需要前导斜杠:@WebServlet("/WelcomeServlet")
。没有领先的斜杠,我的项目抛出了各种例外。
表单的action属性中不需要前导斜杠。浏览器具有从相对路径(即不以斜杠开头的路径)组装URL的规则,以产生最终的URL。因此,对于表单的action属性,只要相对路径解析为与正确的绝对路径相同的路径,就可以使用绝对路径(前导斜杠)或相对路径(无前导斜杠)。正确的绝对路径以项目名称开头。
我必须在getConnection()中指定用户名和密码,对我来说这是'root'和''。有一个版本的getConnection()没有用户名和密码,但我不知道它是如何工作的。
对于getFloat(),列号必须正好为1;无论是2还是0都不会起作用,这在阅读文档之后才有意义:
float getFloat(int columnIndex) throws SQLException以此形式检索此ResultSet对象的当前行中指定列的值 以Java编程语言浮动。参数:columnIndex - 第一列是1 ,第二列是2,...
http://docs.oracle.com/javase/6/docs/api/java/sql/ResultSet.html#getFloat(int)
您的选择仅从客户的记录中检索一件事:余额。因此,您的行只有一列。
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 {
//ADDED this line:
Class.forName("com.mysql.jdbc.Driver").newInstance();
conn = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/my_db",
"root",
""
);
}
catch (Exception e) {
System.out.println("Couldn't create connection.");
// 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();
String accno = request.getParameter("t1");
ResultSet rs = st.executeQuery("SELECT balance FROM accounts WHERE accno = " +request.getParameter("t1"));
if(rs.next()) {
out.println("<h1>Balance is SAR:" +rs.getFloat(1)+"</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();
}
}