I was woarking on the assignment and notices everytime I add a video, it only displays the latest one video I added. Previously added video are gone. I think that is the problem with request attribute. Once I send it to a JSP page, it no longer persist across other JSP page that I may browse into. The only solution I can think of is using session and store attribute there. Is that how I should do it?
Am I not suppose to have listvideo.jsp and addvideos.jsp? How can I just have 1 jsp? After listvideo.jsp displays the video I added before whenever I go to addvideos.jsp to add more video the original object is lost.
You should have 2 jsps. However each of those jsps is controlled by the servlet. You should not go directly from one jsp to the other. jsp#1 sends its info to the servlet which forwards that same info to the jsp#2. Likewise, jsp#2 sends its info to the servlet before the servlet returns you to jsp#1. The servlet controls which jsp is shown.
Hmmm, what should I use as the separation technique inside the servlet though? Should I have some kinda of if else statment to separate request coming from one jsp or the other? And what would be the key to use for determining the separation?
Also I just tested out, it doesn't make difference if I go to another jsp or another servlet, the request attribute is lost. Seems like from the JSP page I need to manually forward the request to the destination jsp or servlet. Just by clicking hyperlink, doesn't forward the request.
What is the syntax to forward the request from JSP? [ October 04, 2007: Message edited by: Tony Smith ]
Have you looked at the methods in the ActionServlet class? There is a method there that will send the user to the specified place, and send along the data as well.
Isn't ActionServlet part of the struts framework? I thought for the assignment we suppose to keep it simple and use basic servlet and jsp to solve the problem.
Are you talking about forward() method? That is pretty similar to requestdispatcher which reguar servlet has already.
That does not address the problem that I have. Assuming 1 servlet is taking care requests coming from 2 jsps. How can the servlet tell if it's from first or second jsp?
And I am already using servlet, just in case you were confused on my previous post. The step shoud go like this:
addvideo.jsp ==> servlet ==> displayvideo.jsp (This part works fine) but if I continue to add another video:
notice the first video I added is gone. If I don't use session, then I need to figure out how to forward request between JSPs and keep the request object alive. [ October 04, 2007: Message edited by: Tony Smith ]
We're talking about the abstract init() method in the jr.jar ActionServlet class which you should override (your VideoServlet should extend com.javaranch.common.ActionServlet). You did download jr.jar in the OOP-1 assignment, right? It contains the source code as well as the .class files which you can see if you unjar the jar file.
Originally posted by Tony Smith: And I am already using servlet, just in case you were confused on my previous post. The step shoud go like this:
addvideo.jsp ==> servlet ==> displayvideo.jsp (This part works fine) but if I continue to add another video:
displayvideo.jsp ==> addvideo.jsp ==> servlet ==> displayvideo.jsp ... the first video I added is gone.
The steps should go like this: addvideo.jsp ==> servlet ==> listvideos.jsp listvideos.jsp ==> servlet ==> addvideo.jsp ==> servlet ==> listvideos.jsp
Your jsp sends a parameter to the servlet to let it know which jsp it is. This could be handled by an if statement, but using the init method is simpler and cleaner. [ October 05, 2007: Message edited by: Marilyn de Queiroz ]
Ok, let me ask this one last question before I give it a rest. I did not realize I am suppose to use actionhandler and actionservlet stuff from previous assignment. I guess I didn't quite understand how it work back then, so that's probably the reason I am not thinking of using it. I still have some doubts on addActionHandler method. Let's say you have:
public void init() { addActionHandler( "abc" , new Default()); }
I know I am suppose to pass in parameter to match abc. But is abc only matching parameter key from doget? So I can have anything in parameter value it will be ignored right?