Skip to content

IF3110-II-14/IF3110-02-Simple-Blog-Java #39

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 18 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/build/
/dist/
26 changes: 26 additions & 0 deletions Delete/delete0_DataHandler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Bagian dari DataHandler class

//Method untuk mengidentifikasi record post
//pilih/set data (yg nanti akan dihapus)

public Post findPostById(int id) throws SQLException { }

Post selectedPst = new Post();

getDBConnection();

stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);
query = "SELECT * FROM post_table WHERE post_id = " + id;
System.out.println("\nExecuting: " + query);

rset = stmt.executeQuery(query);

while (rset.next()) {
selectedPst.setPostId(new Integer(rset.getInt("post_id")));
selectedPst.setTitle(rset.getString("title"));
selectedPst.setDate(rset.getString("date"));
selectedPst.setContent(rset.getString("content"));
}

return selectedPst;

20 changes: 20 additions & 0 deletions Delete/delete1_DataHandler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//Delete
//DataHandler.java

//Method untuk men-delete data


public class DataHandler {
public String deletePostById(int id) throws SQLException { }

getDBConnection();

stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);
sqlString = "DELETE FROM post_table WHERE post_id = " + id;
System.out.println("\nExecuting: " + sqlString);

stmt.execute(sqlString);

return "success";

}
15 changes: 15 additions & 0 deletions Delete/delete2_idx.jsp
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
//Bagian dari index.jsp

//Menambahkan link ke 'page' delete data


while (rset.next ()) {
out.println(
rset.getString("title") +
rset.getString("date") +
rset.getString("content") +
<a href=\"edit.jsp?pstid=" + rset.getInt(1) +"\">Edit</a>
<a href=\"delete_action.jsp?pstid=" + rset.getInt(1) + "\">Delete</a>");
);
}

17 changes: 17 additions & 0 deletions Delete/delete3_action.jsp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
//delete_action.jsp

//Halaman JSP untuk meng-handle aksi delete


//Add a jsp:usebean tag.
//As before, enterempsbean as the ID, and hr.DataHandler as the Class.
//Set the Scope to session, and click OK.

//Add a Scriptlet to the page. Enter the following code into the Insert Scriptlet dialog box:

Integer post_id = new Integer(request.getParameter("pstid"));
PostBean.deletePostById(post_id.intValue());

//Drag Forward from the Component Palette to add a jsp:forward tag to the page.
//In the Insert Forward dialog box, enter employees.jsp.

32 changes: 32 additions & 0 deletions Login/InvalidLogin.jsp.jsp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<!--invalidLogin.jsp(redirects to this page if something wrong)-->


<%@ page language="java" 
      contentType="text/html; charset=windows-1256"
      pageEncoding="windows-1256"
   %>
   <% 
    response.setHeader("Cache-Control","no-store,must-revalidate"); 
    response.setHeader("Pragma","no-cache"); 
    response.setDateHeader ("Expires", -1);
    new java.util.Date();
    %>

   <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"    
      "http://www.w3.org/TR/html4/loose.dtd">

   <html>

      <head>
         <meta http-equiv="Content-Type" 
            content="text/html; charset=windows-1256">
         <title>Invalid Login</title>
      </head>

      <body>
         <center>
            Sorry, you are not registered
         </center>
      </body>

   </html>
57 changes: 57 additions & 0 deletions Login/LoginPage.jsp.jsp
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<!--LoginPage.jsp(view of Login Page)-->


<%@ page language="java" 
    contentType="text/html; charset=windows-1256"
    pageEncoding="windows-1256"
%>
<% 
    response.setHeader("Cache-Control","no-store, must-revalidate"); 
    response.setHeader("Pragma","no-cache"); 
    response.setDateHeader ("Expires", -1);
    new java.util.Date();
    %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">

<html>
   <head>
      <meta http-equiv="Content-Type" content="text/html; charset=windows-1256">
      <script>
function clearForms()
{
  var i;
  for (i = 0; (i < document.forms.length); i++) {
    document.forms[i].reset();
  }
}
function validateForm()
{
var x=document.forms["myForm"]["un"].value;
if (x==null || x=="")
  {
  alert("Username must be filled out");
  document.getElementById('un').focus();
  return false;
  }
var y=document.forms["myForm"]["pw"].value;
if (y==null || y=="")
  {
  alert("password must be filled out");
  document.getElementById('pw').focus();
  return false;
  }
}
</script>
     <title>Login Page</title>
   </head>
    <body onLoad="clearForms()" onunload="clearForms()">
    <form action="LoginServlet" onsubmit="return validateForm()" method="post"  name="myForm">
       Please enter your user name       
      <input type="text" name="un" id="un"/><br>      
      Please enter your password
      <input type="text" name="pw" id="pw"/>
      <input type="submit" value="submit">         
      </form>
   </body>
</html>
31 changes: 31 additions & 0 deletions Login/LoginServlet.java.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
//LoginServlet.java(takes input from LoginPage.jsp and controls the flow based on validation with database)


package ExamplePackage;
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 {
private static final long serialVersionUID = 2562294252731783855L;
public void doPost(HttpServletRequest request, HttpServletResponse response) 
throws ServletException, java.io.IOException {
try {
UserBean user = new UserBean();
user.setUserName(request.getParameter("un"));
user.setPassword(request.getParameter("pw"));
user = UserDAO.login(user);
if (user.isValid()) {
HttpSession session = request.getSession(true);
session.setAttribute("currentSessionUser",user);
response.sendRedirect("userLogged.jsp"); //logged-in page
}
else response.sendRedirect("invalidLogin.jsp"); //error page
}
catch (Throwable theException) {
System.out.println(theException);
}
       }
}
33 changes: 33 additions & 0 deletions Login/LogoutServlet.java.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
//LogoutServelt.java(for logout)


package ExamplePackage;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import ExamplePackage.UserBean;

public class LogoutServlet extends HttpServlet {
   private static final long serialVersionUID = 1L;
   public void doGet(HttpServletRequest request, HttpServletResponse response) 
              throws ServletException, java.io.IOException {

   try
   {       

        UserBean user = new UserBean();
        user.removeUsername();
        user.removePassword();
         HttpSession session=request.getSession(false);
         session.removeAttribute("currentSessionUser");
         session.invalidate();
         response.sendRedirect("LoginPage.jsp");
   }      
   catch (Throwable theException)        
   {
        System.out.println(theException); 
   }
  }
}
40 changes: 40 additions & 0 deletions Login/UserBean.java.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
//UserBean.java(Bean class which holds user data in a bean)


package ExamplePackage;
public class UserBean {

    private String username;
    private String password;
    public boolean valid;
public String role;

    public String getUsername() {
       return username;
   }
    public void setUsername(String newUsername) {
       username = newUsername;
   }     

    public String getPassword() {
       return password;
   }
    public void setPassword(String newPassword) {
       password = newPassword;
   }

    public boolean isValid() {
       return valid;
   }
    public void setValid(boolean newValid) {
       valid = newValid;
   }

public String getRole() {
return role;
}
public void setRole(String newRole) {
role = newRole;
}

}
48 changes: 48 additions & 0 deletions Login/ValidLogin.jsp.jsp
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<!--userLogged(welcome page if login success)-->


<%@ page language="java" 
         contentType="text/html; charset=windows-1256"
         pageEncoding="windows-1256"
         import="ExamplePackage.UserBean"
   %>
   <% 
    response.setHeader("Cache-Control","no-store,must-revalidate"); 
    response.setHeader("Pragma","no-cache"); 
    response.setDateHeader ("Expires", -1); 
    new java.util.Date();
    if(session.getAttribute("currentSessionUser")!=null)
    {
    %>

   <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
   "http://www.w3.org/TR/html4/loose.dtd">

   <html>

      <head>
         <meta http-equiv="Content-Type" 
            content="text/html; charset=windows-1256">
         <title>   User Logged Successfully   </title>
         <SCRIPT type="text/javascript">
function noBack() { 
   window.history.forward(); 
   }
</SCRIPT>
      </head>

<BODY onload="noBack();">
<a href="LogoutServlet">Logout</a>
         <center>
            <% UserBean currentUser = (UserBean)(session.getAttribute("currentSessionUser"));%>

            Welcome <%= currentUser.getFirstName() + " " + currentUser.getLastName() %>
         </center>

      </body>

   </html>

<%}
else
response.sendRedirect("LoginPage.jsp");%>
Loading