send parameter from request through json - java

How can I resend parameter I received from servlet to servlet using json.
Here's what I mean, I am using this way to pass parameters to servlet
<a href="StudentManagementServlet?page=${page}&isActivated=${isActivated}" >
but now, I wanna make it using json, so How can I reach ${page} and ${isActivated} from json?

JSP parses the page before it sends it to the client, so you can use the ${variables} anywhere in the code, including inline in javascript.
To store them as a JavaScript object:
var obj = { page: ${page}, isActivated: ${isActivated} };
To store them as a JSON Object:
var jsonObject = { "page" : "${page}", "isActivated": "${isActivated}" };
Now, if you want to send it to a different servlet, you'll need to attach the JSON objectto a POST request to that servlet.
Unfortunately you can't do POST requests from an anchor tag, you'll need to do either an AJAX call or do a form submit with the jsonObject as one of the values.

Related

How to get the page response if i don't know the post parameters?

I want to get the page response (http://202.72.14.52/p2/cci/SearchHistory.aspx) but the page only allows post method. I could't get the response because I don't know what to set in the parameter name/value pairs. I have tried to insert some data in request parameter such as TextBox1=2015&TextBox2=1&TextBox3=2016&TextBox4=17&R1=v2&DropdownList1=01
Can you make a example using this web http://requestmaker.com/?

Sending a post request to REST API from controller in Playframework with Java

I'm new to playframework and to REST API.
I want to send a POST request to REST API in a controller.
What is the best way to do it? Does play have a support for it or do I have to use a plugin?
Basically I want it to look like this:
User submits a form.
I load a form data in a controller.
I send form data as a POST request
Get response, do something with it and display result
So far I'm stuck at point 3, I have no idea how to do this.
Code to visualize what I have in mind:
public static Result processForm() {
Form<FormData> myForm = Form.form(FormData.class).bindFromRequest();
String text = myForm.get().text;
//Send 'text' in a post request and get response
text = doSomethingWithResponse(response);
return ok(resultpage.render(text));
}
I don't think it matters but this is the API I want to use:
http://open.xerox.com/Services/fst-nlp-tools/Pages/API%20Docs
The following line of code sends a request and waits for a reponse:
WS.url(feedUrl).setHeader("Content-Type", "application/x-www-form-urlencoded").post("arg1=val1&arg2=val2").get().asJson();

Obfuscate rest response using bson

I am using REST API for searching. When an ajax call is fired, REST returns json from Java code
return JResponse.ok(searchResult).build() //searchResult is List of Custom object
In javascript I would stringfy that json and parse to show relevant data on screen.
var search = jQuery.parseJSON(JSON.stringify(data));
Now I want to secure/obfuscate json response returned from REST, so that anyone who directly hits APIs won't get readable response. I tried bson but bot able to implement it successfully. Didn't find much support on how to put collection object in bson and how to retrieve it back in JS while googling.
I will suggest you to go with tokens.
Every time when request made to server request must contain a token which change for every request as well check that the request header for ajax request. If it is an ajax request then and then only return result. Also add rule for no cross browser access.
I think if you did it your data will not be accessible to anyone by direct http request.

Form submission with POST method is truncated in Firefox

I have an Struts 2 application running in https.
I have a large form with data size approx 8KB, where the back end object is binding from the front end.
The form is submitted to the server properly in IE.
But when the form is submitted through Firefox then the data is not binding properly in the back end objects as well as the request method is truncated. we are submitting the form by using POST method but in server the method name is 'OST'.
Ex :
HttpServletRequest request;
request.getMethod() return "OST"
form is submitting with java script and jquery
Ex :
var data = $('#regHHFormID').serialize();
$.get('activateProfile.action', data, function(result) {
console.log(result);
}, "json");
Here is the sample data which is submitted to the server after serialize.
household.status=0&dualActiveDutyfamilyTypeID=15&household.key=897788905893&household.createFrom=CONSUMER&household.profileDatas%5B0%5D.type=SPONSOR&household.profileDatas%5B0%5D.profileID=1847&household.profileDatas%5B0%5D.profileNameData%5B0%5D.profileNameID=1885&household.profileDatas%5B0%5D.profileNameData%5B0%5D.firstName=sss&household.profileDatas%5B0%5D.profileNameData%5B0%5D.middleName=&household.profileDatas%5B0%5D.profileNameData%5B0%5D.lastName=sss&household.password=111111111aA&password2=111111111aA&household.familyGroupID=1&household.familyTypeID=4&household.profileDatas%5B0%5D.branchID=5&household.profileDatas%5B0%5D.command=&household.profileDatas%5B1%5D.type=SPOUSE&household.profileDatas%5B1%5D.profileID=&household.profileDatas%5B1%5D.profileNameData%5B0%5D.profileNameID=&household.profileDatas%5B1%5D.profileNameData%5B0%5D.firstName=ss&household.profileDatas%5B1%5D.profileNameData%5B0%5D.middleName=rrr&household.profileDatas%5B1%5D.profileNameData%5B0%5D.lastName=sss&household.profileDatas%5B1%5D.branchID=0&household.profileDatas%5B1%5D.command=&childProfile.profileNameData%5B0%5D.firstName=C4&childProfile.profileNameData%5B0%5D.middleName=CR&childProfile.profileNameData%5B0%5D.lastName=RG&childProfile.isUnBorn=false&birthDate=11.4.2013&childProfile.profileIATAnswerDatas%5B0%5D.questionID=1&childProfile.profileIATAnswerDatas%5B0%5D.profileIATAnswerID=&childProfile.profileIATAnswerDatas%5B0%5D.answer=false&childProfile.profileIATAnswerDatas%5B0%5D.answerDetails=Please+provide+information+that+will+help+us+support+your+child's+medical+needs.&childProfile.profileIATAnswerDatas%5B1%5D.questionID=2&childProfile.profileIATAnswerDatas%5B1%5D.profileIATAnswerID=&childProfile.profileIATAnswerDatas%5B1%5D.answer=false&childProfile.profileIATAnswerDatas%5B1%5D.answerDetails=Please+describe+accommodations+necessary+to+help+this+child+participate+in+Child+and+Youth+Programs.&household.profileDatas%5B2%5D.profileID=0&household.profileDatas%5B2%5D.type=CHILD&household.profileDatas%5B2%5D.profileNameData%5B0%5D.profileNameID=&household.profileDatas%5B2%5D.profileNameData%5B0%5D.firstName=C4&household.profileDatas%5B2%5D.profileNameData%5B0%5D.middleName=CR&household.profileDatas%5B2%5D.profileNameData%5B0%5D.lastName=RG&household.profileDatas%5B2%5D.birthDateConsumer=11.4.2013&household.profileDatas%5B2%5D.isUnBorn=false&household.profileDatas%5B2%5D.editStatus=&household.profileDatas%5B2%5D.profileIATAnswerDatas%5B0%5D.questionID=1&household.profileDatas%5B2%5D.profileIATAnswerDatas%5B0%5D.profileIATAnswerID=&household.profileDatas%5B2%5D.profileIATAnswerDatas%5B0%5D.answer=false&household.profileDatas%5B2%5D.profileIATAnswerDatas%5B1%5D.questionID=2&household.profileDatas%5B2%5D.profileIATAnswerDatas%5B1%5D.profileIATAnswerID=&household.profileDatas%5B2%5D.profileIATAnswerDatas%5B1%5D.answer=false&household.profileDatas%5B0%5D.profilePostalAddressDatas%5B0%5D.profilePostalAddressID=&household.profileDatas%5B0%5D.profilePostalAddressDatas%5B0%5D.typeCode=PRIMARY&household.profileDatas%5B0%5D.profilePostalAddressDatas%5B0%5D.line1=D&household.profileDatas%5B0%5D.profilePostalAddressDatas%5B0%5D.line2=F&household.profileDatas%5B0%5D.profilePostalAddressDatas%5B0%5D.city=F&household.profileDatas%5B0%5D.profilePostalAddressDatas%5B0%5D.stateCode=AL&household.profileDatas%5B0%5D.profilePostalAddressDatas%5B0%5D.zip=12323&household.profileDatas%5B0%5D.profilePostalAddressDatas%5B0%5D.countryCode=USA&household.profileDatas%5B0%5D.profilePhoneNumberDatas%5B0%5D.profilePhoneNumberID=0&household.profileDatas%5B0%5D.profilePhoneNumberDatas%5B0%5D.phoneNumber=111.111.1111&household.profileDatas%5B0%5D.profilePhoneNumberDatas%5B0%5D.typeCode=WORK&household.profileDatas%5B0%5D.profilePhoneNumberDatas%5B0%5D.entityType=CONSUMER&household.profileDatas%5B0%5D.profilePhoneNumberDatas%5B0%5D.entityID=0&household.profileDatas%5B0%5D.profilePhoneNumberDatas%5B0%5D.belongsTo=1&household.profileDatas%5B0%5D.profilePhoneNumberDatas%5B0%5D.editStatus=&household.profileDatas%5B0%5D.profilePhoneNumberDatas%5B0%5D.isPreferred=true&household.profileDatas%5B0%5D.profileEmailAddressDatas%5B0%5D.profileEmailAddressID=1232&household.profileDatas%5B0%5D.profileEmailAddressDatas%5B0%5D.emailAddress=ss%40fer.com&household.profileDatas%5B0%5D.profileEmailAddressDatas%5B0%5D.typeCode=PRIMARY&household.profileDatas%5B0%5D.profileEmailAddressDatas%5B0%5D.entityType=CONSUMER&household.profileDatas%5B0%5D.profileEmailAddressDatas%5B0%5D.entityID=1847&household.profileDatas%5B0%5D.profileEmailAddressDatas%5B0%5D.belongsTo=1&household.profileDatas%5B0%5D.profileEmailAddressDatas%5B0%5D.isNotify=true&household.profileDatas%5B0%5D.profileEmailAddressDatas%5B0%5D.editStatus=&household.emailNotificationDatas%5B0%5D.emailNotificationID=58&household.emailNotificationDatas%5B0%5D.isMandatory=false&household.emailNotificationDatas%5B0%5D.isSelected=true&__checkbox_household.emailNotificationDatas%5B0%5D.isSelected=true&household.emailNotificationDatas%5B1%5D.emailNotificationID=59&household.emailNotificationDatas%5B1%5D.isMandatory=true&household.emailNotificationDatas%5B2%5D.emailNotificationID=60&household.emailNotificationDatas%5B2%5D.isMandatory=false&household.emailNotificationDatas%5B2%5D.isSelected=true&__checkbox_household.emailNotificationDatas%5B2%5D.isSelected=true&household.emailNotificationDatas%5B3%5D.emailNotificationID=61&household.emailNotificationDatas%5B3%5D.isMandatory=false&household.emailNotificationDatas%5B3%5D.isSelected=true&__checkbox_household.emailNotificationDatas%5B3%5D.isSelected=true&household.emailNotificationDatas%5B4%5D.emailNotificationID=62&household.emailNotificationDatas%5B4%5D.isMandatory=false&household.emailNotificationDatas%5B4%5D.isSelected=true&__checkbox_household.emailNotificationDatas%5B4%5D.isSelected=true&household.emailNotificationDatas%5B5%5D.emailNotificationID=63&household.emailNotificationDatas%5B5%5D.isMandatory=true&household.emailNotificationDatas%5B6%5D.emailNotificationID=64&household.emailNotificationDatas%5B6%5D.isMandatory=false&household.emailNotificationDatas%5B6%5D.isSelected=true&__checkbox_household.emailNotificationDatas%5B6%5D.isSelected=true&household.emailNotificationDatas%5B7%5D.emailNotificationID=65&household.emailNotificationDatas%5B7%5D.isMandatory=false&household.emailNotificationDatas%5B7%5D.isSelected=true&__checkbox_household.emailNotificationDatas%5B7%5D.isSelected=true&household.emailNotificationDatas%5B8%5D.emailNotificationID=66&household.emailNotificationDatas%5B8%5D.isMandatory=false&household.emailNotificationDatas%5B8%5D.isSelected=true&__checkbox_household.emailNotificationDatas%5B8%5D.isSelected=true&household.emailNotificationDatas%5B9%5D.emailNotificationID=67&household.emailNotificationDatas%5B9%5D.isMandatory=false&household.emailNotificationDatas%5B9%5D.isSelected=true&__checkbox_household.emailNotificationDatas%5B9%5D.isSelected=true&household.emailNotificationDatas%5B10%5D.emailNotificationID=68&household.emailNotificationDatas%5B10%5D.isMandatory=true&household.emailNotificationDatas%5B11%5D.emailNotificationID=69&household.emailNotificationDatas%5B11%5D.isMandatory=false&household.emailNotificationDatas%5B11%5D.isSelected=true&__checkbox_household.emailNotificationDatas%5B11%5D.isSelected=true&household.emailNotificationDatas%5B12%5D.emailNotificationID=70&household.emailNotificationDatas%5B12%5D.isMandatory=false&household.emailNotificationDatas%5B12%5D.isSelected=true&__checkbox_household.emailNotificationDatas%5B12%5D.isSelected=true&household.emailNotificationDatas%5B13%5D.emailNotificationID=76&household.emailNotificationDatas%5B13%5D.isMandatory=false&household.emailNotificationDatas%5B13%5D.isSelected=true&__checkbox_household.emailNotificationDatas%5B13%5D.isSelected=true&household.emailNotificationDatas%5B14%5D.emailNotificationID=77&household.emailNotificationDatas%5B14%5D.isMandatory=false&household.emailNotificationDatas%5B14%5D.isSelected=true&__checkbox_household.emailNotificationDatas%5B14%5D.isSelected=true&household.emailNotificationDatas%5B15%5D.emailNotificationID=71&household.emailNotificationDatas%5B15%5D.isMandatory=false&household.emailNotificationDatas%5B15%5D.isSelected=true&__checkbox_household.emailNotificationDatas%5B15%5D.isSelected=true&household.emailNotificationDatas%5B16%5D.emailNotificationID=82&household.emailNotificationDatas%5B16%5D.isMandatory=false&household.emailNotificationDatas%5B16%5D.isSelected=true&__checkbox_household.emailNotificationDatas%5B16%5D.isSelected=true&household.emailNotificationDatas%5B17%5D.emailNotificationID=72&household.emailNotificationDatas%5B17%5D.isMandatory=true&household.emailNotificationDatas%5B18%5D.emailNotificationID=73&household.emailNotificationDatas%5B18%5D.isMandatory=true&household.emailNotificationDatas%5B19%5D.emailNotificationID=74&household.emailNotificationDatas%5B19%5D.isMandatory=true&household.emailNotificationDatas%5B20%5D.emailNotificationID=75&household.emailNotificationDatas%5B20%5D.isMandatory=true
I am not finding any clue why this is happening.
Jquery get method in turn calls the AJAX only. so you can think of using the AJAX api of Jquery
$.ajax({
url:"activateProfile.action"
type: "POST",
dataType : "json"
data: data,
success:function(data){
console.log('in your success step');
}
});
Make sure to declare the cache according to your usage. Hope this helps.
If your request.getMethod() returns "OST" this means that a method is not a valid HTTP one. And the browser revert to default which is "GET".
This method has bounds on data send in the URL. It could be limited by the length of the URL itself. Check the <form> tag or if you are using Ajax request the method "POST" is used to encapsulate the data with the request.

JAVASCRIPT values to JSP variables

How can I pass the value of a variable from javascript to a java variable?
<% String st = "<script>document.writeln(selected)</script>";
out.print("value = " + st);%>
This is my code for java getting the values from javascript variable, selected. But no value is displayed.
You have to make a request and send your variable from the browser (where Javascript lives) to the server (where the JSP lives) that way.
Ajax would work, or an HTML form.
In you JSP, you can then receive it as a request parameter (or as part of the request body, or as a request header).
Javascript runs on client. JSP runs on server. The only way to pass information from client to server in web environment is via HTTP parameters or HTTP headers during HTTP request or as a part of request body if method is POST or PUT.
So, you should create such request. It can be done using either changing of your document location or utilizing AJAX call.
you can pass parameter or make a hidden field inside your jsp code and using javascript assign value for this hidden field, then get parameter value in java code.
Use HTML forms.
On server side, you'll get the data in HTTPServletRequest parameter.
Check this too: Building my first Java Web Application

Categories

Resources