Click here to watch in Youtube : https://www.youtube.com/watch?v=lxxYdV92LLM
Click the below Image to Enlarge
Servlets : Cookies |
Servlets : Cookies |
Servlets : Cookies |
Servlets : Cookies |
Servlets : Cookies |
Servlets : Cookies |
Servlets : Attribute |
import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; public class FirstServlet extends HttpServlet { private static final long serialVersionUID = 1L; public void init() throws ServletException { System.out.println("----------------------------------------------"); System.out.println(" Init method is called in " + this.getClass().getName()); System.out.println("--------------------------------------------"); } public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); PrintWriter out = response.getWriter(); String username = request.getParameter("username"); out.print("Welcome " + username); HttpSession session = request.getSession(); System.out.println("FirstServlet sessionid :" + session.getId()); session.setAttribute("Username", username); session.setAttribute("Role", "Admin"); out.print("<a href='servlet2'> : visit</a>"); out.close(); } public void destroy() { System.out.println("----------------------------------------------"); System.out.println(" destroy method is called in " + this.getClass().getName()); System.out.println("----------------------------------------------"); } }
import java.io.IOException; import java.io.PrintWriter; import java.util.Enumeration; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; public class SecondServlet extends HttpServlet { private static final long serialVersionUID = 1L; public void init() throws ServletException { System.out.println("----------------------------------------------"); System.out.println(" Init method is called in " + this.getClass().getName()); System.out.println("----------------------------------------------"); } public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); PrintWriter out = response.getWriter(); HttpSession session = request.getSession(); System.out.println("Second Servlet sessionid :" + session.getId()); String userName = (String) session.getAttribute("Username"); out.print("Hello " + userName + "<br>"); out.print("*************************************************************" + "<br>"); Enumeration<String> namesEnum = session.getAttributeNames(); while (namesEnum.hasMoreElements()) { String name = (String) namesEnum.nextElement(); String value = (String) session.getAttribute(name); out.print(name + ":" + value); out.print("<br>"); } out.close(); } public void destroy() { System.out.println("----------------------------------------------"); System.out.println(" destroy method is called in " + this.getClass().getName()); System.out.println("----------------------------------------------"); } }
<?xml version="1.0" encoding="ISO-8859-1"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" metadata-complete="true" version="3.0"> <display-name>HttpSessionAttributeDemo</display-name> <description> This is a simple web application with a source code organization based on the recommendations of the Application Developer's Guide. </description> <servlet> <servlet-name>s1</servlet-name> <servlet-class>FirstServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>s1</servlet-name> <url-pattern>/servlet1</url-pattern> </servlet-mapping> <servlet> <servlet-name>s2</servlet-name> <servlet-class>SecondServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>s2</servlet-name> <url-pattern>/servlet2</url-pattern> </servlet-mapping> </web-app>
<html lang="en"> <head> <meta charset="UTF-8"> <title>Multiple Filters Demo</title> </head> <body> <form action="servlet1" method="GET"> UserName : <input type="text" name="username" /><br /> Password : <input type="password" name="password" /><br /> <input type="submit" value="login" /> </form> </body> </html>
Servlets : HttpSession Interface |
Servlets : HttpSession Interface |
Servlets : HttpSession Interface |
import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class PageRedirectServlet extends HttpServlet { private static final long serialVersionUID = 1L; public void init() throws ServletException { System.out.println("---------------------------"); System.out.println(" Init method is called in " + this.getClass().getName()); System.out.println("---------------------------"); } public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // Set response content type response.setContentType("text/html"); // New location to be redirected String site = new String("http://www.google.com"); response.setStatus(response.SC_MOVED_TEMPORARILY); response.setHeader("Location", site); } public void destroy() { System.out.println("---------------------------"); System.out.println(" destroy method is called in " + this.getClass().getName()); System.out.println("---------------------------"); } }
<?xml version="1.0" encoding="ISO-8859-1"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" metadata-complete="true" version="3.0"> <display-name>PageRedirection</display-name> <description> This is a simple web application with a source code organization based on the recommendations of the Application Developer's Guide. </description> <servlet> <servlet-name>PageRedirect</servlet-name> <servlet-class>PageRedirectServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>PageRedirect</servlet-name> <url-pattern>/PageRedirect</url-pattern> </servlet-mapping> </web-app>
<!DOCTYPE HTML> <html lang="en"> <head> <meta charset="UTF-8"> <title>PageRedirectDemo</title> </head> <body> <li><a href="PageRedirect">PageRedirect</a></li> </body> </html>
import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class MySearchServlet extends HttpServlet { private static final long serialVersionUID = 1L; public void init() throws ServletException { System.out.println("----------------------------------"); System.out.println(" Init method is called in " + this.getClass().getName()); System.out.println("----------------------------------"); } public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String searchKey = request.getParameter("searchKey"); response.sendRedirect("https://www.google.co.in/#q=" + searchKey); } public void destroy() { System.out.println("----------------------------------"); System.out.println(" destroy method is called in " + this.getClass().getName()); System.out.println("----------------------------------"); } }
<?xml version="1.0" encoding="ISO-8859-1"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" metadata-complete="true" version="3.0"> <display-name>SendRedirectDemo</display-name> <description> This is a simple web application with a source code organization based on the recommendations of the Application Developer's Guide. </description> <servlet> <servlet-name>search</servlet-name> <servlet-class>MySearchServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>search</servlet-name> <url-pattern>/search</url-pattern> </servlet-mapping> </web-app>
<!DOCTYPE HTML> <html lang="en"> <head> <meta charset="UTF-8"> <title>SendRedirectDemo</title> </head> <body> <li><a href="mysearch.html">mysearch</a></li> </body> </html>
<!DOCTYPE HTML> <html lang="en"> <head> <meta charset="UTF-8"> <title>SendRedirectDemo</title> </head> <body> <form action="search"> <input type="text" name="searchKey"> <input type="submit" value="Google Search"> </form> </body> </html>
import java.io.IOException; import java.io.PrintWriter; import javax.servlet.RequestDispatcher; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class LoginServlet extends HttpServlet { private static final long serialVersionUID = 1L; public void init() throws ServletException { System.out.println("--------------------------------------"); System.out.println(" Init method is called in " + this.getClass().getName()); System.out.println("--------------------------------------"); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); PrintWriter out = response.getWriter(); String userName = request.getParameter("username"); String passWord = request.getParameter("password"); if (userName.equalsIgnoreCase("ram") && passWord.equals("ram")) { response.sendRedirect("http://localhost:8080/SendRedirectDemo/welcome"); } else { out.print("Sorry UserName or Password is Wrong!"); RequestDispatcher requestDispatcher = request .getRequestDispatcher("/index.html"); requestDispatcher.include(request, response); } } public void destroy() { System.out.println("----------------------------------------"); System.out.println(" destroy method is called in " + this.getClass().getName()); System.out.println("-----------------------------------------"); } }
import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class WelcomeServlet extends HttpServlet { private static final long serialVersionUID = 1L; public void init() throws ServletException { System.out.println("--------------------------------------"); System.out.println(" Init method is called in " + this.getClass().getName()); System.out.println("--------------------------------------"); } public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); PrintWriter out = response.getWriter(); out.print("Welcome"); } public void destroy() { System.out.println("--------------------------------------"); System.out.println(" destroy method is called in " + this.getClass().getName()); System.out.println("--------------------------------------"); } }
<?xml version="1.0" encoding="ISO-8859-1"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" metadata-complete="true" version="3.0"> <display-name>SendRedirectDemo</display-name> <description> This is a simple web application with a source code organization based on the recommendations of the Application Developer's Guide. </description> <servlet> <servlet-name>LoginServlet</servlet-name> <servlet-class>LoginServlet</servlet-class> </servlet> <servlet> <servlet-name>WelcomeServlet</servlet-name> <servlet-class>WelcomeServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>LoginServlet</servlet-name> <url-pattern>/login</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>WelcomeServlet</servlet-name> <url-pattern>/welcome</url-pattern> </servlet-mapping> </web-app>
<!DOCTYPE HTML> <html lang="en"> <head> <meta charset="UTF-8"> <title>RequestDispatcherDemo</title> </head> <body> <form action="login" method="post"> UserName : <input type="text" name="username" /><br /> Password : <input type="password" name="password" /><br /> <input type="submit" value="login" /> </form> </body> </html>
import java.io.IOException; import java.io.PrintWriter; import java.util.Enumeration; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class DisplayHeadersServlet extends HttpServlet { private static final long serialVersionUID = 1L; public void init() throws ServletException { System.out.println("\n*******************************************\n"); System.out.println("HelloWorld servlet init method has been called"); System.out.println("\n*******************************************\n"); } public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); PrintWriter out = response.getWriter(); String title = "HTTP Header Request Example"; String docType = "<!doctype html public \"-//w3c//dtd html 4.0 " + "transitional//en\">\n"; out.println(docType + "<html>\n" + "<head><title>" + title + "</title></head>\n" + "<body bgcolor=\"#f0f0f0\">\n" + "<h1 align=\"center\">" + title + "</h1>\n" + "<table width=\"100%\" border=\"1\" align=\"center\">\n" + "<tr bgcolor=\"#949494\">\n" + "<th>Header Name</th><th>Header Value(s)</th>\n" + "</tr>\n"); Enumeration<String> headerNames = request.getHeaderNames(); while (headerNames.hasMoreElements()) { String headerName = (String) headerNames.nextElement(); out.print("<tr><td>" + headerName + "</td>\n"); String headerValue = request.getHeader(headerName); out.println("<td> " + headerValue + "</td></tr>\n"); } out.println("</table>\n</body></html>"); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } public void destroy() { // do nothing. } }
<?xml version="1.0" encoding="ISO-8859-1"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" metadata-complete="true" version="3.0"> <display-name>ServletForm GET</display-name> <description> This is a simple web application with a source code organization based on the recommendations of the Application Developer's Guide. </description> <servlet> <servlet-name>readAllHeaders</servlet-name> <servlet-class>DisplayHeadersServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>readAllHeaders</servlet-name> <url-pattern>/readAllHeaders</url-pattern> </servlet-mapping> </web-app>
<!DOCTYPE HTML><html lang="en"><head> <meta charset="UTF-8"> <title>Order Pizza</title> </head> <body> <p> <h3>Order Pizza</H3> <p></p> <ul> <li><a href="pizza.html">Order Pizza</a></li> </ul> </body></html>
<html> <head> <title>Retrieving All Parameters</title> </head> <body> <h4>Order your Pizza Here</h4> <form action="readAllHeaders" method="POST"> <b>Name</b> <input type="text" name="name"><br><br> <b>Select the Crust:</b> <select name = "crust"> <option value="pan">Pan</option> <option value="thin">Thin Crust</option> <option value="deep">Deep Crust</option> <option value="cheese">Cheese Burst</option> </select> <br><br> <b>Toppings: </b><br> <input type="checkbox" name="toppings" value="peas">Peas<br> <input type="checkbox" name="toppings" value="paneer">Paneer<br> <input type="checkbox" name="toppings" value="redpeppers">Red Peppers<br> <input type="checkbox" name="toppings" value="pineapple">Pineapple<br> <input type="checkbox" name="toppings" value="onion">Onion<br> <input type="checkbox" name="toppings" value="tomato">Tomato<br><br> <b>Select 1 FREE Appetizer</b> <input type="radio" name="appetizer" value="Garlic Bread">Garlic Bread <input type="radio" name="appetizer" value="Cheese Garlic Bread">Cheese Garlic Bread <input type="radio" name="appetizer" value="Veg Soup">Veg Soup <input type="radio" name="appetizer" value="Veg Sandwich">Veg Sandwich<br><br> <b>Address</b><br> <textarea name="address" rows=3 cols=40></textarea><br><br> <b>Credit Card:</b><br> <input type="radio" name="cardType" value="Visa">Visa <input type="radio" name="cardType" value="MasterCard">MasterCard <input type="radio" name="cardType" value="Amex">American Express <br><br> <b>Credit Card Number:</b> <input type="password" name="cardNum"> <b>Repeat Credit Card Number: </b><input type="password" name="cardNum"><br><br> <input type="submit" name="submit" value="Order Pizza"> </form> </body> </html>