Я также попытался сделать getForum()
, чтобы взять строку в качестве аргумента и Я чувствую, что единственная проблема, с которой сталкивается программа, - это разбор целых чисел, но теперь я понимаю, что это в основном из-за того, что указатель нулевой, что сбивает с толку, поскольку doGet()
может получить доступ к этому параметру. * Я открыт для всех предложений и / или решений, большое спасибо!
Сервер: Tom Cat v8.5
IDE: Eclipse Версия: 2019-12 (4.14. 0) Идентификатор сборки: 20191212-1212
Java Версия: 13
DisplayForum:
package web.WebForum;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class DisplayForum
*/
@WebServlet("/DisplayForum")
public class DisplayForum extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public DisplayForum() {
super();
// TODO Auto-generated constructor stub
}
public void init(ServletConfig config) throws ServletException {
super.init(config);
Topic topic = new Topic("Eclipse Problem","Karla Valencia");
List<Forum> forums = new ArrayList<Forum>();
forums.add(new Forum("General Discussion",topic));
forums.add(new Forum("CS3220 Web Programming"));
getServletContext().setAttribute("forums", forums);
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
@SuppressWarnings("unchecked")
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
//response.getWriter().append("Served at: ").append(request.getContextPath());
List<Forum> forums = (List<Forum>) getServletContext().getAttribute("forums");
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<html><head><title>Forums</title></head><body>");
out.println("<table border = '1'>");
out.println("<tr><th>Forum</th><th>Topics</th></tr>");
for(Forum forum : forums) {
out.println("<tr><td><a href='DisplayTopics?id=" + forum.getForumId()+"'>" + forum.getForumName() + "</a></td><td style = 'text-align: center'>" + forum.getNumberOfTopics() + "</td><td>" + forum.getForumId() +"</td></tr>");
}
out.println("</table>");
out.println("</body></html>");
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
DisplayTopics:
package web.WebForum;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class DisplayTopics
*/
@WebServlet("/DisplayTopics")
public class DisplayTopics extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public DisplayTopics() {
super();
// TODO Auto-generated constructor stub
}
public Forum getForum (int id) {
@SuppressWarnings("unchecked")
List<Forum> forums = (List<Forum>) getServletContext().getAttribute("forums");
for(Forum forum: forums) {
if(forum.getForumId() == id) {
return forum;
}
}
return null;
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
//response.getWriter().append("Served at: ").append(request.getContextPath());
int id = Integer.valueOf(request.getParameter("id"));
Forum forum = getForum(id);
List<Topic> topics = forum.getTopics();
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<html><head><title>Forums</title></head><body>");
out.println("<h2>"+ forum.forumName +"</h2><table border = '1' style = 'text-align: center'>");
out.println("<tr><th>Topic</th><th>Author</th><th>Replies</th><th>Last Post</th></tr>");
for(Topic topic : topics) {
out.println("<tr><td><a href='#'>" + topic.getTopicName() + "</a></td><td style = 'text-align: center'>" + topic.getAuthor() + "</td><td>" + topic.getNumComments() +"</td><td>"+ topic.getLastPost() +"</td></tr>");
}
out.println("</table>");
out.println("</br><a href='CreateTopic?id="+ forum.getForumId() +"' > Create Topic</a>");
out.println("</body></html>");
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
Создать топи c Сервлет: (откуда, я думаю, проблема возникает)
package web.WebForum;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet("/CreateTopic")
public class CreateTopic extends HttpServlet {
private static final long serialVersionUID = 1L;
public CreateTopic() {
super();
// TODO Auto-generated constructor stub
}
public Forum getForum (int id) {
@SuppressWarnings("unchecked")
List<Forum> forums = (List<Forum>) getServletContext().getAttribute("forums");
for(Forum forum: forums) {
if(forum.getForumId() == id) {
return forum;
}
}
return null;
}
public Forum getForum(String id) {
@SuppressWarnings("unchecked")
List<Forum> forums = (List<Forum>) getServletContext().getAttribute("forums");
for(Forum forum: forums) {
if(String.valueOf(forum.getForumId()) == id) {
return forum;
}
}
return null;
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
Integer id = Integer.valueOf(request.getParameter("id"));
Forum forum = getForum(id);
request.setAttribute("forumName", forum.getForumId());
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<html><head><title>Create Topic</title></head><body>");
out.println("<h2><a href='DisplayTopics?id=" + forum.getForumId()+"'>" +forum.forumName +"</a> > Create Topic</h2>");
out.println("<form action='CreateTopic' method='post'>");
out.println("<table border = '1'><tr><th>Your Name: </th><td><input type ='text' name='author'></td></tr>");
out.println("<tr><th>Subject: </th><td><input type ='text' name='name'></td></tr>");
out.println("<tr><th>Content</th><td><textarea name='comment'cols='40' rows='4'></textarea></td></tr>");
out.println("<tr><td col='4' row ='1'><input type='submit' name='add' value='Add'/></td></tr>");
out.println("</table></form></body></html>");
out.println(id);
}
@SuppressWarnings("unchecked")
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
String author = request.getParameter("author");
String name = request.getParameter("name");
String comment = request.getParameter("comment");
List<Forum> forums = (List<Forum>) getServletContext().getAttribute("forums");
Topic topic = new Topic(author,name,comment);
response.sendRedirect("DisplayTopics?id=#");
}
}
Форумный класс:
package web.WebForum;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
public class Forum {
static AtomicInteger count = new AtomicInteger(0);
String forumName;
int numberOfTopics;
private final int forumId;
List<Topic> topics = new ArrayList<Topic>();
public Forum(String forumName) {
this.forumName = forumName;
this.numberOfTopics = 0;
this.forumId = count.incrementAndGet();
}
public Forum(String forumName, Topic topic) {
this.forumName = forumName;
this.numberOfTopics = 0;
this.forumId = count.incrementAndGet();
addTopic(topic);
}
public Forum(String forumName, int numberOfTopics) {
this.forumName = forumName;
this.numberOfTopics = numberOfTopics;
this.forumId = count.incrementAndGet();
}
public void addTopic(Topic topic){
this.topics.add(topic);
this.numberOfTopics++;
}
public String getForumName() {
return forumName;
}
public void setForumName(String forumName) {
this.forumName = forumName;
}
public int getNumberOfTopics() {
return numberOfTopics;
}
public void setNumberOfTopics(int numberOfTopics) {
this.numberOfTopics = numberOfTopics;
}
public int getForumId() {
return forumId;
}
public List<Topic> getTopics() {
return topics;
}
public void setTopics(List<Topic> topics) {
this.topics = topics;
}
}
Топи c класс:
package web.WebForum;
import java.util.ArrayList;
import java.util.List;
public class Topic {
String topicName;
String author;
int numComments;
String lastPost = "no posts";
String comment;
List<String> comments = new ArrayList<String>();
public Topic ( String topicName, String author,String comment) {
this.topicName = topicName;
this.author = author;
this.comment = comment;
}
public Topic ( String topicName, String author) {
this.topicName = topicName;
this.author = author;
}
public void addComment(String comment) {
comments.add(comment);
//this.lastPost = comment.postedOn;
this.numComments++;
}
public String getTopicName() {
return topicName;
}
public void setTopicName(String topicName) {
this.topicName = topicName;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public int getNumComments() {
return numComments;
}
public void setNumComments(int numComments) {
this.numComments = numComments;
}
public String getLastPost() {
return lastPost;
}
public void setLastPost(String lastPost) {
this.lastPost = lastPost;
}
public List<String> getComments() {
return (List<String>) comments;
}
public void setComments(List<String> comments) {
this.comments = (List<String>) comments;
}
}