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