Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

servlets and Jsp's

 
padmshree Patil
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • 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
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13071
6
  • Mark post as helpful
  • send pies
  • 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
  • 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
 
Rajesh Hegde
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
  • 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
  • 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]
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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
  • 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
 
Frank Carver
Sheriff
Posts: 6920
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator

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