Understanding sendRedirect Method in JSP and Servlet - BunksAllowed

BunksAllowed is an effort to facilitate Self Learning process through the provision of quality tutorials.

Random Posts

Understanding sendRedirect Method in JSP and Servlet

Share This

Source Code of index.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <!DOCTYPE html> <html> <head> <meta charset="ISO-8859-1"> <title>Insert title here</title> </head> <body> <form action="LoginServ"> <input type="text" name="name"> <input type="password" name="pass"> <input type="submit"> </form> </body> </html>
Source Code of LoginServ.java
package com.t4b.test; import java.io.IOException; import javax.servlet.RequestDispatcher; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * Servlet implementation class LoginServ */ @WebServlet("/LoginServ") public class LoginServ extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public LoginServ() { super(); } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse * response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { System.out.print(request.getParameter("name")); System.out.print(request.getParameter("pass")); if (request.getParameter("name").equalsIgnoreCase("user1") && request.getParameter("pass").equalsIgnoreCase("1234")) { RequestDispatcher rd = request.getRequestDispatcher("home.jsp"); rd.forward(request, response); } else { RequestDispatcher rd = request.getRequestDispatcher("index.jsp"); rd.forward(request, response); } } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse * response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
Source Code of home.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <!DOCTYPE html> <html> <head> <meta charset="ISO-8859-1"> <title>Insert title here</title> </head> <body> <h1>Home page</h1> </body> </html>

Happy Exploring!

No comments:

Post a Comment