Issue
I'm creating online exam app. In creating exam all the question that are present in the database are displayed in a webpage
through servlet
.
In this program how to limit my checkbox
selection. When checkbox
selected
exceeds over 50 I should get an alert
and further checkbox
should not be checked.
Below is my code:
package com.lara;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class CreateExam extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
PrintWriter out = response.getWriter();
response.setContentType("text/html");
String examName = request.getParameter("examName");
String noOfQn = request.getParameter("noOfQn");
String timeForExam = request.getParameter("timeForExam");
String qnSelectionType = request.getParameter("qnSelectionType");
int timeLimit = Integer.parseInt(timeForExam);
int noOf_qn = Integer.parseInt(noOfQn);
String sql = "insert into create_exam values(?, ?, ?, ?, ?)";
int id1=0;
try(Connection con = Util.getConnection();
PreparedStatement pstmt = con.prepareStatement(sql);
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("select * from create_exam"))
{
while(rs.next())
{
id1 = rs.getInt("id");
}
pstmt.setInt(1, ++id1);
pstmt.setString(2, examName);
pstmt.setInt(3, noOf_qn);
pstmt.setInt(4, timeLimit);
pstmt.setString(5, qnSelectionType);
pstmt.executeUpdate();
}
catch(SQLException ex)
{
ex.printStackTrace();
}
if(qnSelectionType.equals("manualSelection"))
{
String sql1 = "select * from question";
out.println("<html>");
out.println("<head>");
// out.println("<script src='https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js'></script>");
//
// out.println("<script>");
// out.println("$.get(CreateExam, function () { $('input[name='name']').change(function () { var maxAllowed = 3;");
// out.println("var cnt = $('input[name='name']:checked').length; if (cnt > maxAllowed){$(this).prop('checked', '');");
// out.println("alert('Select maximum ' + maxAllowed + ' Levels!');}});});");
// out.println("</script>");
out.println("</head>");
try(Connection con = Util.getConnection();
PreparedStatement stmt = con.prepareStatement(sql1))
{
out.println("<body>");
out.println("<form action='CreateExamPg2'>");
ResultSet rs = stmt.executeQuery();
out.println("<center>");
out.println("<table border='1' height='50%' width='50%''>");
out.println("<tr>");
out.println("<th>"+examName+"</th>");
out.println("</tr>");
out.println("<tr>");
out.println("<th>sno</th>");
out.println("<th> </th>");
out.println("<th>category</th>");
out.println("<th>question</th>");
out.println("</tr>");
while(rs.next())
{
int id = rs.getInt("id");
String checkbox="<input type='checkbox' name='qnId' value='" + id +"'/>";
out.println("<input type='hidden' name='examName' value='" + examName +"'/>");
String category= rs.getString("category");
String question = rs.getString("question");
out.println("<tr>");
out.println("<td align='center'>"+id+"</td>");
out.println("<td align='center'>"+checkbox+"</td>");
out.println("<td align='center'>"+category+"</td>");
out.println("<td align='center'>"+question+"</td>");
out.println("</tr>");
}
out.println("</table>");
out.println("</center>");
out.println("<input type='submit' value='submit'>");
out.println("</body>");
out.println("</form>");
out.println("</html>");
}
catch (Exception e)
{
e.printStackTrace();
}
}
else
{
out.print("random method is not working in sql....");
}
}
}
Solution
Not relevant to the question, don't use the servlets for "view" use JSP instead.
Coming back to your question you can use Javascript inside JSP to count how many number of check boxes are selected if the number is more than required number you can alert the user.
Answered By - raviraja