0% found this document useful (0 votes)
10 views4 pages

Internals 2 Web

The document provides a complete implementation of a login page using HTML and a Java Servlet. It includes a servlet that processes login requests, checks hardcoded credentials, and responds accordingly. Additionally, it contains JSP examples for printing 'Hello World' and the current date and time, as well as a JSP for the login module.

Uploaded by

angelin272004
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views4 pages

Internals 2 Web

The document provides a complete implementation of a login page using HTML and a Java Servlet. It includes a servlet that processes login requests, checks hardcoded credentials, and responds accordingly. Additionally, it contains JSP examples for printing 'Hello World' and the current date and time, as well as a JSP for the login module.

Uploaded by

angelin272004
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

CREATE A SERVLET FOR LOGIN PAGE

HTML PROGRAM:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Login Page</title>
</head>
<body>
<center>
<h1>Login Page</h1>
<form action="NewServlet" method="post">
<label>Enter Username:</label>
<input type="text" name="username" required><br><br>

<label>Enter Password:</label>
<input type="password" name="password" required><br><br>

<input type="reset" value="Reset">


<input type="submit" value="Login">
</form>
</center>
</body>
</html>

SERVLET PROGRAM:
import java.io.IOException;
import java.io.PrintWriter;
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("/NewServlet") // Servlet Mapping


public class NewServlet extends HttpServlet {

// Handle both GET & POST requests


protected void doGet(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
processRequest(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
processRequest(request, response);
}

private void processRequest(HttpServletRequest request, HttpServletResponse response)


throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();

// Retrieve form data


String username = request.getParameter("username");
String password = request.getParameter("password");

// Hardcoded username & password check


if ("admin".equals(username) && "12345".equals(password)) {
out.println("<html><body>");
out.println("<h1>Hello, " + username + "!</h1>");
out.println("</body></html>");
} else {
out.println("<html><body>");
out.println("<h1>login failed</h1>");
out.println("</body></html>");
}
}
}

OUTPUT: failed

JSP THAT PRINTS HELLO WORLD(Create jsp file)

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"


%>
<!DOCTYPE html>
<html>
<head>
<title>Hello World JSP</title>
</head>
<body>
<h1><% out.println("Hello World!"); %></h1>
</body>
</html>

CREATE JSP THAT PRINTS CURRENT DATE AND TIME:


<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"
%>
<!DOCTYPE html>
<html>
<head>
<title>Current Date and Time</title>
</head>
<body>
<h1>Current Date and Time:</h1>
<h2>
<%
java.util.Date date = new java.util.Date();
out.println(date);
%>
</h2>
</body>
</html>

CREATE A JSP FOR LOGIN MODULE:


<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"
%>
<!DOCTYPE html>
<html>
<head>
<title>Login Page</title>
</head>
<body>
<h2>Login</h2>
<form action="validate.jsp" method="post">
<label>Username:</label>
<input type="text" name="username" required><br><br>

<label>Password:</label>
<input type="password" name="password" required><br><br>

<input type="submit" value="Login">


</form>
</body>
</html>

You might also like