0% found this document useful (0 votes)
37 views

Java Servlet

The document describes two servlet programs - one to find prime numbers between two user-input numbers, and another to find the greatest common divisor (GCD) of two user-input numbers. For each program, the document provides the necessary code snippets, including the web.xml configuration, HTML form, and Java servlet class. The prime number servlet uses a for loop to check if each number from the input range is prime, while the GCD servlet uses a for loop to find the largest number that divides both inputs.

Uploaded by

019bim017
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
37 views

Java Servlet

The document describes two servlet programs - one to find prime numbers between two user-input numbers, and another to find the greatest common divisor (GCD) of two user-input numbers. For each program, the document provides the necessary code snippets, including the web.xml configuration, HTML form, and Java servlet class. The prime number servlet uses a for loop to check if each number from the input range is prime, while the GCD servlet uses a for loop to find the largest number that divides both inputs.

Uploaded by

019bim017
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

Question 1:

Write a servlet program to find the prime number between a and b where a and b is user input
provided by HTML form.

Code:
In web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-
app_3_1.xsd">
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>/faces/*</url-pattern>
</servlet-mapping>

<servlet>
<servlet-name>display</servlet-name>
<servlet-class>display</servlet-class>
</servlet>
<welcome-file-list>
<welcome-file>faces/index.xhtml</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>prime</servlet-name>
<servlet-class>prime</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>prime</servlet-name>
<url-pattern>/prime</url-pattern>
</servlet-mapping>
</web-app>
In newhtml.html
<!DOCTYPE html>
<html>
<head>
<title>TODO supply a title</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>

<div>TODO write content</div>


<form action="prime" method="post">
<input type="number" name="no1">
<input type="number" name="no2">
<input type="submit">
</form>
</body>
</html>
Output:
In prime.java

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet(urlPatterns = {"/prime"})
public class prime extends HttpServlet {

protected void doPost(HttpServletRequest request, HttpServletResponse response)


throws ServletException, IOException {
try (PrintWriter out = response.getWriter()) {
Integer no1=Integer.parseInt(request.getParameter("no1"));
Integer no2=Integer.parseInt(request.getParameter("no2"));
out.print("The prime numbers are");
for(int i=no1;i<=no2;i++)
{
int c=0;
for(int j=2;j<=i/2;j++)
{
if(i%j==0)
{
c++;
}
}
if(c==0)
{
out.print(i);
out.print("<br>");
}
}

}
}
}

Output:
Question 2:

Write a servlet program to find the GCD of two numbers where these two numbers should be
provided by the HTML form.

Code:
In web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-
app_3_1.xsd">
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>/faces/*</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
<context-param>
<param-name>javax.faces.PROJECT_STAGE</param-name>
<param-value>Development</param-value>
</context-param>
<servlet>
<servlet-name>display</servlet-name>
<servlet-class>display</servlet-class>
</servlet>
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>display</servlet-name>
<url-pattern>/display</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>faces/index.xhtml</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>findingGCD</servlet-name>
<servlet-class>findingGCD</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>findingGCD</servlet-name>
<url-pattern>/findingGCD</url-pattern>
</servlet-mapping>
</web-app>
In gcd.html
<!DOCTYPE html>
<html>
<head>
<title>Finding GCD</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>

<form action="findingGCD" method="post">


<input type="number" name="no1">
<input type="number" name="no2">
</br>
<input type="submit">

</form>
</body>
</html>
Output:
In findingGCD

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet(urlPatterns = {"/findingGCD"})
public class findingGCD extends HttpServlet {

protected void doPost(HttpServletRequest request, HttpServletResponse response)


throws ServletException, IOException {
try (PrintWriter out = response.getWriter()) {
Integer no1=Integer.parseInt(request.getParameter("no1"));
Integer no2=Integer.parseInt(request.getParameter("no2"));

int gcd=1;
for(int i=1;i<=no1 && i<=no2;i++)
{

if(no1%i==0 && no2%i==0)


{
gcd=i;
}
}
out.print("The gcd is:"+gcd);
}

}
}

Output:

You might also like