Witam serdecznie.
Mam problem z przesyłaniem wartości do bazy danych z wykorzystaniem servletu. Po wypełnieniu formularza, wszystkie dane wysłane do bazy mają wartość null. Proszę o jakąś podpowiedź. Walczę z tym już kilka dni.
Oto kod:
register.jsp
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link rel="stylesheet" type="text/css" href="css/ksiazka.css">
<title>Rejestracja</title>
</head>
<body>
<div id="main">
<div id="header">
<div id="widgetBar">
<div id="middle">
<div id="footer">
<div id="registerCenterColumn">
<div><b>
Wypełnij poniższy formularz aby się zarejestrować
i uzyskać dostęp do wszystkich funkcji naszej wypożyczalni</b>
</div>
<div id="registerCenterColumn"><p></p>
<form name="register" action="s1_form" method="post">
<input type="hidden" name="register" value="register"/>
<legend><b>Rejestracja</b></legend>
<table>
<tr>
<td><label for="imie">*Imię:</label></td>
<td><input type="text" name="imie" id="imie" required="" /></td>
</tr>
<tr>
<td><label for="nazwisko">*Nazwisko:</label></td>
<td><input type="text" name="nazwisko" id="nazwisko" required="" /></td>
</tr>
<tr>
<td><label for="mail">*Adres E-mail:</label></td>
<td><input type="email" name="mail" id="mail" required="" /></td>
</tr>
<tr>
<td><label for="login">*Login:</label></td>
<td><input type="text" name="login" id="login" required="" /></td>
</tr>
<tr>
<td><label for="haslo">*Hasło:</label></td>
<td><input type="password" name="haslo" id="haslo" required=""/></td>
</tr>
<tr>
<td><label for="potwierdzhaslo">*Potwierdź hasło:</label></td>
<td><input type="password" name="potwierdzhaslo" id="potwierdzhaslo" required="" /></td>
</tr>
<tr>
<td><label for="adres">Adres:</label></td>
<td><input type="text" name="adres" id="adres" /></td>
</tr>
<tr>
<td><label for="miasto">Miasto:</label></td>
<td><input type="text" name="miasto" id="miasto" /></td>
</tr>
<tr>
<td><label for="wiek">*Wiek:</label></td>
<td><input type="text" name="wiek" id="wiek" /></td>
</tr>
<tr>
<td><input type="submit" value="Rejestruj" /></td>
<td><input type="reset" value="Wyczyść" /></td>
</tr>
<tr>
</tr>
</table>
<table>
<td><label> * (Pola oznaczone gwiazdką są wymagane) </label></td>
</table>
</form>
</div>
</div>
</div>
</body>
</html>
s1_form.java
package controller;
import java.io.IOException;
import java.sql.SQLException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet("/s1_form")
public class s1_form extends HttpServlet {
private static final long serialVersionUID = 1L;
StringBuilder csvSkills = new StringBuilder();
public s1_form() {
super();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
String hdnParam = request.getParameter("register");
if(hdnParam.equals("register")){
String imie = request.getParameter("txtimie");
String nazwisko = request.getParameter("txtnazwisko");
String mail = request.getParameter("txtmail");
String login = request.getParameter("txtlogin");
String haslo = request.getParameter("txthaslo");
String adres = request.getParameter("txtadres");
String miasto = request.getParameter("txtmiasto");
String wiek = request.getParameter("txtwiek");
GetsSets sets = new GetsSets();
sets.setimie(imie);
sets.setnazwisko(nazwisko);
sets.setmail(mail);
sets.setlogin(login);
sets.sethaslo(haslo);
sets.setadres(adres);
sets.setmiasto(miasto);
sets.setwiek(wiek);
try {
DbManager.Insert(sets);
} catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
}
response.sendRedirect("login.jsp");
}
if(hdnParam.equals("login")){
String userName = request.getParameter("txtlogin");
String password = request.getParameter("txthaslo");
if((userName.equals("admin")) && (password.equals("admin"))){
response.sendRedirect("WEB-INF/view/admin.jsp");
}else{
GetsSets set = new GetsSets();
set.setlogin(userName);
set.sethaslo(password);
try {
int checkUser = DbManager.checkUser(set);
System.out.println(checkUser);
if(checkUser == 1){
response.sendRedirect("WEB-INF/view/welcome.jsp");
}else{
response.sendRedirect("login.jsp");
}
} catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
}
}
}
}
}
ConnectionManager.java
package controller;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class ConnectionManager {
private static ConnectionManager instance=null;
static Object getInstance() {
throw new UnsupportedOperationException("Not supported yet.");
}
private final String USERNAME= "admin";
private final String PASSWORD= "";
private final String CONN_STRING= "jdbc:mysql://localhost:3306/baza_wypozyczalnia";
private Connection conn = null;
private ConnectionManager(){
}
public static ConnectionManager getinstance(){
if(instance==null){
instance= new ConnectionManager();
}
return instance;
}
private boolean openConnection(){
try{
conn=DriverManager.getConnection(CONN_STRING,USERNAME,PASSWORD);
} catch(SQLException e)
{
e.printStackTrace();
}
return true;
}
public Connection getConnection(){
if(conn==null){
if(openConnection()){
System.out.println("Connection Open");
return conn;
}else{
return null;
}
}
return conn;
}
public void commit(){
System.out.println("Connection Close");
try{
conn.close();
} catch (SQLException e)
{
e.printStackTrace();
}
conn=null;
}
}
DbManager.java
package controller;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class DbManager {
private static Connection con = ConnectionManager.getinstance().getConnection();
public static void Insert(GetsSets set)
throws ClassNotFoundException, SQLException
{
Class.forName("com.mysql.jdbc.Driver");
String sql= "INSERT INTO register(imie,nazwisko,mail,login,haslo,adres,miasto,wiek) VALUES(?,?,?,?,?,?,?,?)";
PreparedStatement pstmt = con.prepareStatement(sql);
pstmt.setString(1,set.getimie());
pstmt.setString(2, set.getnazwisko());
pstmt.setString(3, set.getmail());
pstmt.setString(4, set.getlogin());
pstmt.setString(5, set.gethaslo());
pstmt.setString(6, set.getadres());
pstmt.setString(7, set.getmiasto());
pstmt.setString(8, set.getwiek());
pstmt.executeUpdate();
ConnectionManager.getinstance();
}
public static int checkUser(GetsSets get) throws ClassNotFoundException, SQLException{
Class.forName("com.mysql.jdbc.Driver");
String sql = "SELECT COUNT(*) FROM register WHERE login=? AND haslo=?";
PreparedStatement pstmt = con.prepareStatement(sql);
pstmt.setString(4, get.getlogin());
pstmt.setString(5, get.gethaslo());
ResultSet rs = pstmt.executeQuery();
int count = 0;
while(rs.next()){
count = rs.getInt(1);
}
ConnectionManager.getinstance();
return count;
}
}
GetsSets.java
package controller;
public class GetsSets {
private String imie;
private String nazwisko;
private String mail;
private String login;
private String haslo;
private String adres;
private String miasto;
private String wiek;
private int id;
public String getimie() {
return imie;
}
public void setimie(String imie) {
this.imie = imie;
}
public String getnazwisko() {
return nazwisko;
}
public void setnazwisko(String nazwisko) {
this.nazwisko = nazwisko;
}
public String getmail() {
return mail;
}
public void setmail(String mail) {
this.mail = mail;
}
public String getlogin() {
return login;
}
public void setlogin(String login) {
this.login = login;
}
public String gethaslo() {
return haslo;
}
public void sethaslo(String haslo) {
this.haslo = haslo;
}
public String getadres() {
return adres;
}
public void setadres
(String adres) {
this.adres = adres;
}
public String getmiasto() {
return miasto;
}
public void setmiasto(String miasto) {
this.miasto = miasto;
}
public String getwiek() {
return wiek;
}
public void setwiek(String wiek) {
this.wiek = wiek;
}
public int getid() {
return id;
}
public void setId(int id) {
this.id = id;
}
}
Z góry dziękuję za każdą pomoc.
To mój pierwszy temat więc proszę o wyrozumiałość.
Pozdrawiam.