Rohit Bajaj
asked on
passing a Map from javascript to Java
Hi,
I have the following code in a jsp:
Here the affiliationParamMap is being generated in java code and is passed to the jsp in a model.
Affiliate.tld :
YatraAffiliateHandler.java
Now i want to move the generation of affiliationParamMap to javascript.
How do i do it and make the map work in jsp.
Thanks
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>
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>
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;
}
}
Now i want to move the generation of affiliationParamMap to javascript.
How do i do it and make the map work in jsp.
Thanks
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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
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
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_PAY MENT&logge r=TRUE&FLI GHT_SECTOR =DOMF" -
var paramMap = {version:"FLOODLIGHTTAG_PA YMENT", 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.get Key()).app end("=").a ppend(entr y.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_PA YMENT"; logger:"TRUE"; FLIGHT_SECTOR:"DOMF"}
if you do not have much javascript knowledge or experience, this may not go well for you?
"version=FLOODLIGHTTAG_PAY
var paramMap = {version:"FLOODLIGHTTAG_PA
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
}
}
return builder.length()>2?builder
}
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_PA
if you do not have much javascript knowledge or experience, this may not go well for you?
ASKER
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.