Schedule

--index.html
--in welcome list give the name of servlet
--revert the things
--create login form in index.html with action as "login"
-- in dopost write method to authenticate uname and password with db
--print userbane and details on index.html

--create a new success.jsp and redirect  id correct cred


String userName = request.getParameter("username");
		String pswd = request.getParameter("password");
		if (userName == "Admin" && pswd == "Admin") {
			response.sendRedirect("success.jsp");
		}

--
--use of session to display username

if (userName.equals("Admin") && pswd.equals("Admin")) {
			request.getSession().setAttribute("username", userName);
			response.sendRedirect("success.jsp");
		} else {
			response.sendRedirect("Index.html");
		}

---on jsp---
<%=request.getSession().getAttribute("username")%>

--- use jstl to get the username on jsp---
download jar : 
https://mvnrepository.com/artifact/org.glassfish.web/javax.servlet.jsp.jstl/1.2.5
prefix: <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>

<br> Hello ${username} in success.jsp

-- want to see request attribute 			request.setAttribute("user", "userTest");
on success.jsp without setting it in session?

---use--- 

request.setAttribute("user", "userTest");
			RequestDispatcher requestDispatcher = request.getRequestDispatcher("success.jsp");
			requestDispatcher.forward(request, response);

--sendridirect after forward
java.lang.IllegalStateException: Cannot call sendRedirect() after the response has been committed
	at org.apache.catalina.connector.ResponseFacade.sendRedirect(ResponseFacade.java:488)

diff b/w forward and include

RequestDispatcher requestDispatcher = request.getRequestDispatcher("success.jsp");
			requestDispatcher.forward(request, response);
			response.getWriter().println("back to servlet"); // this wont b displayed

Last updated

Was this helpful?