Logo 
Search:

OOAD Articles

Submit Article
Home » Articles » OOAD » Servlet RSS Feeds

Servlet application with HTML form asking user to input an integer. The response HTML page should show the integer and all the prime numbers smaller

Posted By: Milind Mishra     Category: OOAD     Views: 6052

Develop a servlet application with HTML form asking user to input an integer. The response HTML page should show the integer and all the prime numbers smaller than that input integer.

Code for Servlet application with HTML form asking user to input an integer. The response HTML page should show the integer and all the prime numbers smaller in OOAD

// primeno.html
                
<html>
<form action="http://localhost:1977/examples/servlet/prime_no" name=form1 method=POST>
enter a number
<input type="text" name="textbox1">
<br>
<input type="submit" name="button1"value="find prime numbers less than this number">
</form>
</html>


//prime_no.java

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
publicclass prime_no extends HttpServlet
{
  publicvoid doPost(HttpServletRequest req,HttpServletResponse res) throws IOException,ServletException
{
  try
    {
        String tvalue=req.getParameter("textbox1");
        int num=Integer.parseInt(tvalue);
        PrintWriter out=res.getWriter();
        int flag=1,j;
        out.println("PRIME NOS.LESS THAN "+num+" ARE:");
        for(int i=2;i<=num;i++)
            {
            for(j=2;j<i;j++)
                {
                    if(i%j==0)
                    {    
                                     flag=0;
                         break;
                    }
                    else
                         flag=1;    
                }
            if(flag==1)    
                out.println(j);
            }    

    }
  catch(Exception e)
    {

       System.out.println(e);
    }   

}


// OUTPUT

----------------
HTML FORM INPUT:
----------------

enter a number 50

-----------
RESPONSE
-----------

PRIME NOS.LESS THAN 50 ARE:
2
3
5
7
11
13
17
19
23
29
31
37
41
43
47


----------------
HTML FORM INPUT:
----------------

enter a number 19

-----------
RESPONSE
-----------

PRIME NOS.LESS THAN 50 ARE:
2
3
5
7
11
13
17
19
  
Share: 



Milind Mishra
Milind Mishra author of Servlet application with HTML form asking user to input an integer. The response HTML page should show the integer and all the prime numbers smaller is from India.
 
View All Articles

Related Articles and Code:


 

Other Interesting Articles in OOAD:


 
Please enter your Comment

  • Comment should be atleast 30 Characters.
  • Please put code inside [Code] your code [/Code].

 
No Comment Found, Be the First to post comment!