main.jsp
<%@page contentType="text/html" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Filter Example</title> </head> <body> <form method="post" action="/servlet/Login"> Enter user Name<input type="text" name="userName" /><br /> Enter Password<input type="password" name="password" /> <input type="submit" value ="login"> </form> </body> </html>
login.java
import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.Cookie; @WebServlet(urlPatterns = {"/Login"}) public class Login extends HttpServlet { @Override public void doPost(HttpServletRequest req, HttpServletResponse res)throws ServletException, IOException{ String userName = req.getParameter("userName"); String password = req.getParameter("password"); if(userName.equals("krishna") && password.equals("krishna")){ Cookie name = new Cookie("userName", userName); res.addCookie(name); res.sendRedirect("http://localhost:8080/servlet/Welcome"); } else{ Cookie name = new Cookie("userName", "username/password is not valid"); res.addCookie(name); res.sendRedirect("http://localhost:8080/servlet/Error"); } } }
Welcome.java
import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.Cookie; @WebServlet(urlPatterns = {"/Welcome"}) public class Welcome extends HttpServlet { @Override public void doPost(HttpServletRequest req, HttpServletResponse res)throws IOException, ServletException{ try(PrintWriter out = res.getWriter()){ Cookie[] cookie = req.getCookies(); for(Cookie c : cookie){ String name = c.getName(); if(name.equals("userName")) out.println("Welcome " + c.getValue()); } } } public void doGet(HttpServletRequest req, HttpServletResponse res)throws IOException, ServletException{ doPost(req, res); } }
Error.java
import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.Cookie; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; @WebServlet(urlPatterns = {"/Error"}) public class Error extends HttpServlet { @Override public void doPost(HttpServletRequest req, HttpServletResponse res)throws IOException, ServletException{ try(PrintWriter out = res.getWriter()){ Cookie[] cookie = req.getCookies(); for(Cookie c : cookie){ String name = c.getName(); if(name.equals("userName")) out.println(c.getValue()); } } } public void doGet(HttpServletRequest req, HttpServletResponse res)throws IOException, ServletException{ doPost(req, res); } }
Program
redirect to the welcome page, if the user name and password is
'krishna', else redirect to error page.
No comments:
Post a Comment