How to retain form values in jsp page? [duplicate] - java

This question already has answers here:
How can I retain HTML form field values in JSP after submitting form to Servlet?
(2 answers)
Closed 7 years ago.
Here is my code, how can i retain the values of the form when i click on the hyper reference link.(a href: bottom of the code)
<form action="APPServlet">
<div class="">
<div class="">Search For:</div>
<div class="">
<input type="text" size="45" align="right" name="searchRequest">
</div>
</div>
<div class="">
<div class="">Exclude:</div>
<div class="">
<input type="text" size="45" align="right" name="excludeWords">
</div>
</div>
<div class="">
<div class="">In Modules:</div>
<div class="">
<select name="modules">
<option name="module" value="all">All modules</option>
<c:forEach var="module" items="${modelObj.modules}">
<option name="module" value="${module}">${module}</option>
</c:forEach>
</select>
</div>
</div>
</form>
<!-- ahref outside the form , this does not work
&searchRequest=${searchRequest} <- Guess the value is out of scope
-->
<div class="div-table-row">
<div class="div-table-single-col">
${question.id} ${question.topic}
</div>
</div>

You have several possibilities, and all involve JavaScript.
When clicking on the link, change its href using JavaScript by appending all the values of the inputs of the form as parameters of the URL
When clicking on the link, add a hidden field to the form containing the question ID, change the action of the form to make it go to MCQApp instead of going to APPServlet, and submit the form
The cleanest one: when clicking on the link, send an AJAX request to a URL which responds with an HTML fragment (or data), and replace the section of the page that you want to update with this HTML fragment, thus leaving the form as it is in the page.

There are two ways, if you're working with a session, store it as an attribute of your session. But if you're not using sessions, you can use Cookies, just store the values you want in a new cookie and then call the cookie and obtain the values.
Sessions are cookies as well, but they have another functions, the cookies are simplier and help you to store values that you can reuse later. Best regards

Keep the data passed in a model and use it in the jsp like,
Keep all your Input variables in search model object and update it whenever search is called.Keep searchRequest in the request attribute before rendering the response.

Related

how to hide the parameter value in href in jsp?

here is my problem. How could I hide the value of the parameter from the url? because I don't have idea how to hide it. it keep on appearing like this (http://localhost:8084/YIP/MentorServlet?action=peribadi&mentorid=951218-02-5598)
<div id="mySidenav" class="sidenav">
<a href="javascript:void(0)" class="closebtn" onclick="closeNav()">×
</a>
<% String id=request.getParameter("mentorid");%>
<li>
Utama
</li>
<li>
Peribadi
</li>
Some options:
do nothing: this is the best one, as there is no such thing as securely hiding something in HTML. Whoever looks into the page source, will see how the servlet in question can be invoked
switch to a form and a submit button, something what #alayor shows. If you use POST, the parameters will not appear in the URL
switch to a form, but keep the looks of an anchor and submit form from JavaScript (some docs some overcomplicated examples)
manipulate browser history from the target page (docs1, docs2)
keep mentorid in a session variable on server-side: hackers never see it
keep mentorid in an encrypted cookie: hackers see it, but can not decode. However they can try reusing it later (replay attack)
the various other ones I have forgotten and/or never even heard about
You can create an HTML for instead of an anchor.
<div id="mySidenav" class="sidenav">
<a href="javascript:void(0)" class="closebtn" onclick="closeNav()">×
</a>
<% String id=request.getParameter("mentorid");%>
<li>
Utama
</li>
<li>
<form action="/MentorServlet" method="POST">
<input type="hidden" name="action" value="peribadi" />
<input type="hidden" name="mentorid" value="<%=id%>" />
<button>Peribadi</button>
</form>
</li>
This way you can avoid sending the parameter in the URL and it will send in the HTTP Request Body instead.

get parameter in servlet for for each loop

I have a jsp page like this
<c:forEach var="product" items="${products}" >
<div class="col-md-3">
<div class="panel-body">
<div>
<a href="description.html">
<div class="thumbnail">
<img src="data:image/jpeg;base64,${product.base64EncodedImage}" alt="image">
<div class="caption" align="center">
<p>
<h4>${product.name}</h4>
<h5>cost:Rs ${product.price}/-</h5>
i fetch the productimage,price,name using for each.from for each in jsp i have to get parameter values in servlet using request.getParameter() and have to implement in another page using those parameters
I have a tumbnails in page.when we click on it it redirects into another page.there we have to set particular tumnails image,product name,price
help me how to get that parameters in servlet class
please check image in this link--->>
I have to get the Product name,product price, image from 1 tumbnail
Thanks !
Provide hyperlink on image and send all the details you want on servlet as query string
<a href="ServletName?name=${product.name}&price=${product.price}">
<img src="data:image/jpeg;base64,${product.base64EncodedImage}" alt="image"/>
</a>
OR
Send only product id and in servlet fetch all product details for that id from database
<a href="ServletName?id=${product.id}">
<img src="data:image/jpeg;base64,${product.base64EncodedImage}" alt="image"/>
</a>

Do not clear form content upon POST

I am using a Spring form with tabs, each tab again has a form.
The problems are
1)After the form submission, the error messages are displayed but the form content is cleared which should not happen
2)After the form submission the control immediately goes to the default tab which is the first tab in my case. I want the control to stay on current tab after transaction and not go to default tab.
This is the first time I am working using Spring MVC, JSP, jQuery. Please let me know if I have to make any changes.
administrator.jsp
Below is the code for Manage Users tab functionality.
jQuery:
$(document).ready(function() {
$('#btnManage').click(function() {
if($.trim($('#userId').val()) == ''){
$('#area5').html("User Id is a required field.");
}else if($.trim($('#region').val()) == 'NONE'){
$('#area5').html("Region is a required field.");
}else{
$('#manageUserForm').submit();
}
});
$('#btnCancel5').click(function(e) {
$('#manageUserForm').trigger("reset");
$('#area5').html("");
});
});
administrator.jsp:
<div id="tab5" class="tab">
<div class="devices" >
<form:form method="post" id="manageUserForm" modelAttribute="adminUser" action="/DeviceManager/admin/manageUser">
<div align=center class="message" id="area5">${serverError5}</div>
<div>
<div class="plLabelSearch">User Id:</div>
<div class="plinput"><form:input path="userId" type="text" size="29"/></div>
</div>
<div>
<div class="plLabelSearch">Region:</div>
<div class="plselect">
<form:select path="region">
<form:option value="NONE" label="------- Select One -------" />
<form:option value="A" label="A"/>
<form:options items="${regionList}"/>
</form:select>
</div>
</div>
<br>
<br>
<div>
<div class="plLabelSearch"> </div>
<div class="plinput"><a id="btnManage" class="abutton">OK</a></div>
<div class="plinput"><a id="btnCancel5" class="abutton">Cancel</a></div>
</div>
</form:form>
</div>
</div>
You need to re-render the page with the data from the original form submission.
In your case this probably means your form should contain a few hidden fields to allow you to determine which tab you are on, your JSP will need to be able to understand this data and use it to select the appropriate tab.
because you submission is not asynchronous,after you submit,the administrator.jsp is reset,so the error message is display ,owing to this code <div align=center class="message" id="area5">${serverError5}</div>

How to generate edit modals for each element in the model?

I'm trying to use thymeleaf to generate edit and delete modals for each element in the model in my ModelAndView using th:each.
The modals are indeed created and have unique ids based on the id field of the elements. The problem I have is none of the values from elements are parsed into the inputs to enable the user to see the current values.
They are obviously there because the view also has a table which displays each element's values along with the anchors which toggle the modals.
Here's some example code of how I'm doing it:
<div th:each="f : ${foos}" th:id="um- + ${f.id}" class="modal fade"
tabindex="-1" role="dialog">
...
<form role="form" th:action="#{/foo/update}" th:object="${foo}" th:method="post">
<input type="hidden" th:field="*{id}" th:value="${f.id}"/>
<fieldset class="form-group">
<label for="bar">Bar</label>
<input th:field="*{bar}" th:value="${f.bar}" class="form-control"
id="bar" type="text" placeholder="Bar"/>
</fieldset>
...
</form>
...
</div>
How to generate edit modals for each element in the model? I'm not sure why thymeleaf is unable to get the values of the fields from the model elements.
That's not a great approach actually. In addition to it not working, doing using a loop obviously creates n modals for the collection.
The solution that worked best was to provide a single modal that would be populated and submitted with Ajax calls.
This no-frills Spring Boot app has all the relavant code.

How to Automate Web Login in Website abelhas.pt using Java

I am having a hard time trying to figure out the correct process for automating login for the following website: http://abelhas.pt
I have used Firefox's HttpFox plugin to discover the login form and action page url, in the generated HTML code and have found out that to perform login, I will need to post the following parameters:
Login=theLogin
Password=thePassword
Redirect=True
RedirectUrl=
RememberMe=false
FileId=0
The url to which the parameters are being passed when the user presses the submit button, is the following: "/action/login/login" (which is the one shown on the "action" attribute of the html element)
However, when generating and posting it using the following URL:
http://abelhas.pt/action/login/login?Login=theLogin&Password=thePassword&Redirect=True&RedirectUrl=&RememberMe=false&FileId=0
(Please note that the the values of "theLogin" and "thePassword" should be replaced by a valid login and password)
I am always getting "http://abelhas.pt/Error404.aspx", meaning that the following webpage was not found...
In the past, I have already been able to successfully automate logging-in in websites such as Google, filefactory, zippyshare, Imageshack, etc..however this one seems to be playing tricks on me...
Can someone help me understanding what is happening or what am I doing wrong?
Many thanks guys!
Gizmo
In my vaadin application i want to redirect to a website with automatic log in.
Now the issue is am unable to logIn through my java code which refers from
http://www.mkyong.com/java/how-to-automate-login-a-website-java-example/
Am also getting response code: 200, and unable to make use of response.
Following is the code snippet of the website to which i want to automate login
<form action="login.php" method="post" id="clientLogin">
<input type="hidden" name="__CSRFToken__" value="eac035fe0a1a64a9945e0ce798a44a52ca040b5c" /><div style="display:table-row">
<div class="login-box">
<strong></strong>
<div>
<input id="username" placeholder="Email or Username" type="text" name="luser" size="30" value="">
</div>
<div>
<input id="passwd" placeholder="Password" type="password" name="lpasswd" size="30" value=""></td>
</div>
<p>
<input class="btn" type="submit" value="Sign In">
</p>
</div>
<div style="display:table-cell;padding: 15px;vertical-align:top">
<div>
<!--<b>I'm an agent</b> —
sign in here
-->
</div>
</div>
</div>
</form>

Categories