Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Kodne til succes.jsp(der hvor der oprettes nye blog posts)
- <%@page contentType="text/html" pageEncoding="UTF-8"%>
- <%@ page import ="java.sql.*" %>
- <%@page import="dk.au.hum.imv.persistence.db.*"%>
- <%@include file="/Header.jsp" %>
- <%@page import="Domain.SessionModel" %>
- <%@page import="Domain.BlogPostDAO" %>
- <%@page import="Domain.Post" %>
- <%@page import="java.util.List" %>
- <%@page import="java.sql.*, dk.au.hum.imv.persistence.db.*, java.util.*, Domain.*" %>
- <html>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
- <title>Insert title here</title>
- </head>
- <body>
- <h1> Velkommen til #Bloggen </h1>
- <h2>Opret ny blogpost</h2>
- <a href='logout.jsp'>Log out</a>
- <%
- //Indsæt post-formular hvis bruger har rettigheder
- if (model.isUser()) { %>
- <div id="post_form">
- <form action="succes.jsp" method="post">
- <fieldset>
- <legend>Opret Post</legend>
- <label for="title">Titel</label> <br /> <input type="text" name="title" /> <br />
- <label for="body">Tekst</label>
- <textarea class="form-control" rows="5" name="body"></textarea><br />
- <input type="submit" name="submit" value="submit" id="submitbtn" />
- </fieldset> </form>
- </div>
- <hr />
- <%
- //Indsend ny post til databasen
- long currentPost = 0;
- ArrayList<String> errors = new ArrayList<String>();
- String title = request.getParameter("title");
- String body = request.getParameter("body");
- long userID = SessionModel.getUserID();
- if (request.getParameter("submit") != null){
- JDBCConnectionFactory.initManualConnectionHandling("jdbc:mysql://student.hum.au.dk:3306/Kristian1709", "Kristian1709", "3I5lWBpj", "com.mysql.jdbc.Driver");
- if (title.equals("")) {
- errors.add("Title must be entered - 40 characters max.");
- }
- if (body.equals("")) {
- errors.add("A text-body must be entered."); }
- if (!(errors.isEmpty())){
- %>
- <div class="alert alert-danger" role="alert"><p> <%=errors %> </p></div> <%
- }
- if (errors.isEmpty()){
- Post post = new Post(title, body, userID); BlogPostDAO.insertPost(post);
- }
- }
- //Udskriver posts + comments i divs til jsp-siden
- ArrayList<Post> postList = BlogPostDAO.getPosts();
- for (Post post : postList){
- }
- for (Post post : postList){
- //for hver post oprettes en post_div
- %>
- <div class="post_div">
- <h2 class="post_title"><%=Post.gettitle() %></h2>
- <p class="byline"> Skrevet af <%=Post.getposterID() %> on <%=Post.getpostDate() %> </p>
- <p class="post_body"> <%=Post.getbody() %> </p>
- <p class="post_id">postID: <%=Post.getpostID() %> </p>
- </div>
- </body>
- </html>
- Post.java koden
- package Domain;
- public class Post {
- private String title;
- private String body;
- private String postDate;
- private long postID;
- private long posterID;
- //public ArrayList<Comment> commentList = new ArrayList <Comment>();
- public Post (String title, String body){
- this.title = title;
- this.body=body;
- }
- public Post (String title, String body, String postDate, long posterID) {
- this.title=title;
- this.body=body;
- this.postDate=postDate;
- this.posterID=posterID;
- }
- public Post (String title, String body, String postDate, long posterID, long postID) {
- this.title=title;
- this.body=body;
- this.postDate=postDate;
- this.posterID=posterID;
- this.postID=postID;
- }
- public Post (String title, String body, long posterID) {
- this.title=title;
- this.body=body;
- this.posterID=posterID;
- postDate = "";
- postID = 0;
- }
- public String gettitle(){
- return title;
- }
- public String getbody() {
- return body;
- }
- public long getposterID() {
- return posterID;
- }
- public String getpostDate() {
- return postDate;
- }
- public long getpostID() {
- return postID;
- }
- }
- BlogPostDAO.java koden
- package Domain;
- import java.sql.Connection;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.sql.Statement;
- import java.util.ArrayList;
- import dk.au.hum.imv.persistence.db.JDBCConnectionFactory;
- public class BlogPostDAO {
- public static ArrayList<Post> getPosts() throws SQLException{
- ArrayList<Post> result = new ArrayList<Post>();
- Connection con = null;
- try {
- con = JDBCConnectionFactory.getNewConnection();
- String sql = "SELECT * FROM post ORDER BY postID DESC";
- Statement statement = con.createStatement();
- ResultSet res = statement.executeQuery(sql);
- while(res.next()){
- String title = res.getString("tittle");
- String body = res.getString("body");
- String postDate = res.getString("postDate");
- int postID = res.getInt("postID");
- int posterID = res.getInt("posterID");
- result.add(new Post(title, body, postDate, postID, posterID));
- }
- } catch (SQLException e){
- e.printStackTrace();
- } finally {
- con.close();
- }
- return result;
- }
- public static void insertPost(Post post) throws SQLException{
- Connection con = null;
- try {
- con = JDBCConnectionFactory.getNewConnection();
- PreparedStatement statement = con.prepareStatement("INSERT INTO post (title, body, postDate, postID, posterID) Values (?, ?, NOW(), ?)");
- statement.setString(1, post.gettitle());
- statement.setString(2, post.getbody());
- statement.setLong(3, post.getposterID());
- statement.executeUpdate();
- } catch (SQLException e) {
- e.printStackTrace();
- } finally {
- con.close();
- }
- }
- }
- SessionModel.java
- package Domain;
- import dk.au.hum.imv.persistence.db.DatabasePersistent;
- public class SessionModel {
- private long userId = DatabasePersistent.NOT_IN_DB_ID_VALUE;
- private Object userType;
- public SessionModel() {
- }
- public void login(long userId) {
- this.userId = userId;
- }
- public void logout() {
- this.userId = DatabasePersistent.NOT_IN_DB_ID_VALUE;
- }
- public boolean isLoggedIn() {
- return (userId != DatabasePersistent.NOT_IN_DB_ID_VALUE);
- }
- public long getUserId() {
- return userId;
- }
- public boolean isUser(){
- return isLoggedIn() && this.userType.equals("user");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement