-
Notifications
You must be signed in to change notification settings - Fork 0
Implemented the storage of comments as username-comment pairs #8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: get-comments
Are you sure you want to change the base?
Conversation
|
|
||
| import java.util.ArrayList; | ||
| import java.util.HashMap; | ||
| // import java.util.ArrayList; |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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>(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- Per my comment on the parent, can you use the interface here instead of the concrete type (Map instead of HashMap)?
- Java can infer the types from the declaration.
- Reduce visibility of member variables as much as possible.
| HashMap<String, String> messages = new HashMap<String, String>(); | |
| private final Map<String, String> messages = new HashMap<>(); |
There was a problem hiding this comment.
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); |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
portfolio/src/main/webapp/script.js
Outdated
| messageContainer.innerText += message + '\n'; | ||
| for (const username in messages) { | ||
| messageContainer.innerText += username + ': ' + messages[username] + '\n\n'; | ||
| // messageContainer.innerText += username + '\n'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit - remove this comment.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I shall do so.
There was a problem hiding this comment.
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...
portfolio/src/main/java/com/google/sps/servlets/DataServlet.java
Outdated
Show resolved
Hide resolved
portfolio/src/main/java/com/google/sps/servlets/DataServlet.java
Outdated
Show resolved
Hide resolved
| messages.add("It is great for storing data like this"); | ||
|
|
||
| response.setContentType("application/json;"); | ||
| String json = new Gson().toJson(messages); |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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'; |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
portfolio/src/main/java/com/google/sps/servlets/DataServlet.java
Outdated
Show resolved
Hide resolved
ccondit
left a comment
There was a problem hiding this 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...
portfolio/src/main/webapp/script.js
Outdated
| messageContainer.innerText += message + '\n'; | ||
| for (const username in messages) { | ||
| messageContainer.innerText += username + ': ' + messages[username] + '\n\n'; | ||
| // messageContainer.innerText += username + '\n'; |
There was a problem hiding this comment.
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...

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.