Karla Valencia
Karla Valencia

Reputation: 49

doPost() is not seeing a Parameter

I am sure that a similar question has probably been asked already and I have been working on this for hours to the point where I'm like... this is getting ridiculous.

Seeing that I suddenly have plenty of time and I am assuming a lot of you do too, I figured let me ask StackOverflow to see if they could help. Also I apologize in advance for all the detail in this question, I just want to give you all the information that I have gathered from these frustrating few hours.

Okay so I am trying to build a very basic web forum using Java Servlets in eclipse. This was a homework assignment that was due about a week ago, I already submitted it not working at 100% but I revisited it now trying to figure out where I went wrong. I am sure you are thinking, "But Karla, there is a much easier way to do this". I am sure there is but this was a project on MVC before the introduction of JSP so it had to be done this way.

My problem: It seems that the problem lies in the servlet CreateTopic in the doPost(), it cannot pull the parameter 'id' so that it can redirect to Display Topics. I will post all servlets that I have at the moment. So the short answer of what it is supposed to do:

  1. Shows all forums
  2. If you click on a forum name, you get a list of topics under that forum
  3. If you click create topic, you should be able to add to the topic list under that forum one you hit add, it all goes down hill.

This is what I know:

  1. The parameter is a string and I have tried to pull it using Integer.valueOf(…) and Integer.parseInt(…) and it doesn't work.
  2. The error I get is a null pointer, I have tested it by manually inserting an id number and it redirects just fine so I figured that there is an issue with doPost() where it cannot seem to pull the parameter ID..
  3. I know that doGet() is able to pull this parameter as it can pull the name of the object and the counter of the amount of topics listed under the forum through the getter and setters of the class Forum.
  4. I have also tried making the getForum() to take a string as an argument and I feel that the only issue that the program is having is through the parsing of integer but I realize now that it is mostly due to the pointer being null which is confusing because doGet() is able to access this parameter.

I am open to all suggestions and or solutions, thank you so very much!

Server: Tom Cat v8.5

IDE: Eclipse Version: 2019-12 (4.14.0) Build id: 20191212-1212

Java Version: 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);
    }

}

Create Topic Servlet: (where I think issue is stemming from)

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=#");

    }

}

Forum Class:


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;
    }


}

Topic class:

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;
    }
}

Upvotes: 4

Views: 556

Answers (0)

Related Questions