tags:

views:

3128

answers:

3

Hi All : I need to call a method of java class from jsp page.. While calling that in jsp page, need to set some request parameters also. How do i do that in jsp page?

Ex :

Java class :

 public void execute() {
     string msg = request.getParameter("text");
 }

JSP file :

 I need to call the method here and also need to set parameter values (eg : &text=hello)

Please help me...

+1  A: 

Just embed your Java code inside the JSP. The object "request" is available to get parameters (parameters can't be set). Unless you plan to forward to another JSP in another context with an attribute should be enough.

To refer to your own class do not forget to add the import statement at the beginning of the JSP (similiar to an import in a Java class).

<!-- JSP starts here -->
<%@page import="test.MyClass" %>

<%
MyClass myClass = new MyClass();
String text=myClass.execute();

// This is not neccessary for this JSP since text variable is already available locally.
// Use it to forward to another page.
request.setAttribute("text");
%>

<p>
This is the <%=text%>
</p>
Fernando Miguélez
A: 

You can't set request parameters - they're meant to have come from the client.

If you need to give the method some information, you should either use normal method parameters, or some other state. The request parameters are not appropriate for this, IMO.

Jon Skeet
A: 

You cannot set request parameters. I suggest you try using the attributes as suggested in the previous answers.

Vinnie