jeudi 25 mai 2017

how to limit the number of checkbox in a html which is developed inside a servlet?

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='http://ift.tt/2eBd7UN'></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....");
        }
    }

}

Im creating online exam app. in creating exam all the question that are present in the database are dispalyed 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 furter checkbox should not be checked.




Aucun commentaire:

Enregistrer un commentaire