Skip to main content

Featured

Game Development Tutorials In Android

Game Development Tutorials In Android Hi Guys,                 We are shortly started Game Development Tutorials in Android Studio. So everyone supports me and share this blogs to your friends. Guys, At this time everyone is freely available to source code and we are provide a video for step by step game developments. you are learn free and feel free to support us. Guys, if you have any question to me, so feel free to comment us. I will try to answer all the question as long as possible.                                                     Thank's                                                                          ...

Servlet Login Demo Program In Java

Servlet Login Demo Program In Java 

Servlet : Servlet is a Platform Independent Server Side Web Technology for dynamic contents to the client's. It's Run on the principles of Request And Response Paradigms. 


Get the Netbeans IDE for Developing an Real-Time Application.

Before we are going to Develop AutoLogin Project using Database or Java Servlet. Click On the all Advertisement, Available on this sites.

Now Let's Start : 

                                     "index.jsp"

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!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=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
        <h1>Hello World!</h1>
    </body>

</html>


sun-web.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE sun-web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Application Server 9.0 Servlet 2.5//EN" "http://www.sun.com/software/appserver/dtds/sun-web-app_2_5-0.dtd">
<sun-web-app error-url="">
  <context-root>/autologin</context-root>
  <class-loader delegate="true"/>
  <jsp-config>
    <property name="keepgenerated" value="true">
      <description>Keep a copy of the generated servlet class' java code.</description>
    </property>
  </jsp-config>

</sun-web-app>

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app 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">
    <servlet>
        <servlet-name>Login</servlet-name>
        <servlet-class>a.Login</servlet-class>
    </servlet>
    <servlet>
        <servlet-name>SetMe</servlet-name>
        <servlet-class>a.SetMe</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>Login</servlet-name>
        <url-pattern>/l</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
        <servlet-name>SetMe</servlet-name>
        <url-pattern>/s</url-pattern>
    </servlet-mapping>
    <session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>
    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
        </welcome-file-list>
    </web-app>

                               "Login.java"
package a;

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class Login extends HttpServlet
{
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
/* Setting MIME type for HTTP header */
response.setContentType("text/html");

/* Obtaining the output stream */
PrintWriter out = response.getWriter();

/* Memory variables that will hold the username and password */
String username = "";
String password = "";

/* Retrieving all unexpired cookies */
Cookie[] cookies = request.getCookies();

/* If cookies were retrieved */
if(cookies != null)
{
/* Traversing across all the cookies and extracting the username and password from the ValidUser and ValidPassword cookies respectively. */
for(int i=0; i < cookies.length; i++) 
{
Cookie thisCookie = cookies[i];

if (thisCookie.getName().equals("ValidUser")) 
{
username = thisCookie.getValue();
}
else if(thisCookie.getName().equals("ValidPassword"))
{
password= thisCookie.getValue();
}
}
}

/* Generating the actual Login form user interface */
out.println("<HTML><HEAD><TITLE>Login Form</TITLE></HEAD>");
out.println("<BODY><FORM ACTION='/autologin/s' METHOD='get' NAME='frmLogin'>");
out.println("<TABLE><TR><TD ALIGN='center'><IMG SRC='/MyWebApplication/security.jpg' WIDTH='64' HEIGHT='64' BORDER='0'>");
out.println("<BR>Welcome to Sharanam Shah's Website!<BR>Use a valid username and password <BR>");
out.println("to gain access to Sharanam Shah's Website</TD><TD BORDERCOLOR='#DEEFF9'>");
out.println("<TABLE WIDTH='25%' BORDER='1' ALIGN='center' CELLPADDING='3' CELLSPACING='1' BORDERCOLOR='#000000'>");
out.println("<TR BORDERCOLOR='#92CAEB' BGCOLOR='white'><TD COLSPAN='2'>Member Login</TD></TR>");
out.println("<TR BORDERCOLOR='#E6F3FB'><TD ALIGN='right'>Username:</TD><TD>");
out.println("<INPUT NAME='txtusername' TYPE='text' TABINDEX='1' VALUE='" + username + "' SIZE='15' MAXLENGTH='15'></TD>");
out.println("</TR><TR BORDERCOLOR='#E6F3FB'><TD ALIGN='right'>Password:</TD><TD>");
out.println("<INPUT NAME='txtpassword' TYPE='password' TABINDEX='2' VALUE='" + password + "' SIZE='15' MAXLENGTH='15'></TD></TR>");
out.println("<TR BORDERCOLOR='#E6F3FB'><TD COLSPAN='2' ALIGN='right'>");
out.println("<INPUT NAME='chkrem' TYPE='checkbox' VALUE='REMEMBER' TABINDEX='3'>REMEMBER ME</TD></TR>");
out.println("<TR BORDERCOLOR='#E0EEF7'><TD COLSPAN='2' ALIGN='right'>");
out.println("<INPUT NAME='submit' TYPE='submit' VALUE='Sign In' TABINDEX='4'></TD></TR>");
out.println("</TABLE></TD></TR></TABLE></FORM></BODY></HTML>");

/* Closing the output stream */
out.close();
}
}

                                                    "SetMe.java"

package a;

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;
import javax.sql.*;

public class SetMe extends HttpServlet
{
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
/* Setting MIME type for HTTP header */
response.setContentType("text/html");

/* Obtaining the output stream */
PrintWriter out = response.getWriter();

/* Creating a Connection and Statement object for later use */
Connection con = null;
Statement stmt;

try 
{
/* Memory variables that hold Db login information */
String username = "root";
String password = "sct2306";
String url = "jdbc:mysql://localhost/bookdb";

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

con = DriverManager.getConnection(url,username,password);

/* If the connection is successful */
if(con != null)
{
out.println("<BR>Successfully connected to MySQL server using TCP/IP...<BR>");
stmt = con.createStatement();
/* A query that will determine if the username and password captured are valid */
String query = "SELECT * FROM login WHERE userid = '" + request.getParameter("txtusername").toString() + "' AND password ='" + request.getParameter("txtpassword").toString() + "'";
/* Executing the query using the ResultSet object */
ResultSet rs = stmt.executeQuery(query);

/* If the resultset holds no records */
if(rs.next() == false) 
{
out.println("<BR>Sorry. Invalid Login. Please try again.");
}
/* If the resultset holds records */
else
{
out.println("<BR>Successful Login. Welcome to the world of Servlets.");

/* If the chkrem i.e. Remember Me check box is checked on */
if(request.getParameter("chkrem") != null)
{
/* A cookie to hold the username is created */
Cookie returnLoginUser = new Cookie("ValidUser", request.getParameter("txtusername").toString());
/* Setting how much time in seconds should elapse before the cookie expires */
returnLoginUser.setMaxAge(36000);
/* Adding cookie to the Set-Cookie response header by means of the addCookie method of HttpServletResponse */
response.addCookie(returnLoginUser);

/* A cookie to hold the password is created */
Cookie returnLoginPassword = new Cookie("ValidPassword", request.getParameter("txtpassword").toString());
/* Setting how much time in seconds should elapse before the cookie expires */
returnLoginPassword.setMaxAge(36000);
/* Adding cookie to the Set-Cookie response header by means of the addCookie method of HttpServletResponse */
response.addCookie(returnLoginPassword);
}
/* If the chkrem i.e. Remember Me check box is checked off */
else
{
/* Assigning a null value */
Cookie returnLoginUser = new Cookie("ValidUser", "");
/* Deleting the cookie */
returnLoginUser.setMaxAge(0);
/* Adding cookie to the Set-Cookie response header by means of the addCookie method of HttpServletResponse */
response.addCookie(returnLoginUser);

/* Assigning a null value */
Cookie returnLoginPassword = new Cookie("ValidPassword", "");
/* Deleting the cookie */
returnLoginPassword.setMaxAge(0);
/* Adding cookie to the Set-Cookie response header by means of the addCookie method of HttpServletResponse */
response.addCookie(returnLoginPassword);

out.println("<BR><BR>You did not choose to remember the login information. Hence you will need to enter login details the next time you visit us.");
}
}
}
/* Closing the output stream */
out.close();
}
catch(Exception e) 
{
System.err.println("Exception: " + e.getMessage());
}
}

Compile And Run on this Project for Better Understanding.

If this information is important to you , and you want to , I will continue writing some more details About Java, Servlet, Android Programming Language, so click on the advertising, available on this page. This is motivate me for writing some more useful stuff, Thank's 

Comments