mercredi 28 juin 2017

How to get used Bean in a JSP

I am doing a shool project in JSP with JavaBeans. Unfortunately, our teacher is not familiar with that. There is an index.jsp, where all entreis of the Bean "Issues" are shown. This bean contains an ArrayList of Issues. The Bean called Issue contains the data to the Issue, e.g. time and place. A servlet controls an add-Issue-operation, that will create a new Issue and add it to the ArrayList. If you click on on of the listed Issues on the index.jsp, you will be redirceted to a page with details. This one contains the details and a Chat. Therefore, there is a Class/Bean called ChatEntry, that saves the Chat and is part of every Issue.

Here the AddIssueServlet:

package servlets;

import java.io.IOException;
import java.time.LocalDateTime;

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 beans.Issue;
import beans.Issues;

@WebServlet("/addIssue")
public class AddIssueServlet extends HttpServlet {
private static final long serialVersionUID = 1L;

protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    String category = request.getParameter("category");
    String title = request.getParameter("title");
    int maxParticipants = 
    Integer.parseInt(request.getParameter("maxParticipants"));
    int currParticipants = 1;
    String description = request.getParameter("description");
    String place = request.getParameter("place");

    String[] startTime = request.getParameter("startTime").split(":");
    String[] endTime = request.getParameter("endTime").split(":");
    String[] startDate = request.getParameter("startDate").split("-");
    String[] endDate = request.getParameter("endDate").split("-");

    LocalDateTime startDateTime = 
    LocalDateTime.of(Integer.parseInt(startDate[0]), 
    Integer.parseInt(startDate[1]),
            Integer.parseInt(startDate[2]), Integer.parseInt(startTime[0]), 
    Integer.parseInt(startTime[1]));
    LocalDateTime endDateTime = 
    LocalDateTime.of(Integer.parseInt(endDate[0]), 
    Integer.parseInt(endDate[1]),
            Integer.parseInt(endDate[2]), Integer.parseInt(endTime[0]), 
     Integer.parseInt(endTime[1]));

    Issue newIssue = new Issue(category, title, description, 
    maxParticipants, currParticipants, startDateTime,
            endDateTime, place);
    Issues issues = (Issues) getServletContext().getAttribute("issues");
    int id = issues.addIssue(newIssue);

    response.sendRedirect("/lobbyDetail.jsp?id=" + id);
}

}

details.jsp:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@taglib prefix="t" tagdir="/WEB-INF/tags"%>
<%@ taglib uri="http://ift.tt/QfKAz6" prefix="c"%>
<jsp:useBean id="issues" class="beans.Issues" scope="application" />

<!DOCTYPE html>
<script src="../js/scripts.js" type="text/javascript" charset="utf-8"
async defer></script>
<t:layout>
<t:issue issue="${issues.issues[param.id]}"></t:issue>

<div id="chat">
    <form action="lobbyDetail" name="lobbyDetail" method="post">
        <textarea rows="50" cols="50" id="textAreaChatHistory"
            readonly="readonly"> <jsp:getProperty property="getChat"
                name="getChat" /></textarea>
        <input type="text" id="username" name="username"
            placeholder="Ihr Nutzername">
        <textarea rows="2" cols="50">Ihre Nachricht</textarea>
        <input type="submit" value="Absenden">
    </form>
</div>
</t:layout>

ChatServlet:

package servlets;

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 beans.Issue;
import beans.Issues;


@WebServlet("/ChatServerServlet")
public class ChatServerServlet extends HttpServlet
{
private static final long serialVersionUID = 1L;


/**
 * Default constructor.
 */
public ChatServerServlet()
{

}

/**
 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse 
response)
 */
protected void doGet(HttpServletRequest request, HttpServletResponse 
response) throws ServletException, IOException
{
}
HttpServlet#doPost(HttpServletRequest request, HttpServletResponse 
response)

protected void doPost(HttpServletRequest request, HttpServletResponse 
response) throws ServletException, IOException
{
    String username = request.getParameter("username");
    String userMessage = request.getParameter("userMessage");

//        How to get the"right" bean?
//        Bean.addChatEntry(username, userMessage);

}
}

How do i get the Bean that is used in the details-page where the button is clicked, so the Chat will be saved to the Correct Issue-Bean/Object?

I googled, read and watched YouToube for four days and found no solution.

We are only allowed to use JSP and JavaBeans.




Aucun commentaire:

Enregistrer un commentaire