Updates
  • Starting New Weekday Batch for Full Stack Java Development on 27 September 2025 @ 03:00 PM to 06:00 PM
  • Starting New Weekday Batch for MERN Stack Development on 29 September 2025 @ 04:00 PM to 06:00 PM

Parameters

• Parameters are a name-value pair.
• The parameter name and value are of type string.
• The parameter is read-only, i.e., the Web Container stirs the parameter in the corresponding object, and you can read and uses that value. You cannot modify the parameters.

There are three types of parameters:

1. ServletRequest Parameters.
2. ServletConfig Parameters.
3. ServletContgext Parameters.

1. ServletRequest Parameters:

• Client-Submitted Data, which is communicated from the web client to the web server along with HttpRequest, is called as request parameters.
• The Web Container collects client-submitted data and stores it in an HttpServlet. Request object as RequestParameters.
• As a developer, you can collect that data from the request object as follows:

Case 1:

String fn=req.getParameter("fname");
  String ln=req.getParameter("lname");

Case 2:

Map<String,String[]> map=req.getParameterMap();
Set pname=map.keySet();
Iterator it=pname.iterator();
while(it.hasNext()){
  String pnm=(String)it.next();
  Object val=map.get(pnm);
  String[] values=(String[])val;
  System.out.println("\n pname"+pnm+"\n Values");
  for(int i=0;i<values.length;i++){
  System.out.println(values[i]);
  }
  }

Case 3:

To Access only request parameters
  Enumeration<String> ens=req.getParameterName();
List<String> list=Collections.list(ens);
for(String pn:list){
  String pv=req.getParameter(pn);
  System.out.println(pn+":"+pv);
}
Container is storing multiple value for sname key in map in the form of string array.
  Map<String,String[]> map=…;
  String course[]=new String[2];
  Course[0]=”Module 1”;
  Course[1]=”Module 2”;
  Map.put{“course”,course);

2. ServletConfig Paramateres:

• ServletConfig is an interface available in the Javax.servlet package and web container. The vendor is responsible for providing the subclass for this interface.
• Every servlet will have its own ServletConfig object, which cannot be shared.
• When you want to use any data that is common for all users but specific to a particular servlet, that data can be specified as config parameters or init Parameters.
• With Servlet 2.x, specify the config parameters in web.xml.

<servlet>
<servlet-name>helloServlet</servlet-name>
<servlet-class>com.jtcindia.servlets.HelloServlet</servlet-class>
<init-param>
<param-name>email</param-name>
<param-value></param-value>
</init-param>
</servlet>

• With Servlet 3.0, specify the configuration parameters in the servlet class with annotations.

@webServlet(name=”helloServ”,urlPatterns={“/hello.jtc},)
  initParams={
    @WebInitParam(name=”email”,value=”hellosom@jtc”),
    @WebInitParam(name=”phone”,value=”9999”),
}
}
Public class HelloServlet extends HttpServlet{
}

• Web Container collects data from either web.xml or annotation and stores that in ServletConfig object as config parameters.
• As a developer, you can collect that data from the config object as follows:

String em=config.getinitParameters("email");

• You can use the following inherit method from HttpServlet:

Public ServletConfig getServletConfig()
Here when HttpServlet class init() implementation will be invoked (from init() method then config object will be returned otherwise null will be returned.)

• Assume that the HttpServlet class is implemented as follows:

Public abstract class HttpServlet{
Private ServletConfig config;
Public void init(ServletConfig config){
This.config=config;
}
Public ServletConfig getServletConfig(){
Return this.config;
}
….
}

Case 1:

Class Abstract extends HttpServlet{
  Protected void service(…){
  ServletConfig cfg=getServletConfig();
    //return the config object.
    //Since init() method from HttpServlet will be called and config will be initialized
  }
}

Case 2:

public void init(ServletConfig config) throws ServletException {
  super.init(config); //Invoking the HttpServlet init() method
}
public void service(ServletRequest arg0, ServletResponse arg1)
    throws ServletException, IOException {
  ServletConfig cfg=getServletConfig();
  //returns null value
  //Since init() method from your class will be called and you are not calling
HttpServlet init() so config won't be initialized
}

Case 3:

public void service(ServletRequest arg0, ServletResponse arg1)
    throws ServletException, IOException {
  ServletConfig cfg=getServletConfig();
  //returns null value
  //Since init() method from your class will be called and you are not calling
HttpServlet init() so config won't be initialized
}

3. ServletContext Parameters:

• ServletContext is an interface available in the Javax.servlet package, and the container vendor is responsible for provide the subclass for this interface.
• One web application will have only one ServletContext object, i.e., a ServletContext object can be shared with all the servlets running in the container.
• When you want to use any data that is common for all the users and common to all the servlets, then data can be used as a context Parameters in the web.xml are as follows:

<context-param> <param-name>website</param-name> <param-value>www.jtcindia.org</param-value> </context-param>

• The web container collects data from web.xml and stores that in the ServletContext object as context parameters.
• As a developer, you can collect that data from context objects as follows:

String web=context.getInitParameter(“website”);

• You can use the following method with a ServletConfig or ServletContext object to access the corresponding parameter names:

Required Files for Jtc1 Program

1. Login.html
2. web.xml
3. HaiServlet.java
4. . HelloServlet.java

1. Login.html

            
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
   <head>
      <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
      <title>Insert title here</title>
   </head>
   <body>
      <h1>THIS IS INDEX HTML</h1>
      <form action="hello.jtc">
         <h2>Enter Name</h2>
         <input type="text" name="fname">
         <br />
         <input type="submit" value="Hello Test">
      </form>
      <form action="hai.jtc">
         <h2>Enter Phone</h2>
         <input type="text" name="phone">
         <br />
         <input type="submit" value="Hai Test">
      </form>
   </body>
</html>
        
            

2. web.xml

            
<display-name>Jtc2</display-name>
<welcome-file-list>
   <welcome-file>index.html</welcome-file>
</welcome-file-list>
<context-param>
   <param-name>website</param-name>
   <param-value>www.jtcindia.org</param-value>
</context-param>
<servlet>
   <servlet-name>helloSevlet</servlet-name>
   <servletclass>com.jtcindia.servlet.HelloServlet</servletclass>
   <init-param>
      <param-name>email</param-name>
      <param-value>hellosom@jtcindia.org</param-value>
   </init-param>
</servlet>
<servlet-mapping>
   <servlet-name>helloservlet</servlet-name>
   <url-pattern>/hello.jtc</url-pattern>
</servlet-mapping>
<servlet>
   <servlet-name>haiservlet</servlet-name>
   <servletclass>com.jtcindia.servlet.HaiServlet</servletclass>
   <init-param>
      <param-name>email</param-name>
      <param-value>haisom@jtcindia.org</param-value>
   </init-param>
</servlet>
<servlet-mapping>
   <servlet-name>haiservlet</servlet-name>
   <url-pattern>/hai.jtc</url-pattern>
</servlet-mapping>undefined</web-app>
        
            

3. HaiServlet.java

            
package com.jtcindia.servlet;
import java.io.IOException;
import java.io.Writer;
import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/*
 * @Author : Som Prakash Rai
 * @Join : JTC
 * @visit : www.jtcindia.org
 *@Call :+91-9990399111
 * */
public class HaiServlet extends HttpServlet {
   // ServletConfig cfg=null;
   // @Override
   // public void init(ServletConfig config) throws
   ServletException {
      // this.cfg=cfg;
      // System.out.println("init() method of
      HelloServlet ");
      //
      // }
      @Override
      protected void service(HttpServletRequest req,
         HttpServletResponse res)
      throws ServletException,
      IOException {
         System.out.println("servic() method of
               HelloServlet ");
               String fname = req.getParameter("fname"); String phone = req.getParameter("phone"); Writer out = res.getWriter(); res.setContentType("text/html"); out.write("<h1>Response from
                  HelloServlet ");
                  out.write("<hr/>Request Parameters"); out.write("<br/>Fname:" + fname); out.write("<br/>Phone:" + phone); out.write("<hr/>Servlet Config
                     Parameters ");
                     ServletConfig cfg = getServletConfig(); String eml = cfg.getInitParameter("email"); out.write("<br/>" + cfg); out.write("<br/>Email:" + eml); out.write("<hr/>Servlet Context
                        Parameters ");
                        ServletContext ctx = cfg.getServletContext(); String web = ctx.getInitParameter("website"); out.write("<br/>" + ctx); out.write("<br/>Web:" + web);
    }
}
        
            

4. HelloServlet.java

            
package com.jtcindia.servlet;
import java.io.IOException;
import java.io.Writer;
import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import
javax.servlet.http.HttpServletRequest;
import
javax.servlet.http.HttpServletResponse;
/*
 * @Author : Som Prakash Rai
 * @Join : JTC
 * @visit : www.jtcindia.org
 *@Call :+91-9990399111
 * */
public class HelloServlet extends
HttpServlet {
   ServletConfig cfg = null;
   @Override
   public void init(ServletConfig config) throws ServletException {
         this.cfg = cfg;
         System.out.println("init()
            method of HelloServlet ");
         }
         @Override
         protected void
         service(HttpServletRequest req,
            HttpServletResponse res)
         throws
         ServletException, IOException {
            System.out.println("servic()
                  method of HelloServlet ");
                  String fname = req.getParameter("fname"); String phone = req.getParameter("phone"); Writer out = res.getWriter(); res.setContentType("text/html"); out.write("<h1>Response from
                     HelloServlet ");
                     out.write("<hr/>Request Parameters"); out.write("<br/>Fname:" + fname); out.write("<br/>Phone:" + phone); out.write("<hr/>Servlet Config
                        Parameters ");
                        String eml = cfg.getInitParameter("email"); out.write("<br/>" + cfg); out.write("<br/>Email:" + eml); out.write("<hr/>Servlet
                           Context Parameters ");
                           ServletContext ctx = cfg.getServletContext(); String web = ctx.getInitParameter("website"); out.write("<br/>" + ctx); out.write("<br/>Web:" + web);
    }
}