How to avoid null values from insertion into the database? - html

This question is related to this question.
I have created many input boxes using a for loop. from this whatever user type value, is get into another JSP page. from the input box values are getting properly.
But at the time of insertion that values into the database inserted as a blank or null. only if user type first input boxes in sequential order(first input box,second input box, third input box) then only value is getting inserted properly as per the expected results.
Even i'm checking the conditions also that is it contain null values or not. but still It'll will get inserted null values.
The main expected result is if user type particular value into the textbox then the value get inserted properly not the blank values get inserted into database.
<!-----new.jsp---------------------------------------------->
<%
for(int i = 0; i<ar.size(); i++)
{
%><span class="left-check"><%=ar.get(i)%></span><%
%>
<!--name=abc will be used in jsp to get value selected in checkboxes-->
<input id ="<%=idcounter%>" type="checkbox" name = "abc" value="<%=ar.get(i)%>" />
<input class = "left-marg-input7 size" id ="<%=idcounter%>" type="text" name = "abc_val" /><br><br>
<%
idcounter++;
}
%>
</center><br><br>
<center><button type= "submit" name="action" >SIGN UP</button></center>
</form>
</body>
</html>
Then the Next jsp page is :
<!------insertdata.jsp---------------------------------------------->
<html>
<%
String check[]= request.getParameterValues("abc");
String checkval[] = request.getParameterValues("abc_val");
String check_str = "";
String checkval_str = "";
if(check != null && checkval != null)
{
// there might be more one checkbox selected so, using loop
for(int i=0; i<check.length;i++)
{
// printing values selected from
check_str = check[i].toString();
checkval_str = checkval[i].toString();
if(checkval_str != null)
{
%><script>alert(<%=checkval_str%>);</script><%
st.executeUpdate("insert into user_assign_leave(org_email,user_email,leave_name,assign_leave,balance_leave)values('"+org_email+"','"+email+"','"+check_str+"','"+checkval_str+"','"+checkval_str+"')");
}
st.executeUpdate("insert into user_assign_leave(org_email,user_email,leave_name,assign_leave,balance_leave)values('"+org_email+"','"+email+"','"+check_str+"','"+checkval_str+"','"+checkval_str+"')");
}
}
st.executeUpdate("insert into user(org_name,org_email,name,email,mobile,custom_ID,pass)values('"+org_name+"','"+org_email+"','"+username+"','"+email+"','"+contactno+"','"+customer_id+"','"+password+"')");
%>
</body>
</html>

Related

Differentiate multiple input form inside forEach statement

I have an array of String, the length is variable. I need to create for each String two buttons: buy and remove. They manage the quantity of the correspondent element. Like this:
Resoult.
I tried this, works but it's not clear.
String go = request.getParameter("go");
if ((go != null)){
String[] info = go.split(",");
int index = Integer.parseInt(info[1]);
if (info[0].equals("+")) {
++quantita[index];
} else {
--quantita[index];
}}
...
<c:forEach var="i" begin="0" end="${length-1}" >
<%
int i = (int) pageContext.getAttribute("i");
out.print(products[i] + " (" + quantita[i] +" in cart)");
%>
<input type=submit name="go" value="-,${i}"/>
<input type=submit name="go" value="+,${i}"/><br>
</c:forEach>
Use <button type="submit"> instead of <input type="submit">. This HTML element allows you to set content via children rather than via value attribute. This way you can simply use the name attribute to indicate the action and the value attribute to indicate the identifier.
<button type=submit name="decrease" value="${i}">-</button>
<button type=submit name="increase" value="${i}">+</button>
String decrease = request.getParameter("decrease");
String increase = request.getParameter("increase");
if (decrease != null) {
--quantity[Integer.parseInt(decrease)];
}
else if (increase != null) {
++quantity[Integer.parseInt(increase)];
}
Is that clearer?

if statement not executed in JSP scriptlet

i encountered the following problem. I have made a table in mysql named 'users' and i want to add users using JSP. The primary key is the Email and i want to check before adding another user if the email already exists. If the email already exists the users will not be added to the database and a message "email already exists" will appear.
I created ResultSet rs object and extracted the "Email" column into a String email using a while loop.Now, here comes the problem. Suppose i have a user with the "denis#yahoo.com" email. If i try to add an user with the exact same email an error will occur which means my if statement was ignored. I looked up in the debug mode for the variables email and Email and they had the exact same "denis#yahoo.com" and still the if statement was ignored.
<!-- file name: addUser.jsp -->
<%
String Email=request.getParameter("Email");
String Name = request.getParameter("Name");
String Adress = request.getParameter("Adress");
String Phone = request.getParameter("Phone");
if (Email != null)
{
jb.connect(); // connect to the database using JavaBean class
ResultSet rs;
rs=jb.seeTable("users");// rs gets first row of table
String email;
int a=1;
while(rs.next())
{
email=rs.getString("Email");
if(Email==email)
{
a=0;
jb.disconnect();
%>
<p> The email already exists</p>
<%
break;
}
}
if(a==1){
jb.addUser(Email,Name, Adress, Phone);
jb.disconnect();
%>
<p>Data has been added.</p>
<%
}
} else {
%>
<form action="addUser.jsp" >
Email: <input type="text" name="Email"><br>
Name: <input type="text" name="Name"><br>
Adress: <input type="text" name="Adress"><br>
Phone: <input type="text" name="Phone"><br>
<button type="submit" >Add User</button><br>
</form>
<%
}
%>
I use NetBeans IDE 8.2, and something that i think is odd appeared in the debug mode. One of the email i try to test appeared with bold characters meanwhile the other was not bold.
Why is my if statement ignored if i add an user with the exact same email of another? Please help me out!

Submit a 'null' to a jsp from an html form

Forwarding an html form to a jsp page which has an
<input type="text" name="name">
for 'name' parameter to be processed in jsp page. When no name is supplied , I do want the form to be submitted to the jsp and jsp should detect 'null' to process an if-else loop to display a relevant message.
How do I achieve that?
I do want the form to be submitted to the jsp and jsp should detect
'null' to process an if-else loop to display a relevant message.
You can use this on your display.jsp page to check if value is not null and not empty:
<%
String name = request.getParameter("name");
if(name != null && !name.isEmpty()){
%>
<p>name is <%=name%></p>
<%
}else{
%>
<p>name is null or empty!</p>
<%
}
%>
But as mentioned already this is highly discouraged! You should be using a servlet. If you want to see how that can be done, let me know and i'll edit this answer with a better solution.

Getting value of radio button from dynamic form

I have a form within a JSP that contains a multiple choice quiz, each question is followed by a group of radio buttons with the options, these are pulled from the database as questions don't all have the same number of options and the radio buttons are added as the form is created, each radio button has a label which contains the text answer. When submitting the page it is passed to another JSP, here I want to get the text from the label to store so I can check the answers.
here's the form, so each group of radio buttons is given the name based on question number, and each member of the group is given a id based on its question number and option within the question.
<form class="container" id="quiz" method="post" action="/quizChecker.jsp">
<% for (int i = 0; i < ques_nos.size(); i++) {
String query = "SELECT answer_text, is_correct FROM Quiz_Question_Option WHERE quiz_ques_id = ?";
PreparedStatement prepstmt = con.prepareStatement(query);
prepstmt.setInt(1, ques_nos.get(i));
ResultSet rsq = prepstmt.executeQuery();
rsq.last();
int size = rsq.getRow();
rsq.first();
%>
<p id="ques" class="Question"<%=i%>"><%=questions.get(i)%></p><br>
<% for (int j = 0; j < size; j++) {
%>
<input class="checkmark" type="radio" name="q<%=i%>" id="q<%=i%><%=j%>">
<label for="q<%=i%><%=j%>"><%=rsq.getString(1)%></label><br>
<% rsq.next();
}
}
session.setAttribute("ques", ques_nos);
%>
<button type="submit" value="checkQuiz">Submit Quiz</button>
</form>
Here's the code where I was trying to get the text value of the selected button.
<%
ArrayList<String> answers = new ArrayList<~>(5);
for (int i = 0; i < 5; i++) {
String given = request.getParameter("q"+i);
answers.add(given);
}
...
When I added a loop to print out what had been added to answers it returned "ononononon" so I guess I am accessing if the button is selected, as I would only be looking at selected radio buttons here, how do I get the value of the label instead?
Try setting a value for all the radio buttons
<input class="checkmark" value="answer1" type="radio" name="q<%=i%>" id="q<%=i%><%=j%>">
<input class="checkmark" value="answer2" type="radio" name="q<%=i%>" id="q<%=i%><%=j%>">

Display data from Custom Query(Joined tables) in liferay search container

I have followed this wiki and have successfully built a custom query.
It works fine. I have used a join between tables.
My question is how do I display it on a jsp using liferay search container since className in search container requires one model class.
EDIT:
What I have tried till now is this:
<%
getAttendanceData attName = new getAttendanceData();
List<Object[]> displayAttListName = AttendanceLocalServiceUtil.findAttendance();
ArrayList name = new ArrayList();
ArrayList title = new ArrayList();
ArrayList status = new ArrayList();
ArrayList remarks = new ArrayList();
for(Object[] att:displayAttListName) {
name.add(att[0]);
title.add(att[1]);
status.add(att[2]);
remarks.add(att[3]);
}
%>
<liferay-ui:search-container delta="20" emptyResultsMessage="No Results Found">
<liferay-ui:search-container-results
total="<%= displayAttListName.size() %>"
results="<%= ListUtil.subList(displayAttListName , searchContainer.getStart(), searchContainer.getEnd()) %>"
/>
<liferay-ui:search-container-row modelVar="search"
className="java.lang.Object">
<%
for(Object displayName:name) {
%>
<liferay-ui:search-container-column-text name='studName' value = '<%=String.valueOf(displayName)%>' href="">
</liferay-ui:search-container-column-text>
<%
}
%>
</liferay-ui:search-container-row>
<liferay-ui:search-iterator/>
</liferay-ui:search-container>
What I have done above displays each of the 10 names 10 times in a column.
I want the names to appear on each new row. How shoould I modify the above code?
EDIT 2
Considering the name array defines earlier I did the following:
<liferay-ui:search-container-column-text name="employee name" href = "">
<%=name.getClass().getDeclaredFields().toString() %>
</liferay-ui:search-container-column-text>
With the above, I am getting the result something like: [Ljava.lang.reflect.Field;#195f1af on each row.
I see that the name, title, status and remarks field are all String (as per your comment) so in the for loop you should cast the Object as a String and you don't need the four ArrayList for this.
Here is how the row tag would look like:
<liferay-ui:search-container-row className="java.lang.Object" modelVar="search">
<%--
Since an "Object[]" is nothing but an "Object", we first cast the "search"
instance to an "Object[]" and then to a "String"
--%>
<liferay-ui:search-container-column-text name='name' value='<%= (String) ((Object[])search)[0] %>' />
<liferay-ui:search-container-column-text name='title' value='<%= (String) ((Object[])search)[1] %>' />
<liferay-ui:search-container-column-text name='status' value='<%= (String) ((Object[])search)[2] %>' />
<liferay-ui:search-container-column-text name='remarks' value='<%= (String) ((Object[])search)[3] %>' />
</liferay-ui:search-container-row>
There you go, this should work.
A more cleaner way I think would be to have a POJO defined that would store these values and then the POJO's list can be returned. I have not tried the second approach though.
Another standard approach is to include extra fields in any one of the entity's *Impl and then returning the list of that entity, in your case I would assume you have Student and Attendance entities, so you can put the fields status & remarks in StudentImpl and then return a List<Student> or put fname in AttendanceImpl and return List<Attendance> from the finder method. (updated after this comment)