0

so i am using java servlets to response to a request from a jsp page. and i want to change the html components name on that jsp page, like i change the buttons value or hide a label.i am wondering if there is any way to access jsp page's HTML components like button, text, ... in a servlet ? i want to return the response in the same page that i have got the requests from. can i just simply write

 button1.name = "john"
or text1.value = "ross geller" ?

1
  • Doesn't this sort of conditional logic belong in the JSP? Commented Jun 17, 2011 at 7:46

1 Answer 1

2

Short answer is "no", longer answer is:

Firstly, you have to understand that HTTP and servlets is not an event-driven GUI like a desktop client, it's a lifecycle oriented, request/response paradigm. What this means is that the client (browser) makes a request for a page. The server (servlet) then responds with the HTML for that page. Once the servlet has sent the HTML to the browser, there is nothing that can be done on the server to change it, unless the browser makes a new request.

In this very basic paradigm, the lifecycle might look something like this:

A request is made by posting a form (browser) -> request is received (servlet) -> servlet does some processing based on request parameters -> HTML is generated (either by the servlet or by forwarding to a JSP page) -> HTML is sent back to the browser -> browser renders the page from the HTML

This is a very basic example, there are many variations on this based on which framework you use but they all boil down to something along these lines.

So, in your case, you have a page with, presumably, a form on it that has a button. You want to post that form and then return the same page but with some other label on the button. In the lifecycle abovem you would extract the parameters posted on the form from the request (paramters=all fields on the form). Then, in the HTML generation, you would use those request parameter values when building the HTML. I would advice you to search the web for some tutorials on servlet technology and look at some examples you might find and this will become clearer.

Sign up to request clarification or add additional context in comments.

2 Comments

.NET "mimics" the behaviour using by using the same syntax as when you work with windows forms, but that is more a features in the IDE (MS Visual Studio) and the often misunderstood "runAt=server". The lifecycle of a web request is still the same. Once the response leaves the server (servlet or aspx page), you can't change it anymore. If you want truly to be able to update a webpage anytime from the server, you have to "fake" event-driven design with Ajax polling (or Web Sockets if you're into HTML5).
thnx, thats what i was thinking. thnx for clearing the case :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.