Simple Login Application using JSP, Servlets and JDBC connectivity to MySQL - Deployment-Descriptor-JSP

Article Index

1 1 1 1 1 1 1 1 1 1 Rating 3.60 (66 Votes)

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<display-name>SimpleLoginApp</display-name>
  <servlet>
    <description>
    </description>
    <display-name>LogonServlet</display-name>
    <servlet-name>LogonServlet</servlet-name>
    <servlet-class>in.techfreaks.login.servlet.LogonServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>LogonServlet</servlet-name>
    <url-pattern>/servlet/LogonServlet</url-pattern>
   </servlet-mapping>
</web-app>



Login.jsp:
The JSP code is pretty straightforward. The call of Form POST to Servlet and use of c:out tag to display errorMsg from the servlet is shown in the below snippet.

<form name="frmLogin" method="POST" action="servlet/LogonServlet">
 <table border="1">
  <tbody>
    <tr>
      <td colspan="2"><c:out value="${errorMsg}"/> </td>
    </tr>
    <tr>


Home.jsp
The JSP is pretty straightforward. It uses the c:out exactly the same way as in Login.jsp to display the logged in username.

That completes our simple login application. You can test it by deploying the WAR file in Tomcat or any other favorite Application Server. The complete WAR along with the source code can be downloaded by clicking here.

Bugs are inevitable! There is a complaint that you can go the Home.jsp by directly typing the URL. Also, after you login and reach the Home.jsp, if you leave the session open for a long time and then refresh the username disappears. We leave the privilege of fixing the bug with you ;) However, your approach would be to add a check in Home.jsp for session variable “userName”. If not available, redirect to Login.jsp. If you have any queries or need assistance, feel free to add your comments in the section below.


Try similar article which extends to the login application and adds Registration functionality by clicking Simple Registration Application using JSP, Servlet and MySQL.

private Connection getConnection() throws Exception {

            Connection conn = null;

            try {

                  String url = "jdbc:mysql://localhost/"+DBNAME+"?user="+DB_USERNAME+"&password="+DB_PASSWORD;

                  Class.forName("com.mysql.jdbc.Driver");

                  conn = DriverManager.getConnection(url);

            } catch (SQLException sqle) {

                  System.out.println("SQLException: Unable to open connection to db: "+sqle.getMessage());

                  throw sqle;

            } catch(Exception e) {

                  System.out.println("Exception: Unable to open connection to db: "+e.getMessage());

                  throw e;

            }

            return conn;

      }


Joomla SEO by MijoSEF