Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

------------------------------------------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--------------------------------------------

Login

Sign In

User Name
Password
Register Login

--------------------------------------------------------------------Register.jsp-------------------------------

Registration

Registration

Name
User Name
Password
Register

------------------------------------------------------------welcome.jsp-------------------------------------------------

Insert title here

Welcome Logout

by using this code i am unable to go to register page i am attavhing the problem

image text in transcribed

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 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 Login

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions

Question

Connect with your audience

Answered: 1 week ago