I want to check if a Integer value is empty.
The value for Integer is completed in a form. Here is my code.
Here the value is introduced:
<input name="varsta" placeHolder="Varsta:" type="text" data-constraints='@NotEmpty @Required @AlphaSpecial'> <br/><br/>
Now I want to check if there are data introduced or not.
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
Integer varsta = Integer.parseInt(request.getParameter("varsta"));
request.getSession().setAttribute("varsta", varsta);
try {
DBConnection connection = new DBConnection();
Connection con = connection.Connect();
PreparedStatement ps = con.prepareStatement(
"insert into user(Nume,Prenume,E_mail,Parola,Varsta,Sex,Greutate,Inaltime,Nivel_activitate,Calcul_calorii)" +
"values ('"+nume+"','"+prenume+"','"+email1+"','"+parola+"','"+varsta+"','"+sex+"','"+greutate+"','"+inaltime+"','"+activitate+"','"+calorii+"')"
);
if (varsta == null && "".equals(varsta)) {
String message = "Va rugam completati cu atentie TOATE campurile!";
request.setAttribute("message", message);
request.getRequestDispatcher("/inregistrare.jsp").forward(request, response);
} else {
int i = ps.executeUpdate();
if (i > 0) {
request.getRequestDispatcher("/preferinte.jsp").forward(request, response);
}
}
ps.close();
con.close();
} catch(Exception se) {
se.printStackTrace();
}
}
is not working.
Could anyone help me?
System.out.println(request.getParameter("varsta"))prints? Some of the errors: You defined you variable asvarstaand you are checkingVarstathis is just the first problem. Beingvarstaan Integer you can't check it against "" (empty) it is an Integer.varstareference instead ofVarstain youif-condition? If you did, thisif-statement will never returntrue, since you're comparing different types of objects:IntegeragainstStringrequest?