> For the complete documentation index, see [llms.txt](https://gyansetu-jspandservlets-for-java.gitbook.io/workspace/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://gyansetu-jspandservlets-for-java.gitbook.io/workspace/request-session-and-context-part-1.md).

# 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 ?

![](/files/-LcoZSJ_i8dgn5Y4Hp3X)

### 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)

![](/files/-Lco_iw2Amb-gyF4UFDG)

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

![](/files/-Lcoa2TViwoPP3Sukddz)

## Session Object:

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

#### Use Session object from request object.

![](/files/-Lcoci0KfIJaSaeSkcRM)

#### When query parameter is passed :

![](/files/-LcocleCuuurGVHM4En2)

#### When query parameter is not passed :

![](/files/-LcocvsxbwdoDu2S4c_G)

#### 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"));
}
```
