Html POST method not working - java

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".

Related

Is it possible to have an input path for variables in Thymeleaf

I know the title might be misleading but here my question: In Thymeleaf we set request params with the input (in HTML). Is it possible to have an input field that sets the path variable. For example I have an method like this:
#PostMapping("/house/{id}/rent")
public String rentHouse(#RequestParam Date startDate, #PathVariable("id") long id, Model model) {
House h = new House();
h.setId(id);
r.setStartDate(startDate);
Rents rents = rentsService.createNewRent(h, id);
model.addAttribute("rent", rents);
return "House";
}
And in House.html I want something like this:
<form th:action="#{/house/${id}/rent/}" method="post">
<label for="startDate">start Date:</label><br>
<input type="datetime-local" id="startDate" th:name="startDate" placeholder="startDate"><br>
<label for="id">house id:</label><br>
<input type="number" id="id" th:name="id" placeholder="id"><br>
<br>
<input type="submit" value="Submit">
<input type="reset" value="Reset">
So that when I input something then the result url should be looking like this (I know start Date has false format):
localhost:8080/House/12/rents?startDate=02.21.22
And is it also possible to pass request body in Thymeleaf, I searched for similar questions but they all solved it by manually putting the path variable in the url.
Thanks in advance

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

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?

How to send values from java class to jsp page ?

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");
}
%>

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