在我填写表单并提交后,我收到404错误。我希望在点击提交按钮后显示提交数据的表格。我还发现我的提交没有在数据库中录制,我不知道为什么?
错误:HTTP状态404 - / Lab5 / ShowParameters
我正在使用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);
}
}
的index.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>
答案 0 :(得分:0)
您需要将servlet映射添加到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>
<servlet>
<servlet-name>showParameters</servlet-name>
<servlet-class>db.ShowParameters</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>showParameters</servlet-name>
<url-pattern>/ShowParameters</url-pattern>
</servlet-mapping>
</web-app>
第一个标记<servlet>
及其内部告诉Tomcat在哪个类中查找servlet并为servlet命名。下一个标记<servlet-mapping>
用该名称标识servlet并将其映射到url。
答案 1 :(得分:0)
使用
在web.xml中添加servlet映射
<servlet>
和
<servlet-mapping>
或者只是在servlet顶部添加@WebServlet
注释。