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
  • Servlet objects are created in two phases :
  • There are methods which run before doGet or doPost
  • init() and init(ServletConfig) and service are part of GenericServlet
  • service() (overidden from GenericServlet) and doGet ..... are part of HttpServlet
  • MyServlet will override doGet and doPost .....
  • Examples :
  • 1) Initparams (Annotation)
  • 2) Using web.xml

Was this helpful?

Understanding init, service and ServletConfig

PreviousRequest , Session and Context Part - 2NextHello JSP

Last updated 6 years ago

Was this helpful?

Servlet objects are created in two phases :

1) At the first call

2) Next only threads are created for each request.

There are methods which run before doGet or doPost

  1. init()

  2. service()

init() and init(ServletConfig) and service are part of GenericServlet

service() (overidden from GenericServlet) and doGet ..... are part of HttpServlet

MyServlet will override doGet and doPost .....

we have already seen

Examples :

1) Initparams (Annotation)

package com.gs.controller;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebInitParam;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class SimpleServlet
 */
@WebServlet(description = "Simple hello", urlPatterns = { "/SimpleServletPath" }, initParams = {
		@WebInitParam(name = "defaultUser", value = "Mohit Malhotra") })
public class SimpleServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		System.out.println("Hello from Get method");
		PrintWriter printWriter = response.getWriter();
		printWriter.println("<h1>Hello in HTML</h1>");
		String initParamValue = this.getServletConfig().getInitParameter("defaultUser");
		printWriter.println("Hello from init method " + initParamValue);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}

2) Using web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns="http://xmlns.jcp.org/xml/ns/javaee"
	xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
	id="WebApp_ID" version="3.1">
	<display-name>SimpleServletProject</display-name>
	<servlet>
		<servlet-name>xmlServlet</servlet-name>
		<servlet-class>com.gs.controller.XmlServlet</servlet-class>
		<init-param>
			<param-name>defaultUser</param-name>
			<param-value>Mohit Malhotra from XML</param-value>
		</init-param>
	</servlet>
	<servlet-mapping>
		<servlet-name>xmlServlet</servlet-name>
		<url-pattern>/xmlservletpath</url-pattern>
	</servlet-mapping>
</web-app>
import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class XmlServlet extends HttpServlet {

	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);
		//getting session
		HttpSession httpSession = request.getSession();
		//getting context
		ServletContext context = request.getServletContext();
		if (userName != null) {
			httpSession.setAttribute("userName", userName);
			context.setAttribute("userName", userName);
		}
		printWriter.println("---Hello from GET method using session parameter--- " + httpSession.getAttribute("userName"));
		printWriter.println("---Hello from GET method using context parameter--- " + context.getAttribute("userName"));
		String initParamValue =  this.getServletConfig().getInitParameter("defaultUser");
		printWriter.println("---Hello from init method using context parameter--- " + initParamValue);
	}
}