servlet that forwards request to JSP and setAttribute.

Posted on 2005-04-10
Last Modified: 2010-04-01

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 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));

Question by:dkim18
    1 Comment
    LVL 1

    Accepted 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

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Suggested Solutions

    After several days of searching and hunting for limited documentation, I wanted to share this guide to hopefully save someone the hassle of trying to figure this out on their own. I have tested this on Xendesktop 7.1 and PS 4.5 running simultaneous…
    ADCs have gained traction within the last decade, largely due to increased demand for legacy load balancing appliances to handle more advanced application delivery requirements and improve application performance.
    Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
    Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    22 Experts available now in Live!

    Get 1:1 Help Now