Tugas 1 Java Web Programming
Cara menghitung Bilangan Prima & Bilangan Fibonacci
Nama : Ardhy Irawan
NIM : 1111511661
1. Index.Jsp
Index.jsp terdapat 2 menu : 1. Bilangan Prima, 2.Bilangan Fibonacci
<html>
<head>
<meta
http-equiv="Content-Type" content="text/html;
charset=UTF-8">
<title>Tugas Java Web</title>
</head>
<style>
body{
background-color: coral;
}
h2{
text-overflow: ellipsis-word;
}
.table{
text-align: inherit;
align : center;
}
</style>
<body>
<center><h1><div style="color:
chartreuse;">Menghitung Bilangan Prima dan Fibonacci</div></h1>
<h2><a href="bilanganPrima.jsp">Bilangan
Prima</a></h2>
<h2><a href="bilanganFibonacci.jsp">Bilangan
Fibonacci</a></h2>
</center>
</body>
<center><h1>1111511661</h1></center>
<center><h1>Ardhy Irawan</h1></center>
</html>
------------------------------------------------------------------------------------------------------------
Tampilan
2. Bilanganprima.jsp
Bilanganprima.jsp menjelaskan tentang menghitung bilangan prima.
a.bilanganprima.jsp
<html>
<head>
<meta
http-equiv="Content-Type" content="text/html;
charset=UTF-8">
<title>Bilangan Prima</title>
</head>
<style>
.table{
background-color: aquamarine;
}
body{
background-color: aquamarine;
}
</style>
<body>
<center>
<h3>Masukkan Bilangan Pertama sebagai Angka awal dan Bilangan Kedua
sebagai batas akhir.</h3>
<a
href="index.jsp"> <input type="button"
value="Back" /> </a>
<form
action="controlerBilanganPrima"> <--Form Input Bilangan Prima -->
<table class="table" cellspacing="1"
cellpadding="5" border="1">
<td>
Bilangan Pertama </td> <td>:
</td> <td><input type="text" name="bil1"/> </td> <--Untuk Input bilangan pertama -->
<tr>
<td>
Bilangan Kedua </td> <td>: </td>
<td><input type="text" name="bil2"/>
</td><--Untuk Input bilangan kedua-->
<tr>
<td colspan="3" align="center">
<input type="submit" value="Hasil"
style="border-radius:5px;"/> </td>
</tr><--Button submit untuk proses hitung -->
</table>
</form>
</center> <br />
<center><h1>1111511661</h1></center>
<center><h1>Ardhy Irawan</h1></center>
</body>
</html>
-----------------------------------------------------------------------------------------------------------
Tampilan
a. Form Input Bilangan prima
b. Form Input Bilangan prima
b. ControlerBilanganPrima.Java <-Controler untuk hitung bilangan prima-->
package Handler;
import static java.awt.Color.red;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import
javax.servlet.http.HttpServletResponse;
/**
*
* @author
*/
public class controlerBilanganPrima
extends HttpServlet {
/**
* Processes requests
for both HTTP <code>GET</code> and <code>POST</code>
* methods.
*
* @param request
servlet request
* @param response
servlet response
* @throws
ServletException if a servlet-specific error occurs
* @throws IOException
if an I/O error occurs
*/
protected void
processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
try
(PrintWriter out = response.getWriter()) {
String a = request.getParameter("bil1");
out.println("Bilangan 1 = "+a+"<br>");
String b = request.getParameter("bil2");
out.println("Bilangan 2 = "+b+"<br>");
int intBil1 = Integer.parseInt(a);
int intBil2 = Integer.parseInt(b);
if (intBil1 < intBil2) {
int aa=0,j,i;
boolean isprima;
for(i=intBil1;i<=intBil2;i++){
isprima=true;
for(j=2;j<i;j++){
if(i%j==0){
isprima=false;
break;
}
}
if(isprima==true){
out.println(i+" ");
}
}
out.print("<center><h1>1111511661</h1></center>");
out.print("<center><h1>Ardhy
Irawan</h1></center>");
}else{
out.print("Angka yang Dimasukan Salah <br>");
out.print("<center><h1>1111511661</h1></center>");
out.print("<center><h1>Ardhy Irawan</h1></center>");
}
}
}
// <editor-fold
defaultstate="collapsed" desc="HttpServlet methods. Click on the
+ sign on the left to edit the code.">
/**
* Handles the HTTP
<code>GET</code> method.
*
* @param request
servlet request
* @param response
servlet response
* @throws
ServletException if a servlet-specific error occurs
* @throws IOException
if an I/O error occurs
*/
@Override
protected void
doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
/**
* Handles the HTTP
<code>POST</code> method.
*
* @param request
servlet request
* @param response
servlet response
* @throws
ServletException if a servlet-specific error occurs
* @throws IOException
if an I/O error occurs
*/
@Override
protected void
doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
/**
* Returns a short
description of the servlet.
*
* @return a String
containing servlet description
*/
@Override
public String
getServletInfo() {
return
"Short description";
}// </editor-fold>
}
-----------------------------------------------------------------------------------------------------------
Tampilan
c. Hasil mencari bilangan prima
-------------------------------------------------------------------------------------------------------------------------
3.bilanganfibonacci.jsp
Bilanganfibonacci.jsp menjelaskan tentang menghitng bilangan fibonacci.
a. bilanganfibonacci.jsp
<html>
<head>
<meta
http-equiv="Content-Type" content="text/html;
charset=UTF-8">
<title>Bilangan
Fibonacci</title>
</head>
<style>
body{
background-color: aquamarine ;
}
</style>
<body>
<center>
<h3>Masukkan Angka Pertama Dan Kedua Untuk Deret Fibonacci</h3>
<a
href="index.jsp">back</a>
<form
action="controlerBilanganFibonacci"> <--Form Input Bilangan Fibonacci-->
<table class="table" cellspacing="1"
cellpadding="1" border="1">
<td>
Bilangan Pertama </td> <td>:
</td> <td><input type="text"
name="bil1"/> </td><--Untuk Input bilangan pertama -->
<tr>
<td>
Bilangan Kedua </td> <td>: </td>
<td><input type="text" name="bil2"/>
</td><--Untuk Input bilangan kedua-->
<tr>
<td align="center" colspan="3">
<input type="submit" value="Hasil"/>
</td> <--Button submit untuk proses hitung -->
</tr>
</table>
</form>
<center><h1>1111511661</h1></center>
<center><h1>Ardhy Irawan</h1></center>
</center>
</body>
</html>
----------------------------------------------------------------------------------------------------------
Tampilan
a. Form Input Bilangan fibonacci
b. Form Input Bilangan fibonacci
b.ControlerBilanganFibonacci.Java <-Controler untuk hitung bilangan prima-->
package Handler;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import
javax.servlet.http.HttpServletRequest;
import
javax.servlet.http.HttpServletResponse;
public class controlerBilanganFibonacci
extends HttpServlet {
/**
* Processes requests
for both HTTP <code>GET</code> and <code>POST</code>
* methods.
*
* @param request
servlet request
* @param response
servlet response
* @throws
ServletException if a servlet-specific error occurs
* @throws IOException
if an I/O error occurs
*/
protected void
processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
try
(PrintWriter out = response.getWriter()) {
String a = request.getParameter("bil1");
out.println("Bilangan 1 = "+a+"<br>");
String b = request.getParameter("bil2");
out.println("Bilangan 2 = "+b+"<br>");
int intBil1 = Integer.parseInt(a);
int intBil2 = Integer.parseInt(b);
if (intBil1 < intBil2) {
int x = 0,y = 1;
for(int i = intBil1; i < intBil2; i++) {
//out.println(x);
if (x>= intBil1 && x<=intBil2){
out.println(x);
}
x = x+y;
y = x-y;
}
out.print("<center><h1>1111511661</h1></center>");
out.print("<center><h1>Ardhy
Irawan</h1></center>");
}
else {
out.println("Angka yang Anda masukkan SALAH!!");
out.print("<center><h1>1111511661</h1></center>");
out.print("<center><h1>Ardhy
Irawan</h1></center>");
}
}
}
// <editor-fold defaultstate="collapsed"
desc="HttpServlet methods. Click on the + sign on the left to edit the
code.">
/**
* Handles the HTTP
<code>GET</code> method.
*
* @param request
servlet request
* @param response
servlet response
* @throws
ServletException if a servlet-specific error occurs
* @throws IOException
if an I/O error occurs
*/
@Override
protected void
doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
/**
* Handles the HTTP
<code>POST</code> method.
*
* @param request
servlet request
* @param response
servlet response
* @throws
ServletException if a servlet-specific error occurs
* @throws IOException
if an I/O error occurs
*/
@Override
protected void
doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
/**
* Returns a short
description of the servlet.
*
* @return a String
containing servlet description
*/
@Override
public String
getServletInfo() {
return
"Short description";
}// </editor-fold>
}
-------------------------------------------------------------------------------------------------------------------------
c. Hasil mencari bilangan fibonacci