views:

262

answers:

3

Hi Everyone,

I need to make a quiz that uses JSP and servlets but I can't find any tutorials online which can help me create my own one.

If there are any can someone recommend me some, I have tried Google and nothing reasonable has come up.

Thanks.

+1  A: 

That's a pretty broad question. It sounds like that you're looking for tutorials which provides ready-to-use code so that you don't need to program the whole stuff yourself. Those kind of tutorials doesn't exist. The tutorials are there to learn the basic concepts.

Before you start programming with JSP/Servlets, you'll need to ensure that you have a good grasp on HTTP, HTML, CSS, JS and especially HTML forms. You should under each learn/know that you can use HTML radiobuttons to represent a multiple choice with one answer and HTML checkboxes to represent a multiple choice with more answers. You should of course also know the basic concepts of Java since the JSP/Servlet API is based on Java. At coreservlets.com you can find a good set of JSP/Servlet tutorials. Play around with it, learn the concepts. JSP is a server side view technology where you can write HTML in and control the flow with taglibs and access backend data with EL. Servlet is a server side API with which you can control, preprocess and postprocess HTTP requests.

Once you understand all the stuff, you should be able to develop the Quiz at your own. If you stucks at a specific technical step, you're always free to ask a question here at SO. E.g. "How do I submit a form to a servlet?", "How do I retain data in subsequent requests?", "How do I redisplay submitted data?", etcetera.

If this is actually homework, I would also consult your tutor and classmates for assistance. Try working in a team. You should also have got course and learning materials, make use of them. If this is actually an interview question or a job assignment then, well, I'd question if you made the right choices.

BalusC
Thanks for the informative answer, yes it is homework and I already do know java but not a lot on JSP coding. I consider myself to be an expert in HTML, CSS, Javascript etc. and forms. So there's no problem there. I just actually needed to know how I could show up a new question when a form button is clicked.
Sandeep Bansal
Store the questions as a `List<Question>` and keep a requestbased index which you can in/decrement so that you know which question is currently in progress.
BalusC
A: 

I have a created a similar application long back using Java, resource properties, check the following classes

  1. RandomComaprator.java
  2. RandomInt.java
  3. RandonQuestions.java
  4. TestQuestions.java
  5. questions.properties

Here are the five classes that i have created, when you run this program RandonQuestions.java

All the questions will be picked randomly from the properties files and display it to the users, The qusetions in the properties can be converted to the quiz kind of questions

RandomComparator.java public class RandomComparator implements Comparator {

    Random random = new Random();

    public int compare(Integer arg0, Integer arg1) {
        return random.nextInt() - random.nextInt();
    }
}

RandomInt.java

public class RandomInt {

    /**
     * @param args
     */
    public static void main(String[] args) {

        RandomInt int1 = new RandomInt();
        Set<Integer> s = new HashSet<Integer>();
        //System.out.println(s);

        //System.out.println(int1.randomSet(s, 2, 1));

        System.out.println(int1.randamPriorityQueue(s, 5, 2));


    }

    private Set<Integer> randomSet(Set<Integer> randomNumbers, int totalQuestions,
            int numOfQuestionToDisplay) {

        while (randomNumbers.size() < numOfQuestionToDisplay) {
            Random randomGenerator = new Random();
            int randomInt = randomGenerator.nextInt(totalQuestions);
            randomNumbers.add(randomInt);
        }

        return randomNumbers;
    }

    private Set<Integer> randamPriorityQueue(Set<Integer> randomNumbers, int totalQuestions,
            int numOfQuestionToDisplay) {

        PriorityQueue<Integer> queue = new PriorityQueue<Integer>(
                totalQuestions, new RandomComparator());

        for (int i = 0; i < totalQuestions; i++) {
            queue.add(i);
        }
        Iterator<Integer> iterator = queue.iterator();

        for (int i = 0; i < numOfQuestionToDisplay && iterator.hasNext(); i++) {
            randomNumbers.add(iterator.next());
        }
        return randomNumbers;

    }

    private Set<Integer> randomSet(Set<Integer> randomNumbers) {
        if (randomNumbers.size() == 2) {
            return randomNumbers;
        }

        Random randomGenerator = new Random();
        for (int i = 0; i < 2; i++) {
            int randomInt = randomGenerator.nextInt(4);
            System.out.println("Generated : " + randomInt);
            System.out.println(randomNumbers + " " + randomNumbers.size());
            if (randomNumbers.size() == 2) {
                return randomNumbers;
            }
            randomNumbers.add(randomInt);

            randomSet(randomNumbers);
        }
        return randomNumbers;
    }
}

TestQuestions.java

public class TestQuestions {

    private static ResourceBundle s_rb;
    private static TestQuestions s_questions; 

    /**
     * Private Constructor
     */
    private TestQuestions()
    {
        s_rb = ResourceBundle.getBundle("org.com.questions");
    }

    /**
     * static method to get the singleton instance
     */
    public static TestQuestions getInstance()
    {
        if(s_questions == null)
            s_questions = new TestQuestions();
        return s_questions;
    }

    /**
     * Get the ResourceBundle
     */
    public final ResourceBundle getBundle()
    {
        return s_rb;
    }

    /**
     * Get the String
     */

    public final String getString(String key)
    {
        return s_rb.getString(key);
    }
}

Following is the questions.properties 0=What is the difference between an Interface and an Abstract class?
1=What is the purpose of garbage collection in Java, and when is it used?
2=Describe synchronization in respect to multithreading? 3=Explain different way of using thread?
4=What are pass by reference and passby value?
5=What is HashMap and Map? 6=Difference between HashMap and HashTable? 7=Difference between Vector and ArrayList? 8=Difference between Swing and Awt? 9=What is the difference between a constructor and a method?
10=What is an Iterator? 11=State the significance of public, private, protected, default ? 12=What is an abstract class? 13=What is static in java?
14=What is final? 15=What releases of Java technology are currently available? What do they contain? 16=What platforms is the JDK software available on? 17=Should I use the Production Release or Reference Implementation of the Solaris JDK software and JRE? 18=What about a version for my favorite platform? When can I get it? 19=How do I download Java technology and/or JDK software? How do I install it? 20=Where can I find information about HotJava? 21=How can I get started with programming in Java? 22=Do I need special server software to use applets? 23=Who is licensing Java technology? 24=Is JavaScript available? How do I find out more about it? 25=What are the security problems I've heard about JavaScript scripts? 26=I can't find the API documentation on any classes in the sun.* packages. 27=Why developers should not write programs that call 'sun' packages 28=Where can I get the Java programming language source code? 29=What is the Java Version/Naming convention? 30=What is the difference between a constructor and a method?

RandonQuestions.java

public class RandonQuestions {

    /**
     *  Display the questions.
     */
    public void getQuestions() {
        String s = "";
        StringBuilder sb = new StringBuilder(s);
        int quest[] = getNumberOfQuestions();

        for (int i = 0; i < quest.length; i++) {
            String key = "" + quest[i] + "";
            sb.append(i + 1 + "" + ". ");
            sb.append(TestQuestions.getInstance().getBundle().getString(key)
                    + "\n");
        }
        System.out.println(sb);
    }
    /**
     * @return int array
     */
    public int[] getNumberOfQuestions() {
        PriorityQueue<Integer> pq;
        Random rm;
        int a = 5;
        int ar[] = new int[5];
        pq = new PriorityQueue<Integer>(a, new Comparator<Integer>() {
            public int compare(Integer i, Integer j) {
                int result = i % 2 - j % 2;
                if (result == 0)
                    result = i - j;
                return result;
            }
        });

        rm = new Random();
        for (int i = 0; i < a; i++) {
            int number = rm.nextInt(30);
            pq.offer(number);
        }

        for (int i = 0; i < a; i++) {
            ar[i] = pq.poll();
        }
        return ar;
    }

    /**
     * @param args
     */
    public static void main(String[] args) {
        RandonQuestions obj = new RandonQuestions();
        obj.getQuestions();
    }
}

Put all the files in the folder and execute, this will create a proejct what you are looking for

harigm
A: 

OK, i appreciate your great work of coding and programing. Sometimes i alwasys wonder why must make a quiz with those complex things. Actually we can make quizzes with some easy to use quiz maker software and free online quiz maker sites. Maybe you can have a try.

Celine