Hello ,
I really need your help!
i created a textbox in a jsp page , but i want its value to change using another JSP page without using a submit button in the second page , i need it automatically if a certain condition applies..
Pleaaaase can anyone tell me
Thanks...

Dani AI

Generated

You don’t need a second button or any manual click here. There are three clean ways to do this, and they avoid scriptlets in the JSP.

  • Server forward (keeps the same request): In your Select handler (ideally a servlet), compute the value and forward to A.jsp with a request attribute. This updates A.jsp automatically when the condition is met, without another submit and without changing the URL. Also, the correct API is RequestDispatcher.forward, not request.forward as hinted earlier. Thanks for pointing out that no button is required.
// SelectServlet#doGet
String v = computeV(request);
request.setAttribute("v", v);
request.getRequestDispatcher("/A.jsp").forward(request, response);
<!-- A.jsp -->
<input type="hidden" name="v" value="${requestScope.v}">
<c:if test="${not empty requestScope.v}">
  <!-- show the extra form/UI here -->
</c:if>
  • PRG redirect (updates browser URL): If you want A.jsp to be the URL in the address bar and support refresh/back properly, use a redirect and pass v as a query parameter. This still requires no button on the second page. Nice follow-up to ’s approach.
String v = computeV(request);
response.sendRedirect("A.jsp?v=" + URLEncoder.encode(v, "UTF-8"));
<input type="hidden" name="v" value="${param.v}">
  • AJAX update (no navigation at all): Keep the user on A.jsp and fetch v from Select when the condition applies.
<script>
document.addEventListener("DOMContentLoaded", () => {
  fetch("Select?vOnly=1")
    .then(r => r.json())
    .then(({v}) => {
      document.querySelector('input[name="v"]').value = v;
      // toggle the form or run your JS based on v
    });
});
</script>

Tips: choose forward when you only need the value once; choose PRG when the user might refresh or share the URL; use AJAX when you want a seamless UI. And always URL-encode v if you place it in the query string.

Recommended Answers

All 5 Replies

Hello ,
I really need your help!
i created a textbox in a jsp page , but i want its value to change using another JSP page without using a submit button in the second page , i need it automatically if a certain condition applies..
Pleaaaase can anyone tell me
Thanks...

Unfortunately, you will require a submit button to send the value to a jsp page.

Do you have any code that might be helpful to understanding your problem better??

Unfortunately, you will require a submit button to send the value to a jsp page.

No, you don't.
You could have some javascript submitting the form without the need to press a button, say on exiting the input control.

Thank you ..
lets say this page is called A.jsp
my code sends values to a jsp page :

<form onsubmit="return validateSelect()"action="Select.jsp" method="get">
<h2>Select:</h2>
<h3>Enter the URL of the publisher you perfer to publish your ads on: </h3> <br/>
<input type="text" name="SUrl" style="width: 250px;" />
<input TYPE="submit" value="Submit"/>
<input type="hidden" id ="v" name="is"/>
</form>

At the select.jsp page its is supposed to check certain conditions and one of them changes the value of the attribute V that is passed initially null then when doen redircts to A.jsp again with the Value of V changed and depending on the value of V a java script is to show a form in the A.jsp page..
Hope you got the idea
Cause I didn’t know how to change the value of V and resend it to the first page without any button it’s supposed to be done automatically when the select.jsp page is redirected to A.jsp
Thanks again

When A.jsp first loads the V is null. So you can do this:

<%
String V = request.getParamater("V");
if (V==null) {
%>

<form onsubmit="return validateSelect()"action="Select.jsp" method="get">
<h2>Select:</h2>
<h3>Enter the URL of the publisher you perfer to publish your ads on: </h3> <br/>
<input type="text" name="SUrl" style="width: 250px;" />
<input TYPE="submit" value="Submit"/>
<input type="hidden" name="V"/>
</form>

<%
} else {
.....
}
%>

There is method called: request.forward ,
if I remember; check it out. To send the V value through a url at the second page use:

String V = request.getParamater("V");
String url = "A.jsp";
if (V!=null) url = url + "?V="+V;

Also it is better not to have multiple forms in a jsp and if statements to determine which to display. Have different jsps, and at the second page, depending on the value of V decide which one to load.

Thank you so much for your help ... it worked ...
:) :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.