Question
--LoginServlet.java-------------------------------------- package com.example; 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; @WebServlet(/login) public class LoginServlet extends HttpServlet { private
--LoginServlet.java--------------------------------------
package com.example;
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; @WebServlet("/login") public class LoginServlet extends HttpServlet { private static final long serialVersionUID = 1L; public LoginServlet() { super(); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String responsePage = "welcome.jsp"; String userName = request.getParameter("userName"); String password = request.getParameter("password"); String message=""; if (UserCache.users.containsKey(userName)) { User user = UserCache.users.get(userName); if (!user.getPassword().equals(password)) { responsePage="index.jsp"; message="Invalid Credentials"; }else { message=user.getName(); } } else { message="User Does not exists"; responsePage="index.jsp"; } RequestDispatcher requestDispatcher = request.getRequestDispatcher(responsePage); request.setAttribute("message", message); requestDispatcher.forward(request, response); }
}
--------------------------------------RegisterServlet.java----------------------
package com.example;
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; @WebServlet("/register") public class RegisterServlet extends HttpServlet { private static final long serialVersionUID = 1L; public RegisterServlet() { super(); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String userName = request.getParameter("userName"); String password = request.getParameter("password"); String name=request.getParameter("name"); User user=new User(); user.setName(name); user.setPassword(password); user.setUserName(userName); UserCache.users.put(userName, user); response.sendRedirect("index.jsp"); }
} -----------------------------------------------------User.java------------------------------------
package com.example;
public class User { String userName; String password; String name; public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getName() { return name; } public void setName(String name) { this.name = name; } } ----------------------------------------------UserCache.java----------------------------------------------
//Works like database
package com.example;
import java.util.HashMap; import java.util.Map;
public class UserCache { public static Map users = new HashMap(); } -------------------------------------------index.jsp--------------------------------------------
--------------------------------------------------------------------Register.jsp-------------------------------
------------------------------------------------------------welcome.jsp-------------------------------------------------
when i click on login without entering any credentials or if i type wrong password instead of getting the message i need to get pop up message using java script validation saying that credentials are wrong. can anyone please make the changes in the above code and send m?
UserCache.java index.jsp Register.jsp welcome.j LoginServlet... RegisterServ... User.java http://localhost:8080/LoginApp/ Sign In User Name Password Register LoginStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started