大家好,当我运行包含表单的 servlet 并单击提交时,我收到以下错误,我在代码中找不到错误,但显然有一个错误。否则,当我单击提交时,Eclipse 控制台中会出现以下内容:
ss
ss
zz@z.com
56464
asdcity
pen
null
22
Done1
Failed
这里有两个问题。首先,为支付模式检索空值。其次,数据库中的插入失败。
形式 :
<%@ 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>Form</title>
</head>
<body>
<form action="SaveServlet">
First Name: <input type="text" name="firstname"><br>
Last Name:<input type="text" name="lastname"><br>
Email ID:<input type="text" name="email_id"><br>
Phone number:<input type="text" name="phone_number"><br>
Address:<input type="text" name="address"><br>
Purchasing Item:<input type="text" name="purchasing_item"><br>
Payment Mode: <select name="paymentment_mode">
<option>Visa</option>
<option>Master</option>
<option>Mastro</option>
<option>Amex</option>
</select><br>
Card number:<input type="text" name="card_number"><br>
<input type="submit" value="Save"></input>
</form>
</body>
</html>
小服务程序:
package myPack;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class SaveServlet
*/
public class SaveServlet extends HttpServlet
{
private static final long serialVersionUID = 1L;
/**
* Default constructor.
*/
public SaveServlet() {
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String s1=request.getParameter("firstname");
System.out.println(s1);
String s2=request.getParameter("lastname");
System.out.println(s2);
String s3=request.getParameter("email_id");
System.out.println(s3);
String s4=request.getParameter("phone_number");
int s41=Integer.parseInt(s4);
System.out.println(s41);
String s5=request.getParameter("address");
System.out.println(s5);
String s6=request.getParameter("purchasing_item");
System.out.println(s6);
String s7=request.getParameter("payment_mode");
System.out.println(s7);
String s8=request.getParameter("card_number");
int s81=Integer.parseInt(s8);
System.out.println(s8);
String c="jdbc:mysql://localhost:3306/test";
System.out.println("Done1");
Connection con=null;
try{
Class.forName("com.mysql.jdbc.Driver").newInstance();
System.out.println("Done2");
con = DriverManager.getConnection(c, "root", "MyNewPass");
System.out.println("Done3");
PreparedStatement ps=null;
System.out.println("Done4");
String qs = "insert into table1 values(?,?,?,?,?,?,?,?);";
ps = con.prepareStatement(qs);
ps.setString(1,s1);
ps.setString(2,s2);
ps.setString(3,s3);
ps.setInt(4,s41);
ps.setString(5,s5);
ps.setString(6,s6);
ps.setString(7,s7);
ps.setInt(8,s81);
System.out.println("Success");
ps.execute();
con.close();
}
catch (Exception e) {
// TODO: handle exception
System.out.println("Failed");}}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
}
}