cancel
Showing results for 
Search instead for 
Did you mean: 

return parameters from servlet

Former Member
0 Kudos

hi

Please tell me how to return 1 or more parameters from servlet to jsp page.

Accepted Solutions (0)

Answers (1)

Answers (1)

former_member698570
Active Participant
0 Kudos

Hi,

you can either use a Session BEAN to store all information that needs to be exchanged in your application or you just use your Session to store information.

Create a new Session Parameter in your servlet and you will then be able to access this Parameter from your JSP since it is stored in your Session

I like the BEAN approach if you are dealing with lots of parameters. If it is just a couple the other approach is easier and faster to implement

Here's a description on howto implement the BEAN and use it in your Servlet and JSP:

package com.xyz....

import whateveryouneed

public class Bean {
 	       
 	        //      Your parameter
                private String YOUR_PARAM = "";

                public Bean() {
 	                super();
 	        }
 	       
 	        public void setYOUR_PARAM(String value) {
 	                YOUR_PARAM = value;
 	        }
 	       
 	        public String getYOUR_PARAM() {
 	                return YOUR_PARAM;
 	        }
}

Then in your Servlet use the following:

Define this at the top of your Servlet Class

	        /* The name of your  bean as expected from JSP. */
 	        public static final String BEAN_NAME = "MyBean";

A Method to get the Bean from the session or create it if it does not yet exist:

(The following Code is intended to be used in a Abstract Portal component. So if you need

in in a different context you can still implement it the way described here but the way to access

the actual Session might differ => e.g. request.getSession() instead of request.getComponentSession() )

/**
* Get the bean from component session - create one if neccessary.
* @param request The request.
* @return A bean instance.
*/
private final Bean getBean(IPortalComponentRequest request) {
               IPortalComponentSession session = request.getComponentSession();
               if (session.getValue(BEAN_NAME) == null) {
               // nothing found, thus create a new one
               Bean bean = new Bean();
               // store the bean in the components session (hopefully HTTP session)
               logger.severe("No action defined yet, initialize Session Bean!");
               session.putValue(BEAN_NAME, bean);
               // If bean not yet initialized bind the values to the bean
               bindValues(request);
             }
             return (Bean) session.getValue(BEAN_NAME);
}

You also need a method to bind the values of the bean. (If you want you can also update the session

parameters stored in the bean in every Server Round Trip)

private final void bindValues(IPortalComponentRequest request) {

   Bean bean = getBean(request);
   
   bean.setYOUR_PARAM("yourValue")
	               
}

Now you can use this BEAN in your JSP also and access your Parameters using the getter Methods defined in your BEAN.

At the top of your JSP after your imports place the following:

<jsp:useBean id="MyBean" scope="session" class="com.xyz.....Bean"/>

the id is the Name as defined in your Servlet and the classname is the full namespace of your Bean Class (Package.Classname)

You can now access the Bean using

<P><span class="myCssClass"><%= PGBean.getYOUR_PARAM() %></span></P>

The easy approach is much shorter and looks like this:

In your Servlet use:

// Save Your Parameter in Session to access from JSP
String YOUR_PARAM="TEST"
request.getComponentSession().putValue("YOURPARAM",YOUR_PARAM);

In your JSP then use:

String yourParamFromServlet = (String) componentRequest.getComponentSession().getValue("YOURPARAM");

<P><span class="myCssClass"><%= yourParamFromServlet  %></span></P>

Hope this helps (Please reward points if helpful)

Cheers

Former Member
0 Kudos

Hi Marcel thanks for help. I am using ajax in netweaver and so I am using servlets.

I want to know what is applicationutils class and where we get it.

Please help me.

former_member698570
Active Participant
0 Kudos

Hi,

I really don't know what you are trying to achieve. Please describe your scenario in more detail.

- What have you already done

- Where are you stuck

Cheers

Former Member
0 Kudos

Hi marcel;

I will expain to you the entire scenario.See I am displaying an image in masthead for which I downloaded the par file for masthead.

I did the necessary changes in the jsp page.I am fetching the url for image from r/3.The code for fetching url from r/3 is written in a abstractlistcomponent class. This image is actually stored on the content server.

Now after I deployed the application I observed that the image is not displayed the first time page is loaded.

However when the page is refreshed the image is displayed .

So I had an option to refresh the page through coding.

But I don't want to refresh entire page .Instead I would use ajax to refresh only the image.Please guide me for the same that what would be the detailed steps to achieve this.

Please guide me.