Advertisement
Kristian1709

Untitled

Aug 22nd, 2016
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.49 KB | None | 0 0
  1. Kodne til succes.jsp(der hvor der oprettes nye blog posts)
  2.  
  3. <%@page contentType="text/html" pageEncoding="UTF-8"%>
  4. <%@ page import ="java.sql.*" %>
  5. <%@page import="dk.au.hum.imv.persistence.db.*"%>
  6. <%@include file="/Header.jsp" %>
  7. <%@page import="Domain.SessionModel" %>
  8. <%@page import="Domain.BlogPostDAO" %>
  9. <%@page import="Domain.Post" %>
  10. <%@page import="java.util.List" %>
  11. <%@page import="java.sql.*, dk.au.hum.imv.persistence.db.*, java.util.*, Domain.*" %>
  12.  
  13.  
  14.  
  15.  
  16.  
  17. <html>
  18. <head>
  19. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  20. <title>Insert title here</title>
  21. </head>
  22.  
  23. <body>
  24. <h1> Velkommen til #Bloggen </h1>
  25. <h2>Opret ny blogpost</h2>
  26. <a href='logout.jsp'>Log out</a>
  27.  
  28.  
  29.  
  30. <%
  31. //Indsæt post-formular hvis bruger har rettigheder
  32. if (model.isUser()) { %>
  33. <div id="post_form">
  34. <form action="succes.jsp" method="post">
  35. <fieldset>
  36. <legend>Opret Post</legend>
  37. <label for="title">Titel</label> <br /> <input type="text" name="title" /> <br />
  38. <label for="body">Tekst</label>
  39. <textarea class="form-control" rows="5" name="body"></textarea><br />
  40. <input type="submit" name="submit" value="submit" id="submitbtn" />
  41. </fieldset> </form>
  42. </div>
  43.  
  44.  
  45. <hr />
  46.  
  47. <%
  48. //Indsend ny post til databasen
  49. long currentPost = 0;
  50. ArrayList<String> errors = new ArrayList<String>();
  51. String title = request.getParameter("title");
  52. String body = request.getParameter("body");
  53.  
  54. long userID = SessionModel.getUserID();
  55.  
  56. if (request.getParameter("submit") != null){
  57. JDBCConnectionFactory.initManualConnectionHandling("jdbc:mysql://student.hum.au.dk:3306/Kristian1709", "Kristian1709", "3I5lWBpj", "com.mysql.jdbc.Driver");
  58. if (title.equals("")) {
  59. errors.add("Title must be entered - 40 characters max.");
  60. }
  61. if (body.equals("")) {
  62. errors.add("A text-body must be entered."); }
  63. if (!(errors.isEmpty())){
  64. %>
  65.  
  66. <div class="alert alert-danger" role="alert"><p> <%=errors %> </p></div> <%
  67. }
  68. if (errors.isEmpty()){
  69. Post post = new Post(title, body, userID); BlogPostDAO.insertPost(post);
  70. }
  71. }
  72.  
  73.  
  74.  
  75. //Udskriver posts + comments i divs til jsp-siden
  76.  
  77.  
  78. ArrayList<Post> postList = BlogPostDAO.getPosts();
  79. for (Post post : postList){
  80.  
  81. }
  82.  
  83. for (Post post : postList){
  84. //for hver post oprettes en post_div
  85. %>
  86.  
  87. <div class="post_div">
  88. <h2 class="post_title"><%=Post.gettitle() %></h2>
  89. <p class="byline"> Skrevet af <%=Post.getposterID() %> on <%=Post.getpostDate() %> </p>
  90. <p class="post_body"> <%=Post.getbody() %> </p>
  91. <p class="post_id">postID: <%=Post.getpostID() %> </p>
  92.  
  93. </div>
  94.  
  95.  
  96. </body>
  97. </html>
  98.  
  99. Post.java koden
  100.  
  101. package Domain;
  102.  
  103.  
  104. public class Post {
  105.  
  106. private String title;
  107. private String body;
  108. private String postDate;
  109. private long postID;
  110. private long posterID;
  111.  
  112. //public ArrayList<Comment> commentList = new ArrayList <Comment>();
  113.  
  114. public Post (String title, String body){
  115. this.title = title;
  116. this.body=body;
  117.  
  118. }
  119.  
  120. public Post (String title, String body, String postDate, long posterID) {
  121. this.title=title;
  122. this.body=body;
  123. this.postDate=postDate;
  124. this.posterID=posterID;
  125. }
  126. public Post (String title, String body, String postDate, long posterID, long postID) {
  127. this.title=title;
  128. this.body=body;
  129. this.postDate=postDate;
  130. this.posterID=posterID;
  131. this.postID=postID;
  132. }
  133. public Post (String title, String body, long posterID) {
  134. this.title=title;
  135. this.body=body;
  136. this.posterID=posterID;
  137. postDate = "";
  138. postID = 0;
  139.  
  140. }
  141. public String gettitle(){
  142. return title;
  143. }
  144. public String getbody() {
  145. return body;
  146.  
  147. }
  148. public long getposterID() {
  149. return posterID;
  150.  
  151. }
  152. public String getpostDate() {
  153. return postDate;
  154. }
  155. public long getpostID() {
  156. return postID;
  157. }
  158.  
  159.  
  160. }
  161.  
  162.  
  163. BlogPostDAO.java koden
  164.  
  165. package Domain;
  166. import java.sql.Connection;
  167. import java.sql.PreparedStatement;
  168. import java.sql.ResultSet;
  169. import java.sql.SQLException;
  170. import java.sql.Statement;
  171. import java.util.ArrayList;
  172. import dk.au.hum.imv.persistence.db.JDBCConnectionFactory;
  173.  
  174.  
  175. public class BlogPostDAO {
  176. public static ArrayList<Post> getPosts() throws SQLException{
  177. ArrayList<Post> result = new ArrayList<Post>();
  178. Connection con = null;
  179.  
  180. try {
  181. con = JDBCConnectionFactory.getNewConnection();
  182. String sql = "SELECT * FROM post ORDER BY postID DESC";
  183.  
  184. Statement statement = con.createStatement();
  185. ResultSet res = statement.executeQuery(sql);
  186.  
  187. while(res.next()){
  188. String title = res.getString("tittle");
  189. String body = res.getString("body");
  190. String postDate = res.getString("postDate");
  191. int postID = res.getInt("postID");
  192. int posterID = res.getInt("posterID");
  193. result.add(new Post(title, body, postDate, postID, posterID));
  194. }
  195. } catch (SQLException e){
  196. e.printStackTrace();
  197. } finally {
  198. con.close();
  199. }
  200. return result;
  201. }
  202.  
  203. public static void insertPost(Post post) throws SQLException{
  204. Connection con = null;
  205.  
  206. try {
  207. con = JDBCConnectionFactory.getNewConnection();
  208. PreparedStatement statement = con.prepareStatement("INSERT INTO post (title, body, postDate, postID, posterID) Values (?, ?, NOW(), ?)");
  209. statement.setString(1, post.gettitle());
  210. statement.setString(2, post.getbody());
  211. statement.setLong(3, post.getposterID());
  212. statement.executeUpdate();
  213. } catch (SQLException e) {
  214. e.printStackTrace();
  215. } finally {
  216. con.close();
  217. }
  218. }
  219.  
  220. }
  221.  
  222.  
  223.  
  224.  
  225.  
  226. SessionModel.java
  227.  
  228. package Domain;
  229.  
  230. import dk.au.hum.imv.persistence.db.DatabasePersistent;
  231.  
  232. public class SessionModel {
  233.  
  234. private long userId = DatabasePersistent.NOT_IN_DB_ID_VALUE;
  235. private Object userType;
  236.  
  237. public SessionModel() {
  238.  
  239. }
  240.  
  241. public void login(long userId) {
  242. this.userId = userId;
  243. }
  244.  
  245. public void logout() {
  246. this.userId = DatabasePersistent.NOT_IN_DB_ID_VALUE;
  247. }
  248.  
  249. public boolean isLoggedIn() {
  250. return (userId != DatabasePersistent.NOT_IN_DB_ID_VALUE);
  251. }
  252.  
  253. public long getUserId() {
  254. return userId;
  255. }
  256.  
  257. public boolean isUser(){
  258. return isLoggedIn() && this.userType.equals("user");
  259. }
  260. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement