[jsp] Create a sample login page using servlet and JSP?

I developed a sample login page for validating username and password. If the user gives correct credentials the page will navigate to some other page, else return a message from the servlet to the same login page.

I have enclosed the sample code here:

FirstJSP.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>`
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org   /TR/html4/loose.dtd">`
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
        <title>Insert title here</title>`
    </head>
    <body>
        <form action="Login" method="post">
            <input type="text" name="Name"/><br>
            <input type="password" name=Pass><br><br><br>
            <input type="submit" value="submit"/><br>
            <%if(request.getAttribute("message")!=(""))
            out.println(request.getAttribute("message"));
            else
            out.println("");%>
            <h1>Hi This is JSP sample code </h1>
            <font color="blue" size="25">
            <marquee>Please login to Work with JSP</marquee></font>
            <%java.text.DateFormat df = new java.text.SimpleDateFormat("MM/dd/yyyy"); %>
            <h1>Current Date: <%= df.format(new java.util.Date()) %> </h1>
        </form>
    </body>
</html>

And servlet code, Login.java:

package com.controller;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class Login extends HttpServlet {
    private static final long serialVersionUID = 1L;
    
    public Login() {
        super();
    }
    
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String username = request.getParameter("Name");
        String password = request.getParameter("Pass");
        response.setContentType("text/html");
        if(username.equals("sugan") && password.equals("raj")) {
            response.getWriter().println("<html><body><Marquee>Welcome to JSP!!!</marquee></body></html>");                
        } 
        else {                
            String message = "OOps!!! Invalid Username/Password";
            request.setAttribute("message", message);
            request.getRequestDispatcher("/FirstJSP.jsp").forward(request, response);                
        }
    }
}

It works fine, but while loading the login page it automatically displays Null. Then if the user gives the wrong credentials, it displays the actual message. How do I disable the Null message during run time?

This question is related to jsp servlets

The answer is


You're comparing the message with the empty string using ==.

First, your comparison is wrong because the message will be null (and not the empty string).

Second, it's wrong because Objects must be compared with equals() and not with ==.

Third, it's wrong because you should avoid scriptlets in JSP, and use the JSP EL, the JSTL, and other custom tags instead:

<c:id test="${!empty message}">
    <c:out value="${message}"/>
</c:if>

As I can see, you are comparing the message with the empty string using ==.

Its very hard to write the full code, but I can tell the flow of code - first, create db class & method inide that which will return the connection. second, create a servelet(ex-login.java) & import that db class onto that servlet. third, create instance of imported db class with the help of new operator & call the connection method of that db class. fourth, creaet prepared statement & execute statement & put this code in try catch block for exception handling.Use if-else condition in the try block to navigate your login page based on success or failure.

I hope, it will help you. If any problem, then please revert.

Nikhil Pahariya


You aren't really using the doGet() method. When you're opening the page, it issues a GET request, not POST.

Try changing doPost() to service() instead... then you're using the same method to handle GET and POST requests.

...