disini saya akan membuat Form input / daftar data dari data karyawan. halaman yang akan dibuat ada 2 buah halaman, yaitu halaman untuk login, dan halaman untuk input sekaligus menampilkan data.
Halaman Login tampilan nya sebagai berikut :
Halaman Input Data sebagai berikut :
Halaman Login , kode nya sebagai berikut :
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; import javax.servlet.http.HttpSession; public class loginServlet extends HttpServlet { final String VALID_NAME = "admin"; final String PASSWORD = "admin123"; final String CODE_VALID = "firsar"; protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { HttpSession session = request.getSession(true); if (request.getParameter("logout") != null) { session.removeAttribute("login"); response.sendRedirect("loginServlet"); } response.setContentType("text/html;charset=UTF-8"); PrintWriter out = response.getWriter(); try { out.println(""); out.println(""); out.println(""); out.println(""); out.println(""); out.println(""); out.println(""); out.println(""); out.println("Sign In "); out.println(""); out.println(""); String username = request.getParameter("username"); String password = request.getParameter("password"); boolean valid = false; if (username != null && password != null){ if ((username.equalsIgnoreCase(VALID_NAME)) && (password.equalsIgnoreCase(PASSWORD))) { valid = true; session.setAttribute("login", username); response.sendRedirect(response.encodeRedirectURL("employeeServlet")); ; } if (!valid) { out.println("Invalid logins!"); } } out.println(""); out.println(" "); out.println(" "); out.println(" " + ""); out.println(""); } finally { } } //FIRMAN SARI NPM : 005131121027"); out.println(""); out.println("/** * Handles the HTTP }GET
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 HTTPPOST
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"; }//
Sedangkan untuk halaman input data sebagai berikut :
import java.io.IOException; import java.io.PrintWriter; import java.util.Enumeration; import java.util.Hashtable; 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; @WebServlet(urlPatterns = {"/employeeServlet"}) public class employeeServlet extends HttpServlet { Hashtable data = new Hashtable(); /** * Processes requests for both HTTPGET
andPOST
* 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 { HttpSession session = request.getSession(true); if (request.getParameter("logout") != null) { session.removeAttribute("login"); response.sendRedirect("loginServlet"); } if(session.getAttribute("login") == null){ response.sendRedirect("loginServlet"); } String nik = request.getParameter("inputNIK"); String nama = request.getParameter("inputNama"); String jk = request.getParameter("rbJK"); String alamat = request.getParameter("inputAlamat"); String email = request.getParameter("inputEmail"); String jabatan = request.getParameter("selJabatan"); String checked1 = ""; String checked2 = ""; String selected = ""; String k = request.getParameter("k"); String cmd = request.getParameter("cmd"); response.setContentType("text/html;charset=UTF-8"); PrintWriter out = response.getWriter(); try { if(nik == null) nik = ""; if(nama == null) nama = ""; if(alamat == null) alamat = ""; if(email == null) email = ""; if (cmd != null) { if(cmd.equalsIgnoreCase("logout")){ session.removeAttribute("login"); response.sendRedirect("loginServlet"); } if (cmd.equalsIgnoreCase("e")) { String[] recEdit = (String[])data.get(k); nik = recEdit[0]; nama = recEdit[1]; jk = recEdit[2]; alamat = recEdit[3]; email = recEdit[4]; jabatan = recEdit[5]; if(jk.equals("Laki-laki")) checked1 = "checked"; else if(jk.equals("Perempuan")) checked2 = "checked"; if(jabatan.equalsIgnoreCase("Manager")) selected = "selected"; else if(jabatan.equalsIgnoreCase("Staff")) selected = "selected"; else selected = ""; data.replace(k, new String[] { nik, nama, jk, alamat, email, jabatan }); } if (cmd.equalsIgnoreCase("d")) { data.remove(k); } } else { if(nik != null && nama != null && jk != null && alamat != null && email != null && jabatan != null) { data.put(nik, new String[] { nik, nama, jk, alamat, email, jabatan }); nik = ""; nama = ""; jk = ""; alamat = ""; email = ""; jabatan = ""; } } out.println(""); out.println(""); out.println(""); out.println(""); out.println(""); out.println(""); out.println(""); out.println(""); out.println("Tugas UTS - Firman Sari "); out.println(""); out.println(""); out.println(""); out.println(""); out.println(""); out.println(""); }finally { out.close(); } } //
"); out.println(" "); out.println("
" + "FIRMAN SARI"); out.println("
" + "NPM : 005131121027
" + "/** * Handles the HTTP }GET
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 HTTPPOST
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"; }//
No comments:
Post a Comment