Create login form and perform state management using Cookies, HttpSession and URL Rewriting.

<!-- HTML FILE -->
<html>
<head>
    <title>Login</title>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <style>
        .b1 {
            width: 370px;
            height: 40px;
            margin: 10px;
            color: white;
            background-color: #008CBA;
            border: 0px solid grey;
            cursor: pointer;
        }
        .side {
            padding: 10px;
            margin: 10px;
            width: 370px;
        }
        h1 {
            color: white;
        }
        .box {
            margin-top: 200px;
        }
    </style>
</head>
<body bgcolor="#222222" align="center">
    <div class="box">
        <h1> Welcome to Login Portal</h1>
        <form action="FirstServlet" method="post">
            <input class="side" type="text" name="userName" placeholder="Username" /><br />
            <input class="side" type="password" name="password" placeholder="Password" /><br />
            <input class="b1" type="submit" value="Login" />
        </form>
    </div>
</body>
</html>

3.86
7

                                    &lt;!-- XML FILE --&gt;
&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
&lt;web-app version=&quot;3.1&quot; xmlns=&quot;http://xmlns.jcp.org/xml/ns/javaee&quot; xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot; xsi:schemaLocation=&quot;http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd&quot;&gt;
    &lt;servlet&gt;
        &lt;servlet-name&gt;FirstServlet&lt;/servlet-name&gt;
        &lt;servlet-class&gt;FirstServlet&lt;/servlet-class&gt;
    &lt;/servlet&gt;
    &lt;servlet&gt;
        &lt;servlet-name&gt;SecondServlet&lt;/servlet-name&gt;
        &lt;servlet-class&gt;SecondServlet&lt;/servlet-class&gt;
    &lt;/servlet&gt;
    &lt;servlet-mapping&gt;
        &lt;servlet-name&gt;FirstServlet&lt;/servlet-name&gt;
        &lt;url-pattern&gt;/FirstServlet&lt;/url-pattern&gt;
    &lt;/servlet-mapping&gt;
    &lt;servlet-mapping&gt;
        &lt;servlet-name&gt;SecondServlet&lt;/servlet-name&gt;
        &lt;url-pattern&gt;/SecondServlet&lt;/url-pattern&gt;
    &lt;/servlet-mapping&gt;
    &lt;session-config&gt;
        &lt;session-timeout&gt;
            30
        &lt;/session-timeout&gt;
    &lt;/session-config&gt;

&lt;/web-app&gt;

3.86 (7 Votes)
0
4
3

                                    //First Servlet File
import java.io.*;
import javax.servlet.ServletException;
import javax.servlet.http.*;

public class FirstServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        try {
            response.setContentType(&quot;text/html&quot;);
            PrintWriter out = response.getWriter();
            String name = request.getParameter(&quot;userName&quot;);
            String pwd = request.getParameter(&quot;password&quot;);
            if (pwd.equals(&quot;birju&quot;) &amp;&amp; name.equals(&quot;admin&quot;)) {
                out.print(&quot;&lt;h1&gt;Welcome &quot; + name + &quot;&lt;/h1&gt;&quot;);
                Cookie ck = new Cookie(&quot;uname&quot;, name);// creating cookie object
                response.addCookie(ck);// adding cookie in the response
                out.print(&quot;&lt;b&gt;Cookie has been generated for this session&lt;br&gt;&lt;/b&gt;&quot;);
                out.print(&quot;&lt;b&gt;Click on button to view Cookie...&lt;/b&gt;&quot;);
                // creating submit button
                out.print(&quot;&lt;form action='SecondServlet' method='post'&gt;&quot;);
                out.print(&quot;&lt;br&gt;&lt;input type='submit' value='go'&gt;&quot;);
                out.print(&quot;&lt;/form&gt;&quot;);
            } else {
                out.println(&quot;Incorrect Username or Password!!!&quot;);
            }
            out.close();
        } catch (Exception e) {
            System.out.println(e);
        }
    }

}

//Second Servlet File
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class SecondServlet extends HttpServlet {

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        try {

            response.setContentType(&quot;text/html&quot;);
            PrintWriter out = response.getWriter();

            Cookie ck[] = request.getCookies();
            out.print(&quot;Value stored in Cookie : &quot; + ck[0].getValue());

            out.close();

        } catch (Exception e) {
            System.out.println(e);
        }
    }

}

4 (3 Votes)
0
Are there any code examples left?
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source