Reading Initialization Parameters in Servlets

0
608

Reading Initialization Parameters in Servlets

Reading Initialization Parameters in Servlets is the easiest topic in computer science, In this article, you will learn easily Reading Initialization Parameters in Servlets.

Reading Initialization Parameters in Servlets
Reading Initialization Parameters in Servlets

The majority of the time, data (for example, admin email, database username and password, user roles, and so on) must be given in production mode (client choice). Initialization parameters can help to simplify the process and make it easier to maintain. The following initialization parameters are defined in the web.xml file:

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

Main value pairs are used to store the initialization parameters. They’re in the init-param tags of the web.xml format. The param-name tags are used to specify the key, and the param-value tags are used to specify the value.


The ServletConfig object is used to retrieve servlet initialization parameters. The initialization parameters can be retrieved using the methods in the ServletConfig interface:

  • 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 example above, the web.xml file contains four pairs of initialization parameters. We validate the username and password entered in the login page against the initialization parameters in the servlet file by downloading them from the web.xml file.

In the example above, the web.xml file contains four pairs of initialization parameters. We validate the username and password entered in the login page against the initialization parameters in the servlet file by downloading them from the web.xml file.

LEAVE A REPLY

Please enter your comment!
Please enter your name here