Overwrite the text file using jsp - java

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?

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

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

pop up message when user has registered successfully

This is my register.jsp page.
Here is where I validate the form using javascript
<script>
function validateForm()
{
var x = document.forms["myForm"]["firstname"].value;
if (x === null || x === "")
{
alert("First name must be filled out");
return false;
}
var x = document.forms["myForm"]["lastname"].value;
if (x === null || x === "")
{
alert("Second name must be filled out");
return false;
}
var x = document.forms["myForm"]["username"].value;
if (x === null || x === "")
{
alert("User name must be filled out");
return false;
}
var x = document.forms["myForm"]["password"].value;
if (x === null || x === "")
{
alert("Password must be filled out");
return false;
}
var x = document.forms["myForm"]["username"].value;
for (var i = 0; i < username.length; i++) {
if (username[i] === newName)
{
alert("USER");
success = false;
//stop further program execution
return false;
}
}
}
</script>
Here is the form the user fills in.
Register Here:
<form class="form" name="myForm" id="loginForm" action="regServlet" method="doPost" onsubmit="return validateForm();" >
<p class="contact">
<label class="description" for="element_1">First Name </label></p>
<input id="name" name="firstname" placeholder="Firstname" class="element text medium" type="text" maxlength="255" value=""/>
<br>
<p class="contact">
<label class="description" for="element_2">Last Name </label></p>
<input id="element_2" name="lastname" placeholder="Lastname" class="element text medium" type="text" maxlength="255" value=""/>
<br>
<p class="contact">
<label class="description" for="element_3">UserName </label></p>
<input id="element_3" name="username" placeholder="Username"class="element text medium" type="text" maxlength="255" value="" onblur="varify();"/><span id="res"></span>
<br>
<p class="contact">
<label class="description" for="element_4">Password </label></p>
<input id="element_4" name="password" placeholder="Password" class="element text medium" type="password" maxlength="255" value=""/>
<br>
<p class="contact">
<label class="description" for="element_9">Email </label></p>
<input id="element_9" name="email" placeholder="E-mail" class="element text medium" type="email" maxlength="255" value=""/>
<p> Press To Submit </p>
<input id="loginButton" class="uname" type="hidden" name="action" value="register" />
<input type="submit" value="register" />
</ul>
</form>
I want to be able to have a pop up saying the user has successfully registered. How do I go about doing this?
In your servlet you can set a request attribute and then forward back to the register.jsp page where you can recover it and show the pop-up

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

This code is not submitting to a form? Why

Since I am using get I expect to see the submitted values appended to the queryString but instead all I see is the URL of the servlet being called with nothing added at the end.
<form id="editClassList" name="editClassList" method="get" action="EditClassList">
<%
HashMap<Integer,String> classes = new HashMap<Integer,String>();
classes = (HashMap<Integer,String>) request.getAttribute("classes");
%>
<%
if(classes.size()==0){ %>
<label><input class="small-link" type="text" id="add-this-class"
size="42" value="" /></label>
<%
}
%>
<%
Set<Integer> classIds = new HashSet<Integer>();
classIds = classes.keySet();
Iterator<Integer> itr = classIds.iterator();
while(itr.hasNext()){
int nextId = (Integer)itr.next();
%>
<label><input class="small-link" type="text" id="<% out.print(nextId); %>"
size="42" value="<% out.print(classes.get(nextId)); %>" />
</label>
<img id="add-class" src="images/add.png" width="16" height="16" /><br />
<label><input class="small-link" type="text" id="class-to-add"
size="42" value="" /></label>
<%
}
%>
<label><input type="submit" id="save-class-btn" value="Save Class(es)" /></label>
</form>
A try: your input tags lacks the name attribute ?
<input name="data" class="small-link" type="text" id="class-to-add" size="42" value="" />

Categories

Resources