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

servlet that forwards request to JSP and setAttribute.

hi!

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 {

    response.setContentType("text/html");


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


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

     RequestDispatcher dispatcher =
       getServletContext().getRequestDispatcher("/hw9/PrimesOne.jsp");

     dispatcher.forward(request, response);

  }

   private BigInteger generatePrimeNumber(String len){

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

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

   }
}
0
dkim18
Asked:
dkim18
1 Solution
 
udilipkumarCommented:
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;
     if(size==null)
        return new Integer(i);
     else
   {
       int noSize = Integer.parseInt(size);
       if(noSize<=1)
       i = 1;
       else
       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)) {
                n++;
            }
            return n;
        }


Hope this shuld help you

Cheers
Dilip Kumar
0

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