Advanced Java and Web Technologies for JNTUK
Blog providing beginner tutorials on different web technologies like HTML, CSS, Javascript, PHP, MYSQL, XML, Java Beans, Servlets, JSP and AJAX
Subscribe to Startertutorials.com's YouTube channel for different tutorial and lecture videos.

Categories: Servlets. No Comments on Initialization Parameters in Servlets

Most of the time, data (Ex: admin email, database username and password, user roles etc…) need to be provided in the production mode (client choice). Initialization parameters can reduce the complexity and maintenance. Initialization parameters are specified in the web.xml file as follows:

<servlet>
	<servlet-name>Name of servlet</servlet-name>
	<servlet-class>Servlet class</servlet-class>
	<init-param>
		<param-name>Mail</param-name>
		<param-value>[email protected]</param-value>
	</init-param>
</servlet>

 

Initialization parameters are stored as key value pairs. They are included in web.xml file inside init-param tags. The key is specified using the param-name tags and value is specified using the param-value tags.

 

Servlet initialization parameters are retrieved by using the ServletConfig object. Following methods in ServletConfig interface can be used to retrieve the initialization parameters:

  • String getInitParameter(String  parameter_name)
  • Enumeration getInitParameterNames()

 

Following example demonstrates initialization parameters:

 

login.html

<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Login</title>
</head>
<body>
	<form action="ValidServ" method="post">
		Username: <input type="text" name="txtuser" /><br/>
		Password: <input type="password" name="txtpass" /><br/>
		<input type="submit" value="Submit" />
		<input type="reset" value="clear" />		
	</form>
</body>
</html>

 

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app>
  <servlet>
    <servlet-name>ValidServ</servlet-name>
    <servlet-class>ValidServ</servlet-class>
    <init-param>
      <param-name>user1</param-name>
      <param-value>pass1</param-value>
    </init-param>
    <init-param>
      <param-name>user2</param-name>
      <param-value>pass2</param-value>
    </init-param>
    <init-param>
      <param-name>user3</param-name>
      <param-value>pass3</param-value>
    </init-param>
    <init-param>
      <param-name>user4</param-name>
      <param-value>pass4</param-value>
    </init-param>
  </servlet>
  <servlet-mapping>
    <servlet-name>ValidServ</servlet-name>
    <url-pattern>/ValidServ</url-pattern>
  </servlet-mapping>
</web-app>

 

ValidServ.java (Servlet file)

import java.io.IOException;
import java.util.Enumeration;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class ValidServ
 */
public class ValidServ extends HttpServlet {
	private static final long serialVersionUID = 1L;
	
	ServletConfig cfg;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ValidServ() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see Servlet#init(ServletConfig)
	 */
	public void init(ServletConfig config) throws ServletException {
		cfg = config;
	}
	
	public void doPost(HttpServletRequest request, HttpServletResponse response) 	throws ServletException, IOException {
		String un = request.getParameter("txtuser");
		String pw = request.getParameter("txtpass");
		boolean flag = false;
		Enumeration<String> initparams = cfg.getInitParameterNames();
		while(initparams.hasMoreElements())
		{
			String name = initparams.nextElement();
			String pass = cfg.getInitParameter(name);
			if(un.equals(name) && pw.equals(pass))
			{
				flag = true;
			}
		}
		if(flag)
		{
			response.getWriter().print("Valid user!");
		}
		else
		{
			response.getWriter().print("Invalid user!");
		}
	}
}

 

In the above example four pairs of initialization parameters are stored in web.xml file. In the servlet file we are validating the username and password entered in the login page against the initialization parameters by retrieving them from web.xml file.

 

In the above example four pairs of initialization parameters are stored in web.xml file. In the servlet file we are validating the username and password entered in the login page against the initialization parameters by retrieving them from web.xml file.

 

Video: Initialization Parameters

How useful was this post?

Click on a star to rate it!

We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post?

Suryateja Pericherla

Suryateja Pericherla, at present is a Research Scholar (full-time Ph.D.) in the Dept. of Computer Science & Systems Engineering at Andhra University, Visakhapatnam. Previously worked as an Associate Professor in the Dept. of CSE at Vishnu Institute of Technology, India.

He has 11+ years of teaching experience and is an individual researcher whose research interests are Cloud Computing, Internet of Things, Computer Security, Network Security and Blockchain.

He is a member of professional societies like IEEE, ACM, CSI and ISCA. He published several research papers which are indexed by SCIE, WoS, Scopus, Springer and others.

Note: Do you have a question on this article or have a suggestion to make this article better? You can ask or suggest us by filling in the below form. After commenting, your comment will be held for moderation and will be published in 24-48 hrs.

Leave a Reply

Your email address will not be published. Required fields are marked *