This is a simple Java web application that demonstrates a basic user login functionality using Jakarta Servlets. The application presents a login form, validates user credentials against hardcoded values, and then either forwards the user to a profile page upon success or displays an error message upon failure.
- User login page with username and password fields.
- Server-side credential validation using a Java Servlet.
- Use of
RequestDispatcherto forward to a success page (/profile). - Use of
RequestDispatcherto include the login page with an error message on failure. - A simple profile page to welcome the logged-in user.
- Backend: Java, Jakarta Servlets API
- Frontend: HTML
- Server: Any Jakarta EE compatible servlet container (e.g., Apache Tomcat, Jetty)
The application flow is as follows:
- Login Page: The user is first directed to
index.html, which contains a simple HTML form asking for a username and password. - Form Submission: The form submits the credentials using the
POSTmethod to theLoginservlet (mapped to/login). - Authentication: The
Loginservlet retrieves theusernameandpasswordfrom the request. It checks them against the hardcoded values:- Username:
priyanshu - Password:
123
- Username:
- Successful Login: If the credentials match, the servlet sets the username as a request attribute (
user) and forwards the request and response objects to theProfileservlet (mapped to/profile). - Profile Page: The
Profileservlet retrieves theuserattribute from the request and displays a "Welcome to your profile, [username]" message. - Failed Login: If the credentials do not match, the
Loginservlet prints an error message and then includes theindex.htmlcontent back into the response, effectively re-rendering the login page with an error.
DemoApp6
├── src/main/java
│ └── (default package)
│ ├── login.java # Servlet for handling authentication
│ └── profile.java # Servlet for the user profile page
└── src/main/webapp
├── WEB-INF
│ └── web.xml # Deployment Descriptor
└── index.html # The HTML login page
To run this project, you will need:
- Java Development Kit (JDK) 8 or higher.
- A servlet container like Apache Tomcat 10.x or higher.
- An IDE like Eclipse or IntelliJ IDEA (optional, but recommended).
Steps:
- Clone or download this repository.
- Build the project into a WAR (Web Application Archive) file.
- Deploy the generated
.warfile to your Apache Tomcat (or other servlet container)webappsdirectory. - Start the Tomcat server.
- Open your web browser and navigate to
http://localhost:8080/DemoApp6/(the port and context path may vary depending on your server configuration).
Use the following credentials to log in:
- Username:
priyanshu - Password:
123