File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes servlets and Jsp's Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "servlets and Jsp Watch "servlets and Jsp New topic
Author

servlets and Jsp's

padmshree Patil
Ranch Hand

Joined: Dec 18, 2000
Posts: 54
Hi,
I have one doubt.(servlet <--->JSP communication)
I have one Test.jsp file for Question and multiple ans
display, login.jsp for login and Display.java for
fetching data from Db and forward data to Test.jsp.
My problem is how to set fields in JSP?
How to send data from servlet to JSP?
I know that with RequestDispatcher it can be done.
RequestDispatcher Rd
=getServletContext()getRequestDispatcher("/Test.jsp");
Rd.forward(req,res);
But how to send the data whice I have fetched from Db.
Please help me out.

thanks ,
padmashree
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12835
    
    5
You can attach an arbitrary object to the request.
The method is in the ServletRequest interface.
setAttribute( "theobj", obj );
On the JSP side you get the object back by
MyObjectClass myObj = (MyObjectClass) request.getAttribute("theobj" );
Bill

------------------
author of:
padmshree Patil
Ranch Hand

Joined: Dec 18, 2000
Posts: 54
Hi,
Thanks. My problem is solved
One more doubt.
In my test.jsp page I have few bottons Back,Next and Finish
When i click on Next it should disply next question.
Can I call jsp method for onclick event?
where i forward it to NextQue servlet it fetches next question

<INPUT TYPE = "BUTTON" NAME ="Back" VALUE ="Back " onclick = 'Backmtd(this)' >
<%!
public void Backmtd()
{ %>
<jsp:forward page="/servlet/NextQue" />

<%! } %>

I tried like this .it's not working.
I am confussed...
please help me out.
thanks,
padmashree
Rajesh Hegde
Ranch Hand

Joined: Sep 15, 2000
Posts: 112
I feel u need to review a bit of Javascript . The backtm() function that u are trying to call is a javascript function. U cannot code it in java though.
<script>
function backtm()
{
// javascript code
}
</script>

<input type=button onClick="backtm()">
I hope this is what u are looking for .
Rajesh
padmshree Patil
Ranch Hand

Joined: Dec 18, 2000
Posts: 54
Thanks,
If I need to do event handling.IS it possible to do in .JSP
I mean when I click next button ,it should disply next question
For that I should have some way to fetch data from database then disply on same page test.jsp
can help me in this.
padamshree
I feel u need to review a bit of Javascript . The backtm() function that u are trying to call is a javascript function. U cannot code it in java though.
<script>
function backtm()
{
// javascript code
}
</script>

<input type=button onClick="backtm()">
I hope this is what u are looking for .
Rajesh[/B]
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
You can have the button submit a form back to the servlet. If you need to, you can use a hidden form field to pass information back to the servlet.
padmshree Patil
Ranch Hand

Joined: Dec 18, 2000
Posts: 54
I want 3 buttons (Back,Next and finish)
Can I have more than 1 submit button?if so then how servlet knows which button I clicked?

Sometimes I feel It could be done more easily by Applet

Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Sure, you can use more than one button. You could give them all the same name and different values like this:

then test the value in the servlet:
padmshree Patil
Ranch Hand

Joined: Dec 18, 2000
Posts: 54
Hi ,
Yes I tried.
out.println(request.getParameter("action"))
prints :Back
out.println("Back".equals(request.getParameter("action")));
it prints :false
so it's not entering if statement.
if ("Back".equals(request.getParameter("action")))
{
out.println("you have clicked Back button");
}

where I am wroung?
thanks
padmashree
Frank Carver
Sheriff

Joined: Jan 07, 1999
Posts: 6920
Try
out.println("'" + request.getParameter("action") + "'");
to see if the parameter value has any leading or trailing spaces.
This is an extremely useful debugging tip. I never log any text strings without quotes these days.


Read about me at frankcarver.me ~ Raspberry Alpha Omega ~ Frank's Punchbarrel Blog
padmshree Patil
Ranch Hand

Joined: Dec 18, 2000
Posts: 54

Thanks.
It solved my problem.It has leading space.
padmashree
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: servlets and Jsp's