填写表单并提交后,我收到404错误。我希望提交数据的表格在点击提交按钮后出现。我也发现我的提交没有记录在数据库中,我不知道为什么?提交我的jsp页面后出现404错误
ERROR:HTTP状态404 -/Lab5 /使用Tomcat 8.0.24
DBentry.java
package db;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
//import com.mysql.jdbc.Connection;
//import com.mysql.jdbc.PreparedStatement;
public class DBentry {
static DBentry instance = new DBentry();
Connection dbconn;
ResultSet results = null;
java.sql.PreparedStatement sql;
String dpwd = null;
StringBuilder sb = new StringBuilder();
java.sql.Statement stmt = null;
//change URL to your database server as needed
String dbPath="jdbc:mysql://localhost:8889/cs485_lab5";
public static DBentry getInstance() {
if (instance==null) {
instance = new DBentry();
}
return instance;
}
//Establish connection to MySQL server
public Connection newConnection() {
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
try {
dbconn = DriverManager.getConnection(dbPath,"root","root");
System.out.println("gain the connection");
return dbconn;
}
catch (Exception s){
System.out.println(s.getStackTrace().toString());}
}
catch (Exception err){
System.out.println(err.getStackTrace().toString());
}
return null;
}
public ResultSet selectStatement(String query) {
try {
dbconn=instance.newConnection();
sql= dbconn.prepareStatement(query);
ResultSet results;
results=sql.executeQuery();
System.out.println("query="+query);
//WARNING!
//Need to process ResultSet before closing connection
dbconn.close();
return results;
}
catch (Exception err) {
System.out.println(err.getMessage());
return null;
}
}
public boolean DBentry(String query) {
try {
System.out.println("query="+query);
instance.newConnection();
sql= dbconn.prepareStatement(query);
sql.executeUpdate(query);
dbconn.close();
return true;
}
catch (Exception err) {
err.getStackTrace();
return false;
}
}
public boolean entry(String itemNum,String price,String fName,String lName,String shipAdd,String cardType, String ccn) {
try {
instance.DBentry("INSERT INTO cs485_lab5.Orders (`Item Number`, `Price`, 'FirstName', 'LastName', 'ShippingAddress', 'CreditCard', 'CCN') " +
"VALUES ('"+itemNum+"','"+price+"','"+fName+"','"+lName+"','"+shipAdd+"','"+cardType+"', '"+ccn+"');");
return true;
}
catch (Exception err) {
err.getStackTrace();
return false;
}
}
public static void main(String[] args) {
//instance.entry("123wdr", "1234.00", "Bill", "Bob", "1234 jon doe street", "Visa", "12345677");
}
}
ShowParameters.java
package db;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.*;
public class ShowParameters extends HttpServlet {
private static final long serialVersionUID = 1L;
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String docType =
"<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 " +
"Transitional//EN\">\n";
String title = "Reading All Request Parameters";
out.println(docType +
"<HTML>\n" +
"<HEAD><TITLE>" + title + "</TITLE></HEAD>\n" +
"<BODY BGCOLOR=\"#FDF5E6\">\n" +
"<H1 ALIGN=CENTER>" + title + "</H1>\n" +
"<TABLE BORDER=1 ALIGN=CENTER>\n" +
"<TR BGCOLOR=\"#FFAD00\">\n" +
"<TH>Parameter Name<TH>Parameter Value(s)");
Enumeration paramNames = request.getParameterNames();
while(paramNames.hasMoreElements()) {
String paramName = (String)paramNames.nextElement();
out.print("<TR><TD>" + paramName + "\n<TD>");
String[] paramValues =
request.getParameterValues(paramName);
if (paramValues.length == 1) {
String paramValue = paramValues[0];
if (paramValue.length() == 0)
out.println("<I>No Value</I>");
else
out.println(paramValue);
} else {
out.println("<UL>");
for(int i=0; i<paramValues.length; i++) {
out.println("<LI>" + paramValues[i]);
}
out.println("</UL>");
}
}
out.println("</TABLE>\n</BODY></HTML>");
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
System.out.println("STOP1\n");
doGet(request, response);
}
}
ShowParameters
林索引.JSP
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Lab 5</title>
</head>
<body>
<%@ page import="db.*" %>
<body BGCOLOR="#FDF5E6">
<h1 align="center"> Customer Order Form</h1>
<form action="ShowParameters" method="post">
Item Number: <input type="text" name="itemNum"> <br>
Price Each: <input value="$" type="text" name="price"> <br>
<hr>
First Name: <input type = "text" name = "fname"> <br>
Last Name: <input type = "text" name = "lname"> <br>
Shipping Address: <textarea name = "shipadd" style ="resize:none "></textarea> <br>
Credit Card: <br>
<input name="creditcard" value ="visa" type="radio">Visa <br>
<input name="creditcard" value ="mastercard" type="radio">MasterCard <br>
Credit Card Number: <input name = "ccn" type="text">
<center><input type="submit" value="Submit Order"></center>
</form>
<%
String itemNum=request.getParameter("itemNum");
String price=request.getParameter("price");
String fName=request.getParameter("fname");
String lName=request.getParameter("lname");
String shipAdd=request.getParameter("shipadd");
String cardType=request.getParameter("creditcard");
String ccn=request.getParameter("ccn");
if (itemNum!= null&&!itemNum.trim().equals("")){
DBentry DBentry = new DBentry();
boolean flag=DBentry.entry(itemNum, price, fName, lName, shipAdd, cardType, ccn);
if(flag){%><script type="text/javascript">alert("Entry Success");</script><%
}
else { %><script type="text/javascript">alert("Entry Failure");</script><%}
}
%>
</body>
</html>
的web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:web="http://java.sun.com/xml/ns/javaee" xmlns="http://java.sun.com/xml/ns/javaee" 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>Lab5</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>
</web-app>
您是否在web.xml中定义了servlet? – developerwjk
我刚刚添加了web.xml。对于web.xml页面的用途我没有太多的理解。我还发现我的数据不再提交给数据库。 – user3362580