本文发表在 rolia.net 枫下论坛I am learning with Java Servlets
How I can use a function named isPrime which takes an int parameter and returns a boolean result?
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
response.setContentType("application/xhtml+xml");
response.setCharacterEncoding("UTF-8"); // set the character encoding BEFORE getting the PrintWriter object
PrintWriter out = response.getWriter();
int n,i,flag=0;
String ns= request.getParameter("param");
n=Integer.parseInt(ns);
if(n>1)
{
for(i=2;i<=n/2;i++)
{
if(n%i==0)
{
flag=1;
break;
}
}
}
out.println("<?xml version=\"1.0\" encoding=\"UTF-8\" ?>");
out.println("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.1//EN\" \"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd\">");
out.println("<html xmlns=\"http://www.w3.org/1999/xhtml\">");
out.println("<head>");
out.println("<title>Check Prime Number </title>");
out.println("</head>");
out.println("<body>");
out.println("<p>You Enter Number is: "+request.getParameter("param")+"<br />");
out.println("</p>");
out.println("<p>");
if(flag==0)
{
out.println(n+" is a prime no.");
}
else
{
out.println(n+" is not a prime no.");
}
out.println("</p>");
out.println("</body>");
out.println("</html>");
}更多精彩文章及讨论,请光临枫下论坛 rolia.net
How I can use a function named isPrime which takes an int parameter and returns a boolean result?
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
response.setContentType("application/xhtml+xml");
response.setCharacterEncoding("UTF-8"); // set the character encoding BEFORE getting the PrintWriter object
PrintWriter out = response.getWriter();
int n,i,flag=0;
String ns= request.getParameter("param");
n=Integer.parseInt(ns);
if(n>1)
{
for(i=2;i<=n/2;i++)
{
if(n%i==0)
{
flag=1;
break;
}
}
}
out.println("<?xml version=\"1.0\" encoding=\"UTF-8\" ?>");
out.println("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.1//EN\" \"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd\">");
out.println("<html xmlns=\"http://www.w3.org/1999/xhtml\">");
out.println("<head>");
out.println("<title>Check Prime Number </title>");
out.println("</head>");
out.println("<body>");
out.println("<p>You Enter Number is: "+request.getParameter("param")+"<br />");
out.println("</p>");
out.println("<p>");
if(flag==0)
{
out.println(n+" is a prime no.");
}
else
{
out.println(n+" is not a prime no.");
}
out.println("</p>");
out.println("</body>");
out.println("</html>");
}更多精彩文章及讨论,请光临枫下论坛 rolia.net