Answer to Question #190684 in Java | JSP | JSF for sagun poudel

Question #190684

Create an applet which take a number as input and display whether it is prime number or not. Use appropriate components, layout and formatting in your program in JAVA.




1
Expert's answer
2021-05-10T17:17:44-0400

// this is the servlet java code

import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import java.io.IOException;

import java.io.PrintWriter;


public class PrimeServlet extends HttpServlet {


@Override

protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

String input = req.getParameter("number");

resp.setContentType("text/html");

PrintWriter writer = resp.getWriter();

if (input.equals("0") || (!input.matches("\\d+"))) {

writer.println("<h2>Please, enter the ordinal value.</h2>");

return;

}

int n = Integer.parseInt(input);

boolean isPrime = n != 1;

if (n < 3) {

for (int i = 2; i < n; i++)

if (n % i == 0) {

isPrime = false;

break;

}

}


try {

writer.println("<h2>The number " + n + " is " + (isPrime ? "" : "not ") + "prime.</h2>");

} finally {

writer.close();

}

}


@Override

protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

doGet(req, resp);


}


}


// this is pom.xml file


<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.bloomingday</groupId>
    <artifactId>com.bloomingday.servlet</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>war</packaging>
  <dependencies>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.1.0</version>
        </dependency>
    </dependencies>
<startupUrls>
     <startupUrl>primeNumbersPage.html</startupUrl>
</startupUrls>

</project>


// this is web.xml file


<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xmlns="http://java.sun.com/xml/ns/javaee"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
         version="3.0">

    <servlet>
        <servlet-name>PrimeServlet</servlet-name>
        <servlet-class>com.bloomingday.servlet.PrimeServlet</servlet-class>
    </servlet>

    <servlet-mapping>
        <servlet-name>PrimeServlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

</web-app>

// primeNumbersPage.html file

<html>
   <body>
      <form action = "PrimeServlet" method = "GET">
         Please enter the ordinal value: <input type = "text" name = "number" />
      </form>
   </body>
</html>

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS