passing a Map from javascript to Java

Hi,
I have the following code in a jsp:
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@taglib uri="http://www.springframework.org/tags" prefix="spring"%>
<%@ taglib uri="/WEB-INF/classes/affiliate.tld" prefix="affiliate" %>
<%@ taglib uri="/WEB-INF/yatraTagLib.tld" prefix="yatra" %>

<c:if test="${(tenantScope eq 'dom') && (pageID eq 'search') && (trackAffiliate == true)}">
	<affiliate:scriptTag affiliateurl='${trackerBaseUrl}/affiliateLogger?version=DFTL&PAGE_TYPE=CONFIRM&PRODUCT_TYPE=AIR&logger=TRUE' paramMap='${affiliationParamMap}' />					      		
</c:if>

Open in new window


Here the affiliationParamMap is being generated in java code and is passed to the jsp in a model.

Affiliate.tld :

<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE taglib PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag 
Library 1.2//EN"
"http://java.sun.com/dtd/web-jsptaglibrary_1_2.dtd">
<taglib>
  <tlib-version>1.0</tlib-version>
  <jsp-version>1.1</jsp-version>
  <short-name>yatra</short-name>
  <tag>
 	 <name>scriptTag</name>
  	 <tag-class>com.yatra.platform.commons.utils.YatraAffiliateHandler</tag-class>
 	 <body-content>empty</body-content>
  	<attribute> 
  	      <name>affiliateurl</name>
  	       <required>true</required>
  	       <rtexprvalue>true</rtexprvalue> 
    </attribute> 
	<attribute> 
  	      <name>paramMap</name>
  	       <required>false</required>
  	       <type>java.util.Map</type>
  	       <rtexprvalue>true</rtexprvalue>
  	 </attribute>
   </tag>
</taglib>

Open in new window


YatraAffiliateHandler.java
package com.yatra.platform.commons.utils;

import java.io.IOException;
import java.util.Map;
import java.util.Map.Entry;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.Tag;

import org.apache.log4j.Logger;

import com.yatra.platform.commons.property.PropertyManager;

public class YatraAffiliateHandler implements Tag{
	
	private static Logger logger = Logger.getLogger(YatraAffiliateHandler.class); 
	
	String affiliateurl;
	Map<String,String> paramMap;
	PageContext pageContext;
	
	public String getAffiliateurl() {
		return affiliateurl;
	}

	public void setAffiliateurl(String affiliateurl) {
		this.affiliateurl = affiliateurl;
	}

	public Map<String, String> getParamMap() {
		return paramMap;
	}

	public void setParamMap(Map<String, String> paramMap) {
		this.paramMap = paramMap;
	}

	@Override
	public int doEndTag() throws JspException {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public int doStartTag() throws JspException {
		 try {
			 String result ="";
			 try{
				 result = YatraUtils.getURLResponse(
						 generateRequestParams(), 
						 affiliateurl, 
						 getConnectionTimeout(),
						 getCookies());
				 logger.debug("Affiliation result : "+ result);
			 }catch (Exception e) {
				logger.error("Unable to get affilation string",e);
			}
			pageContext.getOut().print(result);
	  } catch (IOException ioe) {
		  throw new JspException("Exception caught :", ioe);
	  }
	  return SKIP_BODY;
	}

	@Override
	public Tag getParent() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void release() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void setPageContext(PageContext pageContext) {
		this.pageContext = pageContext;
		
	}

	@Override
	public void setParent(Tag arg0) {
		// TODO Auto-generated method stub
		
	}
	
	private String generateRequestParams(){
		StringBuilder builder = new StringBuilder();
		if(paramMap!=null){
			for(Entry<String,String> entry : paramMap.entrySet()){
				builder.append("&").append(entry.getKey()).append("=").append(entry.getValue());
			}
		}
		return builder.length()>2?builder.substring(1):"";
	}

	private int getConnectionTimeout(){
		PropertyManager propertyManager = (PropertyManager)YatraSpringFactory.getBean("propertyManager");
		return propertyManager.getPropertyAsInt("affiliate.request.time.out");
	}
	public String getCookies() {
		HttpServletRequest request = (HttpServletRequest)pageContext.getRequest();
		String text="";
 	    Cookie[] cookies = request.getCookies();
		  if(cookies != null){
			  for(int i = 0; i < cookies.length;i++){
		      	if(i>0){
		      		text = text + ";";
	 	        }
			  text = text + cookies[i].getName() + "=" + cookies[i].getValue();
			}
		  }
		return text;
	}
}

Open in new window



Now i want to move the generation of affiliationParamMap to javascript.
How do i do it  and make the map work in jsp.

Thanks
Rohit BajajAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Gibu GeorgeChief Technology OfficerCommented:
You want to get the data(Map) into javascript, you cannot use the jsp custom tag as it renders the data to JSP. You can use ajax calls to a servlet which returns the required map from the jsp as json and use it in javascript. If you still want to use the custom tag, and you want the map in javascript you need to modify the custom tag to generate the javascript code as well.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Rohit BajajAuthor Commented:
Hi,
I can use ajax calls to a servlet which returns the map as json. But i need to fire the following url :
http://yjs.yatra.com/flights-aff-tracking/affiliateLogger?version=FLOODLIGHTTAG_PAYMENT&logger=TRUE&FLIGHT_SECTOR=DOMF

In this version, logger, FLIGHT_SECTOR are in affiliation map currently which is getting generated in java code.
Generating this map in java is requiring separate Database hits which is time consuming. We are fetching data using Xpath etc.
But we are already having all the data in Json.

But constructing a map in javascript and using it in jsp i think not possible ?
If i could make map in javascript and pass it to Jsp then my effort will be minimum.
0
Rohit BajajAuthor Commented:
Hi,
Just to clarify my question more. The whole code is written in java server side. I want to move it to client side in Javascript.
I want to generate Map in javascript and fire urls. That what my code does in jsp and java.
But i want to port the whole thing in javascript
0
Slick812Commented:
greetings Robinsuri, , I do not do Java code at this time, but as I remember, the java Map, is just a dynamic container for Key-Values pairs, you can have a container in javascript to hold key-value pairs, as an "js object" with these pairs
    "version=FLOODLIGHTTAG_PAYMENT&logger=TRUE&FLIGHT_SECTOR=DOMF"  -

var paramMap = {version:"FLOODLIGHTTAG_PAYMENT", logger:"TRUE", FLIGHT_SECTOR:"DOMF"}

in your java code there is not much done with your paramMap, you set it with -
     setParamMap(Map<String, String> paramMap) {this.paramMap = paramMap;}

and you make a URL request string with -
generateRequestParams(){ StringBuilder builder = new StringBuilder();
  if(paramMap!=null){
    for(Entry<String,String> entry : paramMap.entrySet()){
      builder.append("&").append(entry.getKey()).append("=").append(entry.getValue());
      }
    }
  return builder.length()>2?builder.substring(1):"";
  }

you can use the object pairs in javascript to make the same  URL request string as the java did, however, as I see it, you problem is going to be writing the of all the "INFORMATION" as data from your java page to the javascript in strings on the web page as javascript variables -

var url1 = {version:"FLOODLIGHTTAG_PAYMENT"; logger:"TRUE"; FLIGHT_SECTOR:"DOMF"}

if you do not have much javascript knowledge or experience, this may not go well for you?
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.