Tuesday 12 May 2015

ServletConfig Interface

index.jsp

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title></title>
    </head>
    <body>
       <form action="config_interface" method="get">
            <input type="submit" value="click here">
        </form>
    </body>
</html>

sevlet code  config_interface.java

 protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
       
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
       
        ServletConfig config = getServletConfig();
       
        String value = config.getInitParameter("mona");
        String value1 = config.getInitParameter("driver");
       
        out.println("value of mona is - <h2>"+value+"</h2>");
        out.println("value of driver is - <h2>"+value1+"</h2>");
        out.close();
               
       
    }


web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
    <servlet>
        <servlet-name>config_interface</servlet-name>
        <servlet-class>swati.config_interface</servlet-class>
       
         <init-param>
        <param-name>mona</param-name>
        <param-value> An object of ServletConfig is created by the web container for each servlet. This object can be used to get configuration information from web.xml file.

If the configuration information is modified from the web.xml file, we don't need to change the servlet. So it is easier to manage the web application if any specific content is modified from time to time. </param-value>
       
    </init-param>
     <init-param>
        <param-name>driver</param-name>
        <param-value>sun.jdbc.odbc.JdbcOdbcDriver</param-value>
       
    </init-param>
    </servlet>
    <servlet-mapping>
        <servlet-name>config_interface</servlet-name>
        <url-pattern>/config_interface</url-pattern>
    </servlet-mapping>
    <session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>
   
  
</web-app>

Simple Login

index.jsp

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>index page</title>
    </head>
    <body>
        <form action="Validate" method="post">
            <center>
                User Name : <input type="text" name="uname"><br><br>
                Password : <input type="password" name="pass"><br>
                <br>
                <input type="submit" value="Login">
            </center>
        </form>
    </body>
</html>


valid.java

protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
       
        response.setContentType("text/html");
        PrintWriter out=response.getWriter();
       
        String name=request.getParameter("uname");
        String pass=request.getParameter("pass");
       
        if(pass.equals("admin") && name.equals("admin")){
            RequestDispatcher rd = request.getRequestDispatcher("welcome.jsp");
            rd.forward(request, response);
        }
        else{
            out.println("<center><h2><font color='red'>Sorry! Wrong user name...</font></h2></center>");
            RequestDispatcher rd = request.getRequestDispatcher("index.html");
            rd.include(request, response);
        }
    }

welcome.jsp

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Welcome</title>
    </head>
    <body>
        <% String name = request.getParameter("uname");
        out.println("Welcome "+name);%>
    </body>
</html>