Tugas ServletJSP

parents
<%@page import="org.perpustakaanku.model.*"%>
<html>
<head>
<title>Perpustakaan</title>
</head>
<body>
<header><h1>Perpustakaan</h1>
</header>
<nav>
<a href="/PerpustakaanKu/Home.jsp">Home</a>
<a href="/PerpustakaanKu/DaftarBuku.jsp">Daftar Buku</a>
<% if(session.getAttribute("username")!=null) {%>
<a href="/PerpustakaanKu/LogOut.do">LogOut</a>
<%}else {%>
<a href="/PerpustakaanKu/Login.jsp">Login</a>
<%} %>
</nav>
<table border="1">
<tr>
<th>Judul</th>
<th>ISBN</th>
<th>Penerbit</th>
<th>Pengarang</th>
<th>Jumlah</th>
<th>Pesan</th>
</tr>
<% for(Buku buku : DaftarBuku.getBukus()){%>
<tr>
<td> <% out.print(buku.getJudul()); %></td>
<td> <% out.print(buku.getIsbn()); %></td>
<td> <% out.print(buku.getPenerbit()); %></td>
<td> <% out.print(buku.getPengarang()); %></td>
<td> <% out.print(buku.getJumlah()); %></td>
<td> <a href="#">Pesan</a></td>
</tr>
<%} %>
</table>
</body>
</html>
\ No newline at end of file
<html>
<head>
<title>Perpustakaan</title>
</head>
<body>
<header>
<h1>Perpustakaan</h1>
</header>
<nav>
<a href="/PerpustakaanKu/Home.jsp">Home</a>
<a href="/PerpustakaanKu/DaftarBuku.jsp">Daftar Buku</a>
<% if(session.getAttribute("username")!=null) {%>
<a href="/PerpustakaanKu/LogOut.do">LogOut</a>
<%}else {%>
<a href="/PerpustakaanKu/Login.jsp">Login</a>
<%} %>
</nav>
</body>
</html>
\ No newline at end of file
<html>
<head>
<title>Perpustakaan</title>
</head>
<body>
<header>
Perpustakaan
</header>
<nav>
<a href="/PerpustakaanKu/Home.jsp">Home</a>
<a href="/PerpustakaanKu/DaftarBuku.jsp">Daftar Buku</a>
<% if(session.getAttribute("username")!=null) {%>
<a href="/PerpustakaanKu/LogOut.do">LogOut</a>
<%}else {%>
<a href="/PerpustakaanKu/Login.jsp">Login</a>
<%} %>
</nav>
<form action="/PerpustakaanKu/Login.do" method="post">
<table>
<tr>
<td>Username</td>
<td><input type = "text" name="username"></td>
</tr>
<tr>
<td>Password</td>
<td><input type = "password" name="password"></td>
</tr>
<tr>
<td colspan = "2"><input type="submit" name="submit" value="login"></td>
</tr>
</table>
</form>
</body>
</html>
\ No newline at end of file
Manifest-Version: 1.0
Class-Path:
<%@page import="org.perpustakaanku.model.*"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title></title>
</head>
<body>
<table border="1">
<tr>
<th>Username</th>
<th>judul</th>
<th>status</th>
</tr>
<% for(Peminjaman peminjaman: DaftarPeminjaman.getPeminjamans()){%>
<tr>
<td> <% out.print(peminjaman.getUsername()); %></td>
<td> <% out.print(peminjaman.getJudul()); %></td>
<td> <% out.print(peminjaman.getStatus()); %></td>
</tr>
<%} %>
</table>
</body>
</html>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>PerpustakaanKu</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
</web-app>
\ No newline at end of file
package org.perpustakaanku.model;
public class Buku {
private String isbn, judul, penerbit, pengarang;
private Integer jumlah;
private Buku()
{
super();
}
public Buku(String isbn, String judul, String penerbit, String pengarang, Integer jumlah) {
super();
this.isbn = isbn;
this.judul = judul;
this.penerbit = penerbit;
this.pengarang = pengarang;
this.jumlah = jumlah;
}
public String getIsbn() {
return isbn;
}
/**
* @param isbn the isbn to set
*/
public void setIsbn(String isbn) {
this.isbn = isbn;
}
/**
* @return the judul
*/
public String getJudul() {
return judul;
}
/**
* @param judul the judul to set
*/
public void setJudul(String judul) {
this.judul = judul;
}
/**
* @return the deskripsi
*/
public String getPenerbit() {
return penerbit;
}
/**
* @param penerbit the penerbit to set
*/
public void setPenerbit(String penerbit) {
this.penerbit = penerbit;
}
public String getPengarang() {
return pengarang;
}
/**
* @param penulis the penulis to set
*/
public void setPenulis(String pengarang) {
this.pengarang = pengarang;
}
public Integer getJumlah() {
return jumlah;
}
/**
* @param banyak the banyak to set
*/
public void setJumlah(Integer jumlah) {
this.jumlah = jumlah;
}
}
package org.perpustakaanku.model;
import java.util.ArrayList;
import java.util.List;
import org.perpustakaanku.model.Buku;
public class DaftarBuku {
private static List<Buku> bukus = new ArrayList<>();
static{
bukus.add(new Buku("1","Laravel","Awan Pribadi","LokoPedia",2));
bukus.add(new Buku("2","Matdis","Anthon","Gramedia",1));
bukus.add(new Buku("3","Longman TOEFL Test","Dewi","Philips",2));
bukus.add(new Buku("4","Motivasi","Ary Cahya","Grafindo",3));
bukus.add(new Buku("5","PABWE","Billy","Media",1));
}
/**
* @return the bukus
*/
public static List<Buku> getBukus() {
return bukus;
}
/**
* @param bukus the bukus to set
*/
public static void setBukus(List<Buku> bukus) {
DaftarBuku.bukus = bukus;
}
/**
*
* @param buku the bukus to add
*/
public static void addToListBukus(Buku buku){
DaftarBuku.bukus.add(buku);
}
/**
*
* @param judul judul from bukus
* @return bukus which has the same judul
*/
public static Buku getBukuFromList(String judul){
for(Buku buku : DaftarBuku.bukus){
if(buku.getJudul().compareTo(judul)==0){
return buku;
}
}
return null;
}
public static boolean isBukuExist(String judul){
for(Buku buku : bukus){
if(buku.getJudul().compareTo(judul)==0 && buku.getJumlah()!=0){
return true;
}
}
return false;
}
}
package org.perpustakaanku.model;
import java.util.ArrayList;
import java.util.List;
public class DaftarPeminjaman {
private static List<Peminjaman> peminjamans = new ArrayList<>();
/**
* @return the peminjamans
*/
public static List<Peminjaman> getPeminjamans() {
return peminjamans;
}
/**
* @param peminjamans the peminjamans to set
*/
public static void setPeminjamans(List<Peminjaman> peminjamans) {
DaftarPeminjaman.peminjamans = peminjamans;
}
/**
*
* @param peminjaman the peminjamans to add
*/
public static void addToListPeminjamans(String username, String judul){
Buku buku = DaftarBuku.getBukuFromList(judul);
if(buku == null || buku.getJumlah()==0){
return;
}
else{
Peminjaman peminjaman = new Peminjaman(username,judul,"pending");
DaftarPeminjaman.peminjamans.add(peminjaman);
buku.setJumlah(buku.getJumlah()-1);
}
}
public static Peminjaman getPeminjamanFromList(String username, String judul){
Buku buku = DaftarBuku.getBukuFromList(judul);
if(buku==null || buku.getJumlah()==0)return null;
else{
for(Peminjaman peminjaman : DaftarPeminjaman.peminjamans){
if(peminjaman.getUsername().compareTo(username)== 0
&& peminjaman.getJudul().compareTo(judul)== 0){
return peminjaman;
}
}
return null;
}
}
public static void deletePeminjaman(String username, String judul){
Peminjaman peminjaman = DaftarPeminjaman.getPeminjamanFromList(username, judul);
if(peminjaman==null)return;
else{
DaftarPeminjaman.peminjamans.remove(peminjaman);
Buku buku = DaftarBuku.getBukuFromList(judul);
buku.setJumlah(buku.getJumlah()+1);
}
}
public static boolean isPeminjamanExist(String username, String judul){
for(Peminjaman peminjaman : peminjamans){
if(peminjaman.getUsername().compareTo(username)==0 && peminjaman.getJudul().compareTo(judul)==0){
return true;
}
}
return false;
}
public static void changeStatusPeminjaman(String username,String judul,String status){
Peminjaman peminjaman = getPeminjamanFromList(username, judul);
peminjaman.setStatus(status);
}
}
package org.perpustakaanku.model;
import java.util.ArrayList;
import java.util.List;
public class DaftarUser {
private static List<User> users = new ArrayList<>();
static{
users.add(new User("fany","fany","1","admin"));
users.add(new User("melisa","melisa","2","user"));
users.add(new User("ranty","ranty","3","user"));
users.add(new User("nanda","nanda","4","user"));
users.add(new User("felicia","felicia","5","user"));
}
/**
* @return the users
*/
public static List<User> getUsers() {
return users;
}
/**
* @param users the users to set
*/
public static void setUsers(List<User> users) {
DaftarUser.users = users;
}
/**
*
* @param user the users to add
*/
public static void addToListUsers(User user){
DaftarUser.users.add(user);
}
/**
*
* @param username username from users
* @return user which has same username in list user
*/
public static User getUserFromList(String username){
for(User user : DaftarUser.users){
if(user.getUsername().compareTo(username)==0){
return user;
}
}
return null;
}
public static boolean isUserExist(String username, String password){
for(User user : DaftarUser.users){
if(user.getUsername().compareTo(username)==0 && user.getPassword().compareTo(password)==0){
return true;
}
}
return false;
}
}
package org.perpustakaanku.model;
public class Peminjaman {
String username,judul,status;
public Peminjaman() {
super();
}
public Peminjaman(String username, String judul,String status) {
super();
this.username = username;
this.judul = judul;
this.status = status;
}
/**
* @return the status
*/
public String getStatus() {
return status;
}
/**
* @param status the status to set
*/
public void setStatus(String status) {
this.status = status;
}
/**
* @return the username
*/
public String getUsername() {
return username;
}
/**
* @param username the username to set
*/
public void setUsername(String username) {
this.username = username;
}
/**
* @return the judul
*/
public String getJudul() {
return judul;
}
/**
* @param judul the judul to set
*/
public void setJudul(String judul) {
this.judul = judul;
}
}
package org.perpustakaanku.model;
public class User {
private String username, password,nim,role;
public User() {
super();
}
public User(String username, String password, String nim, String role) {
super();
this.username = username;
this.password = password;
this.nim = nim;
this.role = role;
}
/**
* @return the username
*/
public String getUsername() {
return username;
}
/**
* @param username the username to set
*/
public void setUsername(String username) {
this.username = username;
}
/**
* @return the password
*/
public String getPassword() {
return password;
}
/**
* @param password the password to set
*/
public void setPassword(String password) {
this.password = password;
}
/**
* @return the nim
*/
public String getNim() {
return nim;
}
/**
* @param nim the nim to set
*/
public void setNim(String nim) {
this.nim = nim;
}
/**
* @return the role
*/
public String getRole() {
return role;
}
/**
* @param role the role to set
*/
public void setRole(String role) {
this.role = role;
}
}
package org.perpustakaanku.servlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.perpustakaanku.model.DaftarUser;
import org.perpustakaanku.model.User;
/**
* Servlet implementation class Login
*/
@WebServlet("/Login.do")
public class Login extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public Login() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
//response.getWriter().append("Served at: ").append(request.getContextPath());
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
//doGet(request, response);
response.setContentType("text/jsp");
//PrintWriter pw = response.getWriter();
String userInput = request.getParameter("username");
String passwordInput = request.getParameter("password");
if(DaftarUser.isUserExist(userInput, passwordInput)){
User user = DaftarUser.getUserFromList(userInput);
HttpSession session = request.getSession();
session.setAttribute("username", user.getUsername());
session.setAttribute("role", user.getRole());
response.sendRedirect("/PerpustakaanKu/Home.jsp");
}
else{
response.sendRedirect("/PerpustakaanKu/Login.jsp");
}
}
}
package org.perpustakaanku.servlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
/**
* Servlet implementation class Logout
*/
@WebServlet("/LogOut.do")
public class Logout extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public Logout() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
//response.getWriter().append("Served at: ").append(request.getContextPath());
HttpSession session = request.getSession();
session.invalidate();
response.sendRedirect("/PerpustakaanKu/Home.jsp");
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
//doGet(request, response);
HttpSession session = request.getSession();
session.invalidate();
response.sendRedirect("/PerpustakaanKu/Home.jsp");
}
}
package org.perpustakaanku.servlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.perpustakaanku.model.User;
/**
* Servlet implementation class Pesan
*/
@WebServlet("/Pesan.do")
public class Pesan extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public Pesan() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
response.getWriter().append("Served at: ").append(request.getContextPath());
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment