# Request, Session and Context Part -1

## Lets try to answer the below problems:

### 1) What are request and response objects ?

### 2) Is servlet itself an object ?

### 3) When are these objects are created and destroyed ?

![](https://1449372731-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LTkdt3HaaBdyfrO73EV%2F-LcoYyIblViVbaB0IJJQ%2F-LcoZSJ_i8dgn5Y4Hp3X%2FScreen%20Shot%202019-04-19%20at%201.13.24%20PM.png?alt=media\&token=69b7eb79-2147-48be-93e5-e5e0d103f299)

### 1) Request and response objects are created per access by the tomcat every time when the request is fired .

### 2) Servlet object is also created by tomcat but not per access.

### 3) Different requests have different servlet thread , not instances for better performance.

## Http  - a stateless protocol (Connection less)

#### For 1 request :  (new Request object is created with pathvariable as userName = Mohit)

![](https://1449372731-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LTkdt3HaaBdyfrO73EV%2F-LcoYyIblViVbaB0IJJQ%2F-Lco_iw2Amb-gyF4UFDG%2FScreen%20Shot%202019-04-19%20at%201.23.51%20PM.png?alt=media\&token=90f41803-4d9f-4000-9952-99720d9a746e)

#### For 2 request : (new Request object is created with pathvariable null )

![](https://1449372731-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LTkdt3HaaBdyfrO73EV%2F-LcoYyIblViVbaB0IJJQ%2F-Lcoa2TViwoPP3Sukddz%2FScreen%20Shot%202019-04-19%20at%201.23.59%20PM.png?alt=media\&token=9ccba1db-8f10-4d31-8868-ecfe351577e4)

## Session Object:

### How to remember ? if http is stateless.

#### Use Session object from request object.

![](https://1449372731-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LTkdt3HaaBdyfrO73EV%2F-LcocPZnfWncAthl1po_%2F-Lcoci0KfIJaSaeSkcRM%2FScreen%20Shot%202019-04-19%20at%201.29.52%20PM.png?alt=media\&token=6e94b8a4-09a3-4022-8fed-be8d358431b1)

#### When query parameter is passed :

![](https://1449372731-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LTkdt3HaaBdyfrO73EV%2F-LcocPZnfWncAthl1po_%2F-LcocleCuuurGVHM4En2%2FScreen%20Shot%202019-04-19%20at%201.35.03%20PM.png?alt=media\&token=8fb36943-0d55-4fd8-968a-5e94762db706)

#### When query parameter is not passed :

![](https://1449372731-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LTkdt3HaaBdyfrO73EV%2F-LcocPZnfWncAthl1po_%2F-LcocvsxbwdoDu2S4c_G%2FScreen%20Shot%202019-04-19%20at%201.36.04%20PM.png?alt=media\&token=5f61b9da-f3d1-408a-a2ba-c9efba821fe8)

#### Username  mohit is still coming from session object.

### Code snippet :

```java
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		System.out.println("Hello from Get method of XML servlet");
		response.setContentType("text/html");
		String userName = request.getParameter("userName");
		PrintWriter printWriter = response.getWriter();
		printWriter.println("Hello from GET method using request parameter " + userName);
		HttpSession httpSession = request.getSession();
		if (userName != null) {
			httpSession.setAttribute("userName", userName);
		}
		printWriter.println("Hello from GET method using session parameter " + httpSession.getAttribute("userName"));
}
```
