3

Below is my java code:

package employees;  
public class showString{    
    public String setSection(){
        String myStr = "Hello";
        return myStr ;
    }
};

How do i call setSection() method in my jsp page using JSTL? I've tried several methods but none of them worked.

I've already checked this page How to avoid Java Code in JSP-Files? but don't understand how to call my method on the jsp file

This will be a great help. Thanks

Community
  • 1
  • 1
Chamara Keragala
  • 5,627
  • 10
  • 40
  • 58
  • look here http://stackoverflow.com/questions/1019618/escape-apostrophe-as-with-cout-jsp#answer-1019875. It will help you – maks Feb 28 '13 at 08:59

2 Answers2

7

You can try <jsp:usebean> to call the method of the java bean.. Check the example below

package my;
public class MyBean {

  private String name=new String();

  public String getName() {
  return name;
  }
  public void setName(String name) {
  this.name = name;
  }
  } 

To call the setname method in jsp

<jsp:useBean id="mybean" class="my.MyBean" scope="session" >
<jsp:setProperty name="mybean" property="name" value=" Hello world" />
</jsp:useBean>

To call the getname method in jsp

<jsp:getProperty name="mybean" property="name" />

The main requirement is your method name should be start from get and set appended by property name

MayurB
  • 3,609
  • 2
  • 21
  • 36
  • Thanks for the reply. Can you please explain what each line of code means on **To call the setname method in jsp** and **To call the getname method in jsp**? will be a great help for me as well as others :) – Chamara Keragala Feb 28 '13 at 09:36
  • 1
    The is used to locate or instantiates a java bean component The element contains a element that is used to sets property values in the Bean The element that is used to get the value of property. – MayurB Feb 28 '13 at 09:40
  • http://profesores.elo.utfsm.cl/~agv/elo330/2s03/projects/Tomcat/doc/1.2/syntaxref1217.html This link will help you to understand more – MayurB Feb 28 '13 at 09:43
0

showString is not a method but a class. You cannot "call" classes. If you want to call setSection method, then you can try ${objectYouCreated.setSection()}

Also note you code does not follow case conventions in Java (The name of the class should start with an uppercase letter), and I'm not 100% sure if that semicolon at the end is valid Java syntax but looks really strange to me.

orique
  • 1,295
  • 1
  • 27
  • 36
  • im sorry.. I have mistakenly added showString() as method instead of setString() in the description. i've tried to call `setSection` method but it didnt work – Chamara Keragala Feb 28 '13 at 09:13
  • is it ok to create a instance of `showString()` in the JSP file? the java code should be seperated from jsp code right? – Chamara Keragala Feb 28 '13 at 09:17
  • You need to create an instance of `showString` somewhere and make it available to the JSP in some way. If you use Struts2 you could create the instance anywhere in your Java code and then expose it to the JSP via an Action class field. – orique Feb 28 '13 at 09:23