我敢肯定,可能已经有人问过类似的问题,而且我已经为此工作了几个小时,直到我想……这越来越荒谬了。
看到我突然有了足够的时间,而且我假设你们中的很多人也有,我想我可以问问 StackOverflow 看看他们是否可以提供帮助。另外,对于这个问题中的所有细节,我提前道歉,我只想向您提供我从这令人沮丧的几个小时中收集到的所有信息。
好的,所以我正在尝试在 Eclipse 中使用 Java Servlet 构建一个非常基本的 Web 论坛。这是一个大约一周前到期的家庭作业,我已经提交了它,但没有 100% 工作,但我现在重新审视它,试图找出我哪里出错了。我相信你在想,“但是 Karla,有一个更简单的方法可以做到这一点”。我确信有,但这是在引入 JSP 之前关于 MVC 的一个项目,所以它必须以这种方式完成。
我的问题:似乎问题出在 servlet CreateTopic 中doPost()
,它无法提取参数 'id' 以便它可以重定向到显示主题。我将发布我目前拥有的所有 servlet。所以它应该做什么的简短回答:
- 显示所有论坛
- 如果您单击论坛名称,您将获得该论坛下的主题列表
- 如果您单击创建主题,您应该能够添加到您点击添加的那个论坛下的主题列表中,这一切都会下降。
这是我所知道的:
- 该参数是一个字符串,我尝试使用它来拉它
Integer.valueOf(…)
并且Integer.parseInt(…)
它不起作用。 - 我得到的错误是一个
null
指针,我已经通过手动插入一个 ID 号对其进行了测试,它重定向得很好,所以我认为doPost()
它似乎无法提取参数 ID 存在问题。 - 我知道
doGet()
能够拉出这个参数,因为它可以通过类 Forum 的 getter 和 setter 拉取对象的名称和论坛下列出的主题数量的计数器。 - 我也尝试过将
getForum()
字符串作为参数,我觉得程序唯一的问题是通过整数解析,但我现在意识到这主要是由于指针为空,这令人困惑,因为doGet()
是可以访问此参数。
我对所有建议和/或解决方案持开放态度,非常感谢!
服务器:汤姆猫 v8.5
IDE:Eclipse 版本:2019-12 (4.14.0) 构建 ID:20191212-1212
Java 版本:13
显示论坛:
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);
}
}
显示主题:
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);
}
}
创建主题 Servlet:(我认为问题出在哪里)
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;
}
}
话题类:
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;
}
}