gpt4 book ai didi

java - Java 新手请help.form n servlet

转载 作者:太空宇宙 更新时间:2023-11-04 07:56:59 25 4
gpt4 key购买 nike

大家好,当我运行包含表单的 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>

Servlet:

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
}
}

最佳答案

付款方式:选择 name="payment_mode"

String s7 = request.getParameter("payment_mode");
System.out.println(s7);

payment_mode payment_mode 不同

在获取下拉列表中的值时也可以使用:

并将此 select name="payment_mode" 更改为 select id="payment_mode"

var s7 = document.getElementById("payment_mode"); 
var strUser = s7.options[s7.selectedIndex].text;

关于java - Java 新手请help.form n servlet,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13467274/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com