Heading

Wednesday, March 5, 2014

Web Application in java

Hi Friends

If any one interested to build simple web application in java then go through the posts from

Web Application in Java with JDBC, Servlets, JSp and MYSQL DataBase-1

to

Web Application in Java with JDBC, Servlets, JSp and MYSQL DataBase-14

Then you will idea how to make a simple Web Application.

Hope these articles helps you to improve yourself.

Web Application in Java with JDBC, Servlets, JSp and MYSQL DataBase-14

 Login. java

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author SDSTAFF
 */
   import java.io.*;
   import java.sql.*;
   import javax.servlet.*;
   import javax.servlet.http.*;
   public class Login extends HttpServlet {
    String username="";
    String password="";
    String mail="";
    Connection con=null;
    Statement st=null;
    Statement st1=null;
    ResultSet rs=null;
    ResultSet rs1=null;
    RequestDispatcher rd=null;
    HttpSession sn=null;
    PrintWriter out=null;
    public void doPost(HttpServletRequest req, HttpServletResponse res) throws IOException,ServletException {
        username = req.getParameter("user");
        password = req.getParameter("pass");
        System.out.println("username");
        HttpSession sn = req.getSession(true);
        sn.setAttribute("eid",username);
        res.setContentType("text/html");
        out = res.getWriter();
        sn = req.getSession(true);
        sn.setAttribute("username",username);
        RequestDispatcher rd;
      
                try {
            Class.forName("com.mysql.jdbc.Driver");
            con = DriverManager.getConnection("jdbc:mysql://localhost:3306/employee","root","password");
            st = con.createStatement();
            rs = st.executeQuery("select * from userlogin where userid='"+username+"' && passid='"+password+"'");
        if(rs.next())
            {
               
                st1=con.createStatement();
                rs1=st1.executeQuery("select email from userlogin where userid='"+username+"'");
                while(rs1.next())
                {
                mail=rs1.getString("email");
                sn = req.getSession(true);
                sn.setAttribute("emai",mail);
                System.out.println(mail);
                }
                rd=req.getRequestDispatcher("User_Home.jsp");
            }
            else {
                rd=req.getRequestDispatcher("Login_Fail.jsp");
                //out.println("welcome");
            }
            rd.forward(req,res);
        } catch(Exception e2) {
            //System.out.println("Exception : "+e2.toString());
              out.println(e2);
        }
    }
}

Web Application in Java with JDBC, Servlets, JSp and MYSQL DataBase-13

CSS File:

/*
    Document   : First
    Created on : Feb 25, 2014, 3:48:30 PM
    Author     : SDSTAFF
    Description:
        Purpose of the stylesheet follows.
*/

root {
    display: block;
}

            a:link {color:#cc00ff;text-decoration:none;}
            a:hover{color: blue;background-color: azure;}
            a:visited{color: palegoldenrod;}
            body
            {
                background: grey;
                padding-left:5%;
                width: 80%;
            }
            div
            {
                background-color: snow;
            }
            #heading
            {
                margin-left: 10%;
                text-align: center;
                font-size: 20px;
            }          
            #heading1
            {
                margin-left: 10%;

            }
            table,input
            {
                margin-left: 25%;
            }
            #site
            {
                margin-left: 10%;
                text-align: center;
                font-family: cursive;
                font-size: xx-large;
                background: ghostwhite;
            }
            #site1
            {
                margin-left: 10%;
                text-align: center;
                font-family: cursive;
                font-size: larger;
                background: ghostwhite;
                margin-bottom: 5%;
              
            }


Web Application in Java with JDBC, Servlets, JSp and MYSQL DataBase-12

User Home Tabs:

Add Products Page:

<%--
    Document   : Add_Product
    Created on : Feb 25, 2014, 12:31:33 PM
    Author     : SDSTAFF
--%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Product Page</title>
                 <link rel="stylesheet" type="text/css" href="First.css">
    </head>
    <body>

         <p id="site">
           G C K Products
        </p>
        <div id="heading">
            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href="Add_Product.jsp">Add Product</a> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;  <a href="View_Product.jsp">View Product</a> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <a href="Logout.jsp"> Logout</a>
        </div>   
                 <p align="right"> Hello <%=session.getAttribute("eid")%></p>
                
                 <p id="heading">Enter Product Details Here</p>
        <form id="form1" name="form1" method="post" action="Product_Register.jsp">
            <table style="">
            <tr>
                <td> Product Id:</td>
                <td> <input type="text" name="productid" id="pid"/></td>
            </tr>
            <tr>
                <td> Product Name:</td>
                <td> <input type="text" name="productname" id="pname"/></td>
            </tr>
            <tr>
                <td> Product Cost:</td>
                <td> <input type="text" name="productcost" id="pcost"/></td>
            </tr>
            <tr>
                <td> <input type="submit" value="Add Product" name="submit1"/></td>
                <td> <input type="reset" value="Reset" name="submit2"/></td>
            </tr>
            <tr>
                      <td height="48" colspan="2">  <%
                                                       String message=request.getParameter("Message");
                                                       if(message!=null && message.equalsIgnoreCase("success"))
                                                       {
                                                               out.println("<font color='red' size='+1'><blink>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Product Added Successfully !</blink></font>");
                                                       }
                                               %> &nbsp;
                      </td>
            </tr>
        </table>
        </form>
                  <div id="site1" >
            <p>
                Copy Rights @ G C K Solutions-2014.
            </p>
        </div>
    </body>
</html>
 

Web Application in Java with JDBC, Servlets, JSp and MYSQL DataBase-11

User Home Tabs:

Log Out Page:
<%--
    Document   : Logout
    Created on : Feb 24, 2014, 3:23:50 PM
    Author     : SDSTAFF
--%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Logout Successfully</title>
        <link rel="stylesheet" type="text/css" href="First.css">
    </head>
    <body>
       
        <%
           HttpSession hs=request.getSession(false);
           if(hs!=null)
           {
               hs.invalidate();
               RequestDispatcher rd=request.getRequestDispatcher("Login_Page.jsp");
               rd.forward(request, response);
               return;
           }
          
          
        %>
        <h1>Hello User you are Logged Out Successfully.</h1>
        For Login<a href="Login_Page.jsp"> Click Here</a>
        To goto Home Page<a href="Home_Page.html">Click Here</a>
    </body>
</html>
 

Web Application in Java with JDBC, Servlets, JSp and MYSQL DataBase-10

 User Home Tabs:

View Registered Products Page:

<%--
    Document   : View_Product
    Created on : Feb 25, 2014, 1:14:13 PM
    Author     : SDSTAFF
--%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@ page import="java.sql.*"%>
<%@ page import="java.io.*"%>
<%@ page import="java.util.*"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Product Page</title>
                 <link rel="stylesheet" type="text/css" href="First.css">
    </head>
    <body>
        <%
           HttpSession hs=request.getSession(false);
           if(hs==null)
           {
               RequestDispatcher rd=request.getRequestDispatcher("Login_Page.jsp");
               rd.forward(request, response);
               return;
           }
          
          
        %>
         <p id="site">
           G C K Products
        </p>
        <div id="heading">
            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href="Add_Product.jsp">Add Product</a> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;  <a href="View_Product.jsp">View Product</a> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <a href="Logout.jsp"> Logout</a>
        </div>   
                 <p align="right"> Hello <%=session.getAttribute("eid")%></p>
   
        <p id="heading"> Product Details Are</p>
        <table>
            <tr>
                <td>
                    <b> Product ID</b>
                </td>
                <td>
                    <b> Product Name</b>
                </td>
                <td>
                    <b> Product Cost</b>
                </td>
               
            </tr>
            <%!
                String id;
                String name ;
                String cost;
            %>
           
            <%
                Connection con = null;
                Statement st = null;
                ResultSet rs = null;

               

                try{
                    Class.forName("com.mysql.jdbc.Driver");
                    con = DriverManager.getConnection("jdbc:mysql://localhost:3306/employee","root","password");
                    st=con.createStatement();
                    //st.execute("select  product values('"+a+"','"+f+"','"+g+"')");
                    rs=st.executeQuery("SELECT * FROM  `employee`.`product` LIMIT 0, 10");
                    while(rs.next())
                    {
                        id=rs.getString(1);
                        name=rs.getString(2);
                        cost=rs.getString(3);
                   %>

            
            <tr>
                <td>
                    <b> <%= id %></b>
                </td>
                <td>
                    <b> <%= name %></b>
                </td>
                <td>
                    <b> <%= cost %></b>
                </td>
            </tr>
            <%
               }

            %>
            <%
                 }
                    catch(Exception ex)
                    {
                            out.println(ex);
                    }  
                    finally
                    {
                        con.close();
                    }
              %>
        </table>
                <div id="site1" >
            <p>
                Copy Rights @ G C K Solutions-2014.
            </p>
        </div>
    </body>
</html>

Web Application in Java with JDBC, Servlets, JSp and MYSQL DataBase-9

User Home Tabs:

Production Registration :  This jsp file helps us to get the values from Add_Product .jsp and helps us to store in the DB.

<%--
    Document   : Product_Register
    Created on : Feb 25, 2014, 12:48:48 PM
    Author     : SDSTAFF
--%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@ page import="java.sql.*"%>
<%@ page import="java.io.*"%>
<%@ page import="java.util.*"%>
<%
           HttpSession hs=request.getSession(false);
           if(hs==null)
           {
               RequestDispatcher rd=request.getRequestDispatcher("Login_Page.jsp");
               rd.forward(request, response);
               return;
           }
          
          
        %>
<%
Connection con = null;
Statement st = null;
ResultSet rs = null;

String a = request.getParameter("productid");
String f = request.getParameter("productname");
String g = request.getParameter("productcost");

try{
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/employee","root","password");
st=con.createStatement();
st.executeUpdate("insert into product values('"+a+"','"+f+"','"+g+"')");
response.sendRedirect("Add_Product.jsp?Message=success");

%>

<%
}
catch(Exception ex)
{
    out.println(ex);
}
%>

Web Application in Java with JDBC, Servlets, JSp and MYSQL DataBase-8

Login Fail Page:

<%--
    Document   : Login_Fail
    Created on : Feb 24, 2014, 3:20:30 PM
    Author     : SDSTAFF
--%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Login Fail</title>
                 <link rel="stylesheet" type="text/css" href="First.css">
    </head>
    <body>
        <h1>You Entered Wrong Details........ Login Failed.</h1>
        To Login <a href="Login_Page.jsp">Click Here</a>
    </body>
</html>

Web Application in Java with JDBC, Servlets, JSp and MYSQL DataBase-7

Login Success Page(User Home page):

<%--
    Document   : User_Home
    Created on : Feb 24, 2014, 3:19:36 PM
    Author     : SDSTAFF
--%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Home Page</title>
         <link rel="stylesheet" type="text/css" href="First.css">
    </head>
    <body>
         <%
           HttpSession hs=request.getSession(false);
           if(hs==null)
           {
               RequestDispatcher rd=request.getRequestDispatcher("Login_Page.jsp");
               rd.forward(request, response);
               return;
           }
          
          
        %>
       
         <p id="site">
           G C K Products
        </p>
        <div id="heading">
            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href="Add_Product.jsp">Add Product</a> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;  <a href="View_Product.jsp">View Product</a> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <a href="Logout.jsp"> Logout</a>
        </div>   
                 <p align="right"> Hello <%=session.getAttribute("eid")%></p>
                
                 <div id="site1" >
            <p>
                Copy Rights @ G C K Solutions-2014.
            </p>
        </div>
    </body>
</html>

Web Application in Java with JDBC, Servlets, JSp and MYSQL DataBase-6

Login Page :

<%--
    Document   : Login_Page
    Created on : Feb 24, 2014, 1:52:42 PM
    Author     : SDSTAFF
--%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Login Page</title>
     <link rel="stylesheet" type="text/css" href="First.css">
    </head>
    <body>
        <p id="site">
           G C K Products
        </p>
        <div id="heading">
            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href="Home_Page.html">Home</a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href="Login_Page.jsp">Login</a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <a href="Register_Page.jsp">Register</a>
       
        </div>
        <div id="heading1">
              
               <form id="form1" method="post" action="logme">
                      <p>&nbsp;                      </p>
                      <table width="370" height="173" border="0" >
                        <tr>
                          <th width="144" align="left" valign="middle" scope="row"><table width="147" height="75" border="0">
                              <tr>
                                <th width="139" height="29" align="center" valign="middle" class="style22" scope="row">LOGIN</th>
                              </tr>
                              <tr>
                                <th height="40" align="center" valign="middle" scope="row"><span class="style22">PASSWORD</span></th>
                              </tr>
                          </table></th>
                          <th width="210" align="left" valign="middle" scope="row"><table width="209" height="86" border="0">
                              <tr>
                                <td width="200" height="40"><p>
                                    <input name="user" type="text" id="user" size="32" height="25" />
                                </p></td>
                              </tr>
                              <tr>
                                <td height="40"><p>
                                    <input name="pass" type="password" id="pass" size="32" height="25" hspace="25" />
                                </p></td>
                              </tr>
                              <tr>
                                  <td>
                                      <input type="submit" value="Sign-In" />
                                  </td>
                                 
                              </tr>
                          </table></th>
                        </tr>
                      </table>

                    </form>
               </div>
                <footer>
        <div id="site1" >
            <p>
                Copy Rights @ G C K Solutions-2014.
            </p>
        </div>
        </footer>
    </body>
</html>
 

Web Application in Java with JDBC, Servlets, JSp and MYSQL DataBase-5

Registration.java:


import java.io.*;
   import java.sql.*;
   import javax.servlet.*;
   import javax.servlet.http.*;
   public class Registration extends HttpServlet {
    Connection con=null;
    Statement st=null;
    ResultSet rs=null;
    RequestDispatcher rd=null;
    public void doPost(HttpServletRequest req, HttpServletResponse res) throws IOException,ServletException {
    String userid= req.getParameter("userid");
        String passid= req.getParameter("passid");
        String username= req.getParameter("username");
    String email= req.getParameter("email");
        String mob= req.getParameter("mob");
        System.out.println(mob+" "+ userid+" "+passid+" "+username+" "+email);

    RequestDispatcher rd;
        try {
            Class.forName("com.mysql.jdbc.Driver");
            con = DriverManager.getConnection("jdbc:mysql://localhost:3306/employee","root","password");
            st = con.createStatement();
             System.out.println(con);      
             int add=st.executeUpdate("insert into userlogin values('"+userid+"','"+passid+"','"+username+"','"+email+"','"+mob+"')");
          System.out.println(add);
          res.sendRedirect("Register_Page.jsp?Message=success");
         
           // rd=req.getRequestDispatcher("Login_Page.jsp");
          //rd.forward(req,res);
        } catch(Exception e2) {
             rd=req.getRequestDispatcher("Register_Page.jsp");
        }
    }
}

Web Application in Java with JDBC, Servlets, JSp and MYSQL DataBase-4

Registration Page:

<%--
    Document   : Register_Page
    Created on : Feb 24, 2014, 1:57:02 PM
    Author     : SDSTAFF
--%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Register Page</title>
             <link rel="stylesheet" type="text/css" href="First.css">
    </head>
    <body>
        <%
           HttpSession hs=request.getSession(false);
           if(hs==null)
           {
               RequestDispatcher rd=request.getRequestDispatcher("Login_Page.jsp");
               rd.forward(request, response);
               return;
           }
          
          
        %>      
        <p id="site">
           G C K Products
        </p>
        <div id="heading">
            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href="Home_Page.html">Home</a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href="Login_Page.jsp">Login</a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <a href="Register_Page.jsp">Register</a>
       
        </div>   
        <br/>
        <div id="heading1">
               <table  border="1" >
                <tr>
                  <td  valign="middle" bgcolor="#A8C5D7">
                  <form name="form1" action="accept" method="post" id="form1" onsubmit=" ">
                    <table border="0" align="center">
                      <tr>
                        <td  align="center" valign="bottom">User Name </td>
                        <td  align="center" valign="bottom"><input type="text" name="userid" id="userid" size="30"/></td>
                      </tr>
                      <tr>
                        <td align="center" valign="bottom"><span class="style28 style34">Password</span></td>
                        <td align="center" valign="bottom"><input type="password" name="passid" id="passid" size="30"/></td>
                      </tr>
                      <tr>
                        <td align="center" valign="bottom"><span class="style28 style34">Name</span></td>
                        <td align="center" valign="bottom"><input type="text" name="username" id="username" size="30" /></td>
                      </tr>
                      <tr>
                        <td align="center" valign="bottom"><span class="style28 style34">Email</span></td>
                        <td align="center" valign="bottom"><input type="text" name="email" id="email" size="30" /></td>
                      </tr>
                      <tr>
                        <td align="center" valign="bottom"><span class="style28 style34">Mobile No </span></td>
                        <td align="center" valign="bottom"><input name="mob" id="mob" type="text" size="30" /></td>
                      </tr>
                      <tr>
                          <td>
                              <%
                                                       String message=request.getParameter("Message");
                                                       if(message!=null && message.equalsIgnoreCase("success"))
                                                       {
                                                               out.println("<font color='red' size='+1'><blink>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Registered Successfully !</blink></font>");
                                                       }
                                %>
                          </td>
                      </tr>
                    </table>
                    <p class="aligncenter style4">
                      <input type="submit" name="submit" value="Submit" onclick=" "/>
                    </p>
                   </form></td>
                </tr>
              </table>
        </div>
                   
                            <footer>
        <div id="site1" >
            <p>
                Copy Rights @ G C K Solutions-2014.
            </p>
        </div>
        </footer>
    </body>
</html>
 

Web Application in Java with JDBC, Servlets, JSp and MYSQL DataBase-3

HomePage.html
 Design Home Page as shown below

Important is just the Home Register and Login links
As shown below. Give link to that using anchor tag and use href attribute.
      Home      Login       Register