• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Create a very simple web application to display comments

 
neilp pre
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want to create a simple web page using servlet. This was one of my exam questions, I did my practical at exam, but it was not working as they expect, However now I want clarify my question. My question is there is a webpage and it divide into 2 parts. At the bottom part it should display a text box and a text area to enter comments. And also there is a submit button. When submit button clicks that entered comment should display at the top of the same page. To do this I have created a servlet call Welcome.java and divide it into 2 parts using iframes. Then I created a servlet call test.java and create a textbox and textarea in that servlet.Then create another servlet call text22.java to catch the comment and to display them. But it does not give the expected output.
I attach a screenshot of how this page looks now, I want to display the comment at the top of the page. Please help me to solve this problem.I really appreciate your help...

Welcome.java



test.java




test22.java
image.jpg
[Thumbnail for image.jpg]
image
 
J. Kevin Robbins
Bartender
Pie
Posts: 1801
28
Chrome Eclipse IDE Firefox Browser jQuery Linux MySQL Database Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First, welcome to the Ranch!

Now, about that code. I can't see any reason to use an iframe. Second, using PrintWriter to generate html is very bad practice; you should be using a jsp page as a template for your html. But for now, we'll assume that's outside the parameters of the assignment.

So if you are going to use this method to create html, you need to create ALL the html. That means everything from the doctype, head section, body section and everything right down to the last html tag.

One problem that jumps out is that you have form fields but no form defined so nothing is going to be submitted.

Start by posting the resulting html. Do you know how to display the source on a web page?

And you have a choice to make; we can try to fix this code, or you can learn to do it the correct way with a jsp page. Your choice. It sort of depends on where you are in your learning curve.

 
neilp pre
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you help me to solve this problem i really appreciate it. By the way I'm new to servlet, jsp. So wht should I do now
 
J. Kevin Robbins
Bartender
Pie
Posts: 1801
28
Chrome Eclipse IDE Firefox Browser jQuery Linux MySQL Database Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would start by creating the html page the way you want it to look. Don't worry about servlets yet. And don't use an iframe. You can just use a text area for output if you want it in a field element, or even just a span element for the output to be inserted.

But start there. HTML first, then later we'll create a servlet to receive the input and produce the output.

I need to know if this is homework. If it is, post the text of the assignment so we can make sure your code complies with the requirements.

I need to step away for a couple of hours; got real work to do.
 
neilp pre
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes sure, Actually this was my exam question, But I couldn't complete it as they expect, So it gave as my assignment. So in order to gain marks I have to complete this.
The question is creating a simple web application to display comments
Requirements are as follows.
1)The web page should divide into 2 parts as top and bottom. At the bottom part there is a text field for enter user name. Then there should be a text area where user can enter his/her comment .After click on the submit button that entered comment should display at the top of the same page. Then if again user enter comment it shold display at the top of the same page with previous comment.
 
Dave Tolls
Ranch Hand
Posts: 2112
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The first thing that I notice in that is there is nothing that says it expects to talk to a server.
Though that might just be me looking for the minimum amount of effort to comply with the requirements.
 
neilp pre
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yep because I'm really new to this subject. I have tried a lot to solve this from what I know up to now, But it does not work
 
J. Kevin Robbins
Bartender
Pie
Posts: 1801
28
Chrome Eclipse IDE Firefox Browser jQuery Linux MySQL Database Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Since this is an assignment, we aren't going to write the code for you. We're NotACodeMill, we are a learning site, so we're happy to guide you on the path to understanding.

How's that html page looking? Since you need to display previous comments, that makes it a bit tricker. I suggest a text area for the output portion of the page where the comments will be displayed.

I'm going to assume that we need to do this without using JavaScript.

Incidentally, what's the purpose of the username field?
 
neilp pre
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm trying to do this from the begining.So I code an html file to enter username and textarea. User name field is used just for enter a name. Not allow to use javascript to this .


I'm notmention a form action yet
 
J. Kevin Robbins
Bartender
Pie
Posts: 1801
28
Chrome Eclipse IDE Firefox Browser jQuery Linux MySQL Database Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good start. I asked about the name because you don't do anything with it. But it's part of the assignment so we'll ignore that for now.

You are going to need an output area. Perhaps a textarea?

I'm headed to lunch in a few minutes. When I get back I'll write a working app so I can be sure I'm guiding you in the right direction.

btw, what Java version are you using?
 
neilp pre
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes sure, I use net beans 7.3.1, jdk 7
 
J. Kevin Robbins
Bartender
Pie
Posts: 1801
28
Chrome Eclipse IDE Firefox Browser jQuery Linux MySQL Database Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay, now create a servlet. At this time, I don't want it to do anything except read back the comment. Inside the get method, add these lines



Change the action of your form to point to this servlet. Type something in the comment box and click submit.

That should display whatever you put in the comment box into your tomcat log. See if you can get this much working.
 
neilp pre
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I create a servlet call Servlet1. And also change the form action to Servlet1

Servlet1.java

 
J. Kevin Robbins
Bartender
Pie
Posts: 1801
28
Chrome Eclipse IDE Firefox Browser jQuery Linux MySQL Database Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What happens when you run it? Do you see your comment in the output log?
 
J. Kevin Robbins
Bartender
Pie
Posts: 1801
28
Chrome Eclipse IDE Firefox Browser jQuery Linux MySQL Database Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Incidentally, you don't need the PrintWriter. We won't be using that.
 
neilp pre
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It displays the text field and text area, when I enter a comment and click on submit button it displays a blank page. BTW i use glassfish server. the blank page's url is
localhost:8080/TesrRanch/Servlet1?username=neil&comment=Hello++++
 
J. Kevin Robbins
Bartender
Pie
Posts: 1801
28
Chrome Eclipse IDE Firefox Browser jQuery Linux MySQL Database Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The blank page is normal at this point. I want to make sure the comment is being sent to the output log, and I would guess so based on the get parameters.

In Netbeans the log is viewable in the Output window. If you don't see it, click Window - Output. Do you see your comment there among all the server messages?
 
neilp pre
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes. It shows the output
 
J. Kevin Robbins
Bartender
Pie
Posts: 1801
28
Chrome Eclipse IDE Firefox Browser jQuery Linux MySQL Database Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Excellent. We're closing in on it.

You've learned how to send parameters from a page to a servlet. This is already more than you had. Now we need to get the servlet to save that data somewhere that the page can fetch it.

JEE has several scopes that you should read up on, but for now, we're going to save your data in the session scope. This is so the data remains available no matter how many times you load the page (at least until the session ends). This will be needed later.

First you need to get a reference to the session. In your servlet, do this:

I'll let you figure out the rest of that line. <hint: look at the api for Request>

Then you save the comment that you sent to the servlet.

I'll let you figure out what those parameters should be.

Now to get rid of the blank page. For the last two lines in your servlet get() method, add these:


This will send you back to the page after the submit.

Now one more step. We need to make the page display the value that you just saved in the session. Rename your page from .html to .jsp and add this section (make sure the file name is .jsp in that RequestDispatcher code, too)


Now you should be able to enter a comment, click submit, and see your comment displayed.

Pat yourself on the back and have a beer.

But you may have noticed one slight problem. Each time you enter a new comment, the old one is replaced. The assignment said you need to display all previous comments. That means you'll have to fetch the comment string from the session, append the new comment to it, and replace it in the session. Any thoughts on how you might do that?

 
neilp pre
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is how my Form.jsp looks now


this is how Servlet1.java looks


But it says my says this, session.setAttribute("comment",commentText); parameters are wrong, I can't fix it
 
J. Kevin Robbins
Bartender
Pie
Posts: 1801
28
Chrome Eclipse IDE Firefox Browser jQuery Linux MySQL Database Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator



That doesn't give you a reference to the session. Try

HttpSession session = request.getSession();

then set the attribute on the following line.

My day is over here and I'm heading home. I'll try to check in later.
 
neilp pre
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok,
Sorry, i've done a mistake before, i fix it

This is how my Form.jsp looks now


this is how Servlet1.java looks

I entered comment, It displays comment. It works up to now
 
J. Kevin Robbins
Bartender
Pie
Posts: 1801
28
Chrome Eclipse IDE Firefox Browser jQuery Linux MySQL Database Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You've done great so far. Any progress on that last step of displaying all accumulated comments?
 
neilp pre
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have used ServletContext in my program as I uesd in previous test22.java , but it doesn't work.
 
J. Kevin Robbins
Bartender
Pie
Posts: 1801
28
Chrome Eclipse IDE Firefox Browser jQuery Linux MySQL Database Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't see what ServletContext has to do with this. You say it's working in that you can enter a comment and it's displayed. Now you need to create an if-else that will display all comments.

I'll get you started because the first time you run this you will get a null pointer exception if there is no comment attribute in the session.


Puzzle over this for a while and see what you can come up with.
 
neilp pre
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What I have done is this , but it doesn't work

 
J. Kevin Robbins
Bartender
Pie
Posts: 1801
28
Chrome Eclipse IDE Firefox Browser jQuery Linux MySQL Database Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You don't need a try-catch block. Nothing in here is going to throw an exception.

I'll give you another hint


 
neilp pre
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have fix the code as follows

 
J. Kevin Robbins
Bartender
Pie
Posts: 1801
28
Chrome Eclipse IDE Firefox Browser jQuery Linux MySQL Database Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are very close. But how does it look? That's just going to retrieve and resend the same comment over and over.

You need to concatenate the old comment with the new. Do you know how to concatenate Strings?
 
neilp pre
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In java i have used string concatenation. And also when I deploy and run my project it does not display the comment,
 
J. Kevin Robbins
Bartender
Pie
Posts: 1801
28
Chrome Eclipse IDE Firefox Browser jQuery Linux MySQL Database Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What has changed since the last time it worked?

Post your current servlet code and the jsp page.

One thing I notice is that you are not specifying a path to the jsp page in your RequestDispatcher code. That can cause errors.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic