Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

call a servlet from a jsp????

 
verduka fox
Ranch Hand
Posts: 178
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am developing a website using jsp's. I had planned to use jsp's for every page, but I need to use a graphing product (EasyCharts at www.objectplanet.com) that requires the graph to be built in a servlet NOT a jsp. I am saving the values to be graphed in various java beans. I know this is probably an elementary question, but can someone please tell me how to call the servlet from my jsp, how to pass the values from the bean to the servlet, and how to get from the servlet to a jsp? Can one jsp call the servlet and then be called when the servlet is finished, or does this require the use of at least two servlets? Any suggestions and/or examples is greatly appreciated!!
 
Shuaib Gill
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To call a servlet from a JSP, try...
<jsp: forward page="../servlets/YourServlet" />
Then to set some values to a bean, in YourServlet do this.
...
SomeBean obj = new SomeBean();
obj.setValues("yourvalues");
// forward to a jsp
RequestDispatcher newJSP = getServletContext().getRequestDispatcher("/xxx/yyy/next.jsp");
newJSP.forward(req,res);
Hope this helps
 
Andrey Opanasets
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
Use send HttpServletResponse.sendRedirect() or RequestDispatcher.forward() methods. See API for more complete information.
 
verduka fox
Ranch Hand
Posts: 178
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the replies. Just a few more questions:
How do I set values in the bean from the jsp so that they are in the bean when I am in the servlet? See, I start with a jsp; I need to set values from this jsp and then go to the servlet. I thought that the form had to be submitted in the jsp to get the values set in the bean. Any ideas?
 
Paramagurusamy Balasubramanian
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi fox,
U need to do the following steps
<jsp:useBean id="mybean" scope="session" class="storeBean"/>
.....
.....
<%
int yourvalue=10;
mybean.storeValue(yourvalue);
out.println("The value in the bean is "+storeBean.value);
// or u can use
out.println("The value in the bean is "+mybean.retrieveValue());
%>
storeBean.java
--------------
public class storeBean
{
public int value;
public storeBean()
{}
public void storeValue(int value)
{
this.value=value;
}
public int retrieveValue()
{
return this.value
}
}

I hope i fulfilled your request..
Best Regards,
Paramaguru.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic