• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 252
  • Last Modified:

servlet that forwards request to JSP and setAttribute.


I created a servlet that reads request parameter from the user, creates a prime umber of the specified size, and forwards the request to a JSP page that displays the result. However, the jsp page just only displays the number I put. What am I doing wrong?

package hw9;

import coreservlets.ServletUtilities;
import moreservlets.Primes;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.math.BigInteger;

public class PrimesOne extends HttpServlet {

  private int defalutSize = 30;

  public void doGet(HttpServletRequest request,
                    HttpServletResponse response)
      throws ServletException, IOException {


    String size = request.getParameter("primeSize");

    BigInteger primeNum = generatePrimeNumber(size);
    //String primeNum = "100";
      request.setAttribute("primeNum", primeNum);

     RequestDispatcher dispatcher =

     dispatcher.forward(request, response);


   private BigInteger generatePrimeNumber(String len){

         if( len==null){
               return Primes.nextPrime(Primes.random(defalutSize));
                  int length = Integer.valueOf(len).intValue();

               return Primes.nextPrime(Primes.random(length));

1 Solution
hi dkim,

Your servlet coding and jsp calling looks perfect. There seems to be some problem in the way you calculate the prime number of a given size. The generatePrimeNumber() method doesnt seem to work. Currently you are using some methods of your own Primes class. Please check whether its generating correct prime numbers for the size specified.

Alternatively you can use the following methods to generate the smallest prime number of the given size,

public Integer generatePrimeNumber(String size)
    int i = 0;
        return new Integer(i);
       int noSize = Integer.parseInt(size);
       i = 1;
       i = Math.power(10,noSize-1);
      return new Integer(getPrime(i));


        public boolean isPrime(int n) {
        // 2 is the smallest prime
            if (n <= 2) {
                return n == 2;
            // even numbers other than 2 are not prime
            if (n % 2 == 0) {
                return false;
            // check odd divisors from 3
            // to the square root of n
            for (int i = 3, end = (int)Math.sqrt(n);
            i <= end; i += 2) {
                if (n % i == 0) {
                    return false;
            return true;
      // find the smallest prime >= n
        public int getPrime(int n) {
            while (!isPrime(n)) {
            return n;

Hope this shuld help you

Dilip Kumar

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now