I want to insert something into my database based on a checkbox parameter that i get from a jsp file. I tried this and it does not work for some reason. I also printed the output of the checkbox value when it was checked and when it wasnt. When it wasnt i got "null" and when it was checked i got "on". I tried both "kurs == on" and "kurs != null" but none worked. What am i doing wrong?
my servlet:
package bacit.web.bacit_web;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.servlet.*;
import java.sql.Date;
@WebServlet(name = "RegisterUserServlet", value = "/RegisterUserServlet")
public class RegisterUserServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
int userID = 4;
String username = request.getParameter("username");
String password = request.getParameter("password");
String fname = request.getParameter("fname");
String lname = request.getParameter("lname");
String phone = request.getParameter("phone");
String kurs = request.getParameter("kurs");
Connection db = null;
try {
db = DBUtils.getINSTANCE().getConnection(out);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
if (kurs != null) {
String query1 = "insert into CERTIFICATE (USER_ID, COURSE_ID) values (?, 1)";
try {
PreparedStatement statement = db.prepareStatement(query1);
statement.setInt(1, userID);
}
catch (SQLException e) {
e.printStackTrace();
}
String query = "insert into USER (Fname, Lname, Phone, Username, Password) values (?, ?, ?, ?, ?)";
try {
PreparedStatement statement = db.prepareStatement(query);
statement.setString(1, fname);
statement.setString(2, lname);
statement.setString(3, phone);
statement.setString(4, username);
statement.setString(5, password);
ResultSet rs = statement.executeQuery();
} catch (SQLException e) {
e.printStackTrace();
}
} else {
String query = "insert into USER (Fname, Lname, Phone, Username, Password) values (?, ?, ?, ?, ?)";
try {
PreparedStatement statement = db.prepareStatement(query);
statement.setString(1, fname);
statement.setString(2, lname);
statement.setString(3, phone);
statement.setString(4, username);
statement.setString(5, password);
ResultSet rs = statement.executeQuery();
} catch (SQLException e) {
e.printStackTrace();
}
}
out.println(kurs);
}
}
and my jsp:
<div>
<label for="kurs"> Personløftersertifikat </label>
<input type="checkbox" name="kurs" id="kurs">
</div>
Aucun commentaire:
Enregistrer un commentaire