Sunday, December 24, 2017

javax.servlet.ServletConfig interface detail

ServletConfig is an interface of servlet API. Implementation of which is provided by vendors. An object of type ServletConfig is created by the server for each servlet.
This object has following use:
1- It is used by the server to provide Servlet specific initialization parameter to
    Servlets. 
2- It is used by the server to provide reference of the ServletContext object to the Servlet.


2.2- ServletConfig object is created for the Servlet.
2.3- <init-param> (if define for the Servlet) are read and store in it.
2.4- Reference of ServletContext is saved in it.
3.0- Server call the init() method of Servlet and provide the reference of ServletConfig.

Method of javax.servlet.ServletConfig interface:

1- getServletContext():
                                      Is used to obtain reference of ServletContext object.
    public ServletContext getServletContext();

2- getInitParameter():
                                      Is used to obtain the values of initialization parameter.
    public String getInitParameter(String PName);

3- getInitParameterNames():
                                      Is used to obtain the name of initialization parameters.
    public Enumeration getInitParameterNames();

Example 1- program to access ServletConfig initialization parameter (Which is Servlet scope) 

Program save with Name: index.html

<html>
<head><title>Login Page</title></head>
<body>
<form method="get" action="loginServlet">
UserId : <input type="text" name="txtUserId"><br>
Password : <input type="password" name="txtPassword"><br>
<input type="submit" value="Login">
</form>
</body>
</html>

Program save with Name: LoginServlet.java

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.sql.*;

public class LoginServlet extends HttpServlet
{
          public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
          {
                   try
                   {
                   response.setContentType("text/html");
                   PrintWriter out=response.getWriter();
                   String id=request.getParameter("txtUserId");
                   String pwd=request.getParameter("txtPassword");
                   ServletConfig cnf=getServletConfig();
                   /* First get ServletConfig object reference Because
                      ServletConfig object contains the Servlet scope initilization parameter <init-param> */
                   Class.forName(cnf.getInitParameter("driverName"));
                   Connection con=DriverManager.getConnection(cnf.getInitParameter("url"),cnf.getInitParameter("userName"),cnf.getInitParameter("password"));
                   PreparedStatement stmt=con.prepareStatement("select * from userInfo where id=? and password=?");
                   stmt.setString(1,id);
                   stmt.setString(2,pwd);
                   ResultSet rset=stmt.executeQuery();
                   if(rset.next())
                   {
                             out.println("<b>Welcome, "+rset.getString(2)+"</b>");
                   }
                   else
                   {
                             out.println("<b> Invalid UserId or Password....<b><br>");
                             RequestDispatcher rd=request.getRequestDispatcher("/index.html");
                             rd.include(request,response);
                   }
                   con.close();
                   out.close();
                   }
                   catch(Exception e)
                   {
                             System.out.println(e);
                   }
          }
}
Program save with Name: web.xml

<web-app>

<servlet>
<servlet-name>s1</servlet-name>
<servlet-class>LoginServlet</servlet-class>

<init-param>
<param-name>driverName</param-name>
<param-value>oracle.jdbc.driver.OracleDriver</param-value>
</init-param>

<init-param>
<param-name>url</param-name>
<param-value>jdbc:oracle:thin:@localhost:1521:xe</param-value>
</init-param>

<init-param>
<param-name>userName</param-name>
<param-value>system</param-value>
</init-param>

<init-param>
<param-name>password</param-name>
<param-value>oracle</param-value>
</init-param>

</servlet>

<servlet-mapping>
<servlet-name>s1</servlet-name>
<url-pattern>loginServlet</url-pattern>
</servlet-mapping>

</web-app>
Output :

If valid userId and password then display:      
Welcome, rupendra

If invalid userId or password then display:      
Invalid UserId or Password....
and include index.html page



No comments:

Post a Comment

Java Functional Interface

Java Lambda expression Lambda expression came to enable to use functional programming feature in java. It provides the facility t...