• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

servlets and Jsp's

 
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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]
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Sheriff
Posts: 7001
6
Eclipse IDE Python C++ Debian Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
padmshree Patil
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Thanks.
It solved my problem.It has leading space.
padmashree
 
reply
    Bookmark Topic Watch Topic
  • New Topic