我正在使用JavaBeans在JSP中做一个shool项目。不幸的是,我们的老师并不熟悉。有一个index.jsp,其中包含Bean的所有entre#34;问题"显示。这个bean包含一个ArrayList of Issues。名为Issue的Bean包含问题的数据,例如时间和地点。 servlet控制add-Issue-operation,它将创建一个新的Issue并将其添加到ArrayList。如果单击index.jsp上列出的问题,您将被重新扫描到包含详细信息的页面。这个包含详细信息和聊天。因此,有一个名为ChatEntry的类/ Bean,可以保存聊天,并且是每个问题的一部分。
这里是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://java.sun.com/jsp/jstl/core" 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);
}
}
如何获取单击按钮的详细信息页面中使用的Bean,以便将聊天保存到正确的问题-Bean / Object?
我用Google搜索,阅读并观看YouToube四天,但没有找到解决方案。
我们只允许使用JSP和JavaBeans。