Skip to content

Conversation

@ihsan314
Copy link
Owner

This pull request builds on #7 by adding the usage of HashMaps and JavaScript dictionaries to store username-comment pairs. Further the pull request uses the POST method to update the list of username-comment pairs with textboxes placed inside a form.

@ccondit ccondit changed the base branch from master to get-comments July 11, 2020 01:08

import java.util.ArrayList;
import java.util.HashMap;
// import java.util.ArrayList;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit - can you remove all the commented out code? let's not check in code that unused.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok will do

@WebServlet("/data")
public class DataServlet extends HttpServlet {
ArrayList<String> messages = new ArrayList<>();
HashMap<String, String> messages = new HashMap<String, String>();
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. Per my comment on the parent, can you use the interface here instead of the concrete type (Map instead of HashMap)?
  2. Java can infer the types from the declaration.
  3. Reduce visibility of member variables as much as possible.
Suggested change
HashMap<String, String> messages = new HashMap<String, String>();
private final Map<String, String> messages = new HashMap<>();

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, I will do this as well.

public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
String username = request.getParameter("username");
String message = request.getParameter("comment-or-question");
messages.put(username, message);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no need to take action now, but what if a client sends in a null username? or a null comment?

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I could disable null usernames/comments later otherwise likely no exceptions will be thrown if I do not disable null usernames/comments.

messageContainer.innerText += message + '\n';
for (const username in messages) {
messageContainer.innerText += username + ': ' + messages[username] + '\n\n';
// messageContainer.innerText += username + '\n';
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit - remove this comment.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I shall do so.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks like the commented out code is still here...

@ccondit
Copy link

ccondit commented Jul 11, 2020

Also - a reminder to update your base branch when chaining PRs:
image

@ihsan314 ihsan314 requested a review from ccondit July 11, 2020 01:41
messages.add("It is great for storing data like this");

response.setContentType("application/json;");
String json = new Gson().toJson(messages);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No change needed, just FYI:
Google protocol buffers is a much better alternative to XML and JSON.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok that's useful information for later web development, I will keep this in mind.

for (const message of messages) {
messageContainer.innerText += message + '\n';
for (const username in messages) {
messageContainer.innerText += username + ': ' + messages[username] + '\n\n';
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do the \n\n have the desire effect? Since this is HTML?

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe it did, but I'll render again and check.

@ihsan314 ihsan314 requested a review from kaveh096 July 13, 2020 19:19
Copy link

@ccondit ccondit left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This LGTM one that comment is removed...

messageContainer.innerText += message + '\n';
for (const username in messages) {
messageContainer.innerText += username + ': ' + messages[username] + '\n\n';
// messageContainer.innerText += username + '\n';
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks like the commented out code is still here...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants