9

I don't understand why ServletRequest has a setAttribute method. Eg: if i have a jsp page containing 1 textbox and a submit button...

My objective is when i submit my name should be displayed in that textbox. Question here: whatever is displayed in that textbox is response from servlet right?

But in my assignment in servlet part: It was written request.setAttribute("name","abc");

at jsp side:
It was written: <input type="text" value = < %request.getAttribute("name")% > >

I know in javadocs they have setattribute inside request but why not under response?

Noumenon
  • 5,099
  • 4
  • 53
  • 73
swapyonubuntu
  • 1,952
  • 3
  • 25
  • 34

3 Answers3

11

Request : From Client to Server

Response: From Server to Client

When your client sends your servlet request, you add object in request and later retrieves it in jsp.The request is passed from servlet to jsp.Your jsp retrieves the object,generates a HTML and sends it as response back to your browser.The same request sent by your client is passed from one servlet or jsp to another jsp or servlet and then these are compiled to generate HTML format and sent to browser as response from server.Setting object in response doesnot make sense because browser just displays HTML.

ntstha
  • 1,187
  • 4
  • 23
  • 41
6

Because I think setting the attribute of a response makes no sense. You can go to a hotel and order for lunch. This is request, and what you want for lunch, that is a request's Attribute. But the quality of the food i.e. response entirely depends on the hotel (the server). Similarly when a Web server responds to a HTTP request to the browser, the response typically consists of a status line, some response headers, and the document (optional, like any JSON data or something similar). So if you have response.setAttribute i.e means you can change the name of the browser from Mozilla to Chrome, but that's insane.

Pabru
  • 231
  • 2
  • 7
argentum47
  • 2,385
  • 1
  • 18
  • 20
5

when they say request.setAttribute(Object, Object) it is simply to imply that the scope of the attribute is for that request only and the attribute will not exist in subsequent requests.

qualebs
  • 1,291
  • 2
  • 17
  • 34