How to send values from java class to jsp page ? - java

I have created two jsp pages in which I am able to get the response of my input. Below are my 2 jsp pages. Also I would like to get a response from android java class to the same Index1.jsp.(For eg: If I am sending one string to Index1.jsp I would to get the response as same string).
Index.jsp
<form action="index1.jsp" method="POST">
<input type="checkbox" name="maths" checked="checked" /> Maths
<input type="checkbox" name="physics" /> Physics
<input type="checkbox" name="chemistry" checked="checked" /> Chem
<input type="submit" value="Select Subject" />
</form>
Index1.jsp
<%
Enumeration paramNames = request.getParameterNames();
while(paramNames.hasMoreElements()) {
String paramName = (String)paramNames.nextElement();
out.print("<tr><td>" + paramName + "</td>\n");
String paramValue = request.getParameter(paramName);
out.println("<td> " + paramValue + "</td></tr>\n");
}
%>

Related

How to modify the specific value in the html through jsoup

Here is my code:
Elements parents = doc.select("input[value]");
for (Element parent : parents) {
System.out.println(
parent.attr("value")
.replace("X70xAkOaaAeWGxNgWnTJolmy6/FFoFaBD47IzyBYWf4=", "Ranjan")
.replace("17572418", "17572418123")
.replace("200", "199")
.replace("2018-09-13T16:28:28Z", "2018-09--5T16:28:28Z")
.replace("2018-09-17", "2018-09-25")
);
}
But when I print System.out.println(doc); it is printing the same old value instead I should get the modified one. How to modify the specific value which are under input tag?
EDIT:
I have the following HTML:
<input type="hidden" name="sessionValidity" value="2018-09-13T16:28:28Z">
<input type="hidden" name="shipBeforeDate" value="2018-09-17">
<input type="hidden" name="merchantReturnData" value="">
<input type="hidden" name="shopperLocale" value="en_GB">
<input type="hidden" name="skinCode" value="Ce0xkMuQ">
<input type="hidden" name="merchantSig" value="X70xAkOaaAeWGxNgWnTJolmy6/FFoFaBD47IzyBYWf4=">
I am not familiar with Jsoup but it seems like you do not change the value of the attributes. Element.attr(String s) returns a String. I guess you meant to use public Element attr​(String attributeKey, String attributeValue).
Then you use public String replace(CharSequence target, CharSequence replacement) which does not modify the String itself (String is immutable, replace returns a new String)
I think you want to do this way instead:
private static String html =
"<input type=\"hidden\" name=\"sessionValidity\" value=\"2018-09-13T16:28:28Z\">\n" +
"<input type=\"hidden\" name=\"shipBeforeDate\" value=\"2018-09-17\"> \n" +
"<input type=\"hidden\" name=\"merchantReturnData\" value=\"\"> \n" +
"<input type=\"hidden\" name=\"shopperLocale\" value=\"en_GB\"> \n" +
"<input type=\"hidden\" name=\"skinCode\" value=\"Ce0xkMuQ\"> \n" +
"<input type=\"hidden\" name=\"merchantSig\" value=\"X70xAkOaaAeWGxNgWnTJolmy6/FFoFaBD47IzyBYWf4=\">";
public static void main(String[] args) {
Document doc = Jsoup.parse(html);
doc.select("input[name$=merchantSig]").attr("value", "Ranjan");
// and the other ones
System.out.println(doc.html());
}
that prints out
<html>
<head></head>
<body>
<input type="hidden" name="sessionValidity" value="2018-09-13T16:28:28Z">
<input type="hidden" name="shipBeforeDate" value="2018-09-17">
<input type="hidden" name="merchantReturnData" value="">
<input type="hidden" name="shopperLocale" value="en_GB">
<input type="hidden" name="skinCode" value="Ce0xkMuQ">
<input type="hidden" name="merchantSig" value="Ranjan">
</body>
</html>
You can see that the merchantSig value has been modified

S3 Signature Doesn't Match Error -- SignatureDoesNotMatch

I'm using AWS' S3 example file upload but can't seem to get the signature to match their calculated value. Any suggestions would be appreciated.
Example: https://aws.amazon.com/articles/1434
Using Tomcat 8, Java 1.8.60
My JSP FIle:
<%# page import="java.util.Base64" %>
<%# page import="javax.crypto.Mac" %>
<%# page import="javax.crypto.spec.SecretKeySpec" %>
<html>
<head>
<meta http-equiv="Content-Type" content="image/jpeg; charset=UTF-8" />
</head>
<body>
<%
String policy =
"{\"expiration\": \"2020-01-01T00:00:00Z\"," +
"\"conditions\": [" +
"{\"bucket\": \"my-bucket\"}," +
// "[\"starts-with\", \"$key\", \"*\"]," +
"{\"acl\": \"private\"}," +
"{\"success_action_redirect\": \"OpSuccess.jsp\"}," +
"[\"starts-with\", \"$Content-Type\", \"\"]," +
"[\"content-length-range\", 0, 1048576]" +
"]" +
"}";
policy.replaceAll("\n","").replaceAll("\r","");
String encPolicy = Base64.getEncoder().encodeToString(policy.getBytes("UTF-8"));
String awsAccessKeyId = <my id>;
String awsSecretKey = <my private key>;
Mac hmac = Mac.getInstance("HmacSHA1");
hmac.init(new SecretKeySpec(awsSecretKey.getBytes("UTF-8"), "HmacSHA1"));
String signature = Base64.getEncoder().encodeToString(hmac.doFinal(policy.getBytes("UTF-8"))).replaceAll("\n", "").replaceAll("\r","");
%>
<form action="https://s3.amazonaws.com/my-bucket/" method="post" enctype="multipart/form-data">
<input type="hidden" name="key" value="p_1_L.jpg">
<input type="hidden" name="AWSAccessKeyId" value="<%= awsAccessKeyId %>">
<input type="hidden" name="acl" value="private">
<input type="hidden" name="success_action_redirect" value="OpSuccess.jsp">
<input type="hidden" name="policy" value="<%= encPolicy %>" >
<input type="hidden" name="signature" value="<%= signature %>" >
<input type="hidden" name="Content-Type" value="image/jpeg">
File to upload to S3:
<input name="file" type="file">
<br>
<input type="submit" value="Upload File to S3">
</form>
</body>
</html>
After some digging I found the problem. I was signing the string version of the policy not the base64 encoded version...
String signature = Base64.getEncoder().encodeToString(hmac.doFinal(*policy*.getBytes("UTF-8"))).replaceAll("\n", "").replaceAll("\r","");
%>
Should be
String signature = Base64.getEncoder().encodeToString(hmac.doFinal(*encPolicy*.getBytes("UTF-8"))).replaceAll("\n", "").replaceAll("\r","");
%>
in the above example

Html POST method not working

Why is my method does not work?
My Java code:
#POST
#Path("/request=PostStage")
#Produces(MediaType.APPLICATION_JSON)
public String getStagePOST(#QueryParam("fn")String fn,
#QueryParam("tn")String tn,
#QueryParam("stat")String stat,
#QueryParam("length")String length,
#QueryParam("lon")String lon,
#QueryParam("lat")String lat,
#QueryParam("crgw")String crgw,
#QueryParam("lane")String lane) throws SQLException{
return "Lat: " + lat + " lon: " + lon + " crgw: " + crgw;
}
My HTML code:
<form action="http://localhost:9090/services/stage/request=PostStage" method="POST">
<p>Localization:</p>
<p> fn : <input name="fn" /></p>
<p> tn : <input name="tn" /></p>
<p>stat : <input name="stat" /></p>
<p>length : <input name="length" /></p>
<p>Geoposition:</p>
<p>lon : <input name="lon" /></p>
<p>lat : <input name="lat" /></p>
<P> Other:</P>
<p>crgw : <input name = "crgw" /></p>
<p> lane : <input name="lane" /></p>
<input type="submit" value="Searchh" />
</form>
I give examples of parameters in a html page: lon - 12, lat - 12 etc.
As a result, I get:
Lat: null lon: null crgw: null
Why?
I can not find the problem :(
Very thanks for all answers .
Shouldn't there be types on the input-tags, like <input name="lon" type="text" />?
Also verify that the POST from the form really contains the parameters, the browser debugging IDE in both Chrome and Firefox is probably able to see how the POST-request looks.
Working! My mistake was to use "#QueryParam" instead "#FormParam".

Overwrite the text file using jsp

I am trying to overwrite a text file which contains 6 lines of data by inserting 6 different lines of data.
Here is my form.
<form method="POST" action="write.jsp">
Name: <input type="text" name="name1" size="20" /><br>
Price: <input type="text" name="price" size="20" /><br>
Due time: <input type="text" name="DueTime" size="20" /><br>
Location: <input type="text" name="Location" size="20" /><br>
Photo Url: <input type="text" name="url" size="20" /><br>
Description: <input type="text" name="desc" size="20" /><br>
<input type="submit" name="submit" onclick="window.close()"/>
</form>
After user clicks on submit, it will go to write.jsp, which is shown below.
String name = request.getParameter("name1");
String price = request.getParameter("price");
String DueTime = request.getParameter("DueTime");
String Location = request.getParameter("Location");
String url = request.getParameter("url");
String desc = request.getParameter("desc");
String paths="somepath/hotel1.txt";
FileWriter filewriter = new FileWriter(paths, true);
filewriter.write(name + System.getProperty("line.separator"));
filewriter.write(price + System.getProperty("line.separator"));
filewriter.write(DueTime + System.getProperty("line.separator"));
filewriter.write(Location + System.getProperty("line.separator"));
filewriter.write(url + System.getProperty("line.separator"));
filewriter.write(desc + System.getProperty("line.separator"));
filewriter.close();
But somehow it does work, can anyone help me to fix this?

html to jsp email form post - null parameters

I have a simple form
form action="email.jsp" method="post"
<label for="firstname">Your Name: </label>
input type="text" id="name"<br/>
<label for="email">Your Email: </label>
input type="text" id="address"<br/>
<label for="message">Message: </label>
textarea size="30" rows="4" class="expand" id="comments"</textarea<br/>
input type="submit" value="Send" input type="reset"
/form
and am posting to a email.jsp page running in tomcat 5.5, working for another website i use that is in flash
this email.jsp is coming up with null values every time i post data to it - code below
can anyone see what i'm doing wrong?
<%# page import="sun.net.smtp.SmtpClient, java.io.*, javax.servlet.http.HttpServletResponse, javax.servlet.jsp.PageContext" %>
<%
String name = request.getParameter("name");
out.print(name);
String address = request.getParameter("address");
String comments = request.getParameter("comments");
String from="test#there.com.au";
String to="test#where.com";
try{
SmtpClient client = new SmtpClient("localhost");
client.from(from);
client.to(to);
PrintStream message = client.startMessage();
message.println("From: " + from);
message.println("To: " + to);
message.println();
message.println("Enquiry :-) from " + name + " at " + address);
message.println();
message.println("Details: "+ comments);
client.closeServer();
}
catch (IOException e){
System.out.println("ERROR SENDING EMAIL:"+e);
}
out.print("Email Sent Correctly");
%>
Your HTML is a bit garbled, but it looks like you're not specifying the "name" attribute for your inputs. The "id" attribute is good for referencing your field from a <label> or for accessing your inputs from Javascript, but the browser will not use it in the POST request.
The solution is simple, add the "name" attribute to your input elements, like this:
<input type="text" id="name" name="name" />

Categories

Resources