JSP & Servlets
1.0.0
1.0.0
  • Setting up
  • Pre-requisites
  • Servlet Part 1
  • Servlet Part 2
  • Understanding the servlet
  • Servlet XML Configuration
  • POST method and passing parameters
  • Passing more parameters
  • Understanding GET and POST
  • Request, Session and Context Part -1
  • Request , Session and Context Part - 2
  • Understanding init, service and ServletConfig
  • Hello JSP
  • Understanding JSP
  • JSP Page directives
  • HttpServletRequest Path Decoding
  • Scopes in JSP and the PageContext Object
  • Understanding MVC pattern
  • PART -1 Writing an MVC app
  • JSTL
    • For-each
  • CRUD-1
  • Project Work
    • Create a Simple Java Web Application Using Servlet, JSP/JSTL and JDBC/Transaction
  • Locale Filter
  • Action Plan
    • Schedule
    • Hit webservice
Powered by GitBook
On this page
  • 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 ?
  • 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)
  • Session Object:
  • How to remember ? if http is stateless.
  • Code snippet :

Was this helpful?

Request, Session and Context Part -1

PreviousUnderstanding GET and POSTNextRequest , Session and Context Part - 2

Last updated 6 years ago

Was this helpful?

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 ?

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)

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

Session Object:

How to remember ? if http is stateless.

Use Session object from request object.

When query parameter is passed :

When query parameter is not passed :

Username mohit is still coming from session object.

Code snippet :

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