Pages

Saturday, September 22, 2012

Simple login tutorial for web application using MySQL

Hey my dear friends. In this post we are going to create simple login application.Before we start check following resources do you have.
  1. Netbeans IDE or you can use any other Java IDE.
  2. JDBC mysql connector - you can download it from here.
  3. MySQL Server - you can download it from here.(Im not here describe how to install MySQL Server and Database creating part)
01. Open Netbeans IDE. File>New Project.

02. Select Java Web under Categories and Web Application under the Projects  and Click Next.

03. Give your Project Name in the Project Name box. In mine SimpleLogin and click Next>Finish.
     Do you have any problem up to now creating new webproject please refer the first four step in
     this post.

04. Create following form in index.jsp.  

<html>
    <head>
    </head>
    <body>
        <form name="loginform" method="post" action="loginbean.jsp">
            <br><br>
            <table align="center"><tr><td><h2>Welcome to School of Java</h2></td></tr></table>
            <table width="300px" align="center" style="border:1px solid #000000;background-color:#FE9A2E;">
                <tr><td colspan=2></td></tr>
                <tr><td colspan=2> </td></tr>
                <tr>
                    <td><b>Login Name</b></td>
                    <td><input type="text" name="userName" value=""></td>
                </tr>
                <tr>
                    <td><b>Password</b></td>
                    <td><input type="password" name="password" value=""></td>
                </tr>
                <tr>
                    <td></td>
                    <td><input type="submit" name="Submit" value="Submit"></td>
                </tr>
                <tr><td colspan=2> </td></tr>
            </table>
        </form>
    </body>
</html>



05. Now right click web pages in your Project(SimpleLogin) and choose New>JSP. Give a filename
      as loginbean and click Finish. In loginbean.jsp edit as follows.

<%@ page language="Java" import="java.sql.*" %>
<html>
    <head><title>School of JAVA</title></head>
    <body>
        <jsp:useBean id="db" scope="request" class="logbean.LoginBean" >
            <jsp:setProperty name="db" property="userName" value="<%=request.getParameter("userName")%>"/>
            <jsp:setProperty name="db" property="password" value="<%=request.getParameter("password")%>"/>
        </jsp:useBean>
        <jsp:forward page="hello">
            <jsp:param name="username" value="<%=db.getUserName()%>" />
            <jsp:param name="password" value="<%=db.getPassword()%>" />
        </jsp:forward>
    </body>
</html>

06. Next right click web pages in your Project an choose New>JSP. Give a  filename as welcome
     and click Finish.  In welcome.jsp edit as follows.

<html>
<head>
<title>Welcome to School of JAVA</title>
</head> 
<body>
<br><br>
        <%
            if (session.getAttribute("username") != null && session.getAttribute("username") != "") {
            String user = session.getAttribute("username").toString();
        %>
        <center><h1>Welcome <b><%= user%></center>
        <%}%>
</body>
<html>

07. Next right click Source Packages in your Project an choose New>Java Package. Give a 
      Package Name as com.schoolofjava.bean and click Finish.



08. Right click on  com.schoolofjava.bean>New>Java Class.Give class name as LoginBean
     and click Finish.Add following codes to LoginBean class.

package com.schoolofjava.bean;

public class LoginBean{

    private String username;
    private String password;

    public String getUserName() {
        return username;
    }

    public void setUserName(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
}

09. Next right click Source Packages in your Project an choose New>Java Package. Give a 
     Package Name as com.schoolofjava.sql and clickFinish.



10. Right click on  com.schoolofjava.bean>New>Servlet.Give servlert name as Login and click
     Next.Select Add information to deployment descripter(web.xml) and click Finish.Add
     following codes to Login Servlet.


 Add following codes to Login Servlet.

 package com.schoolofjava.sql;

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

public class Login extends HttpServlet {

    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();

        System.out.println("MySQL Connect Example.");
        Connection conn = null;
        String url = "jdbc:mysql://localhost:3306/";
        String dbName = "user";
        String driver = "com.mysql.jdbc.Driver";
        String sqlUser = "root";
        String sqlPassword = "sql";

        String username = "";
        String password = "";
        String strQuery = "";
        Statement st = null;
        ResultSet rs = null;
        HttpSession session = request.getSession(true);

        try {
            Class.forName(driver).newInstance();
            conn = DriverManager.getConnection(url + dbName, sqlUser, sqlPassword);
            if (request.getParameter("username") != null && request.getParameter("username") != "" && request.getParameter("password") != null && request.getParameter("password") != "") {
                username = request.getParameter("username").toString();
                password = request.getParameter("password").toString();
                strQuery = "select * from user_info where username='" + username + "' and  password='" + password + "'";
                System.out.println(strQuery);
                st = conn.createStatement();
                rs = st.executeQuery(strQuery);
                int count = 0;
                while (rs.next()) {

                    session.setAttribute("username", rs.getString(2));
                    count++;
                }

                if (count > 0) {
                    response.sendRedirect("welcome.jsp");
                } else {
                    response.sendRedirect("index.jsp");
                }

            } else {
                response.sendRedirect("index.jsp");
            }
            System.out.println("Connected to the DB");
            conn.close();
            System.out.println("Disconnected from DB");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

Note: Change the url, dbName, sqlUser, sqlPassword with your Database URL, Database Name, MySQL username and MySQL passwordsetting.
  
10.Right click on Libaries>ADD JAR/Folder give your path to JDBC MySQL connector JAR file.


  
12. Now right click your project and choose Run.Now your Simple Login Application run in your web
     browser.It look like as follows.



 



 I hope this post will be helpful.See you in next post......

Thursday, September 20, 2012

Create your first web application in netbeans

01. Choose File>New Project 



  02. Select Java Web under Categories>Web Application under the
       Projects and Click Next


03. Give Project Name in the Project Name box. In mine FirstWebApplication
     and click Next(If you want to change Default Project location click Browse
     and select the location).



04. Select your server and click Finish.

  
05. Right click the source package node and chose New>Package



06. Enter Package Name and click Finish



07. Right click on your package and choose New>Java Class


 08. Enter Class Name and click Finish.


09. In the Source Editor Enter following code.

    String name;
   
    public SayHello() {
        name=null;
    }

10. Right click name and choose Refactor>Encapsulate Fields


11. Now we are going to create accessor(Getter & Setter) methods to field
      called name. Click Refactor.


12. Now your class look similar to the following.
 
 

13. Now go to the index.jsp and edit it as follows.


 14.Now right click web pages in Project window an choose New>JSP


15. Give a filename as response and click Finish.


16. Now go to the response.jsp and edit it as follows


 17. Now right click your project and choose Run.


Now your first Web Application run in your web browser.It look like as follows



Tuesday, September 11, 2012

How to install Netbeans IDE on ubuntu 12.04

01.Go to the netbeans downlod page and download the netbeans linux installer(like netbeans-7.1.2-ml-linux.sh).

02.Open Terminal(Ctrl+Alt+T).

03. Normally Terminal open with your home folder. Change the path to your netbeans installer is downloaded.

cd  /Download
04.Type the following code and press enter. Change netbeans-7.1.2-ml-linux.sh with your downloaded file name.

sudo chmod +x  netbeans-7.1.2-ml-linux.sh
05.Type your password. Then type the follwing code and press enter.Change netbeans-7.1.2-ml-linux.sh with your downloaded file name.

sudo ./netbeans-7.1.2-ml-linux.sh
06.Then install netbeans from GUI installer. Click customize and select Apache Tomcat as your runtime. Wait for the Installation to finish. Launching NetBeans IDE go to Applications -> Programming ->NetBeans IDE.


Note : Java is required to install nebeans. First install java and next install netbeans to your computer. This post is guide how to install java in ubuntu.

In next post we will see how to write your first web application in java using netbeans.

Tuesday, June 12, 2012

How to manual install Java on Ubuntu

In today’s post, I’m going to show you how to manual install Oracle Java (JDK) 7 on Ubuntu without using PPA. There are several ways to install Java on Ubuntu. In this post I'm going to show  you install Java on Ubuntu without using PPA.

1. Download Java Development Kit from this link from

2. After downloading jdk extract the downloaded package by running the commands
    below. This assumes that the package was downloaded in your Downloads folder.

tar -xvf /home/mahesh/Downloads/jdk-7u4-linux-i586.tar.gz






3. JDK 7 package is extracted into ./jdk1.7.0_04 directory. Now move the JDK 7 directory to /usr/lib

sudo mv /home/mahesh/Downloads/jdk1.7.0_04 /usr/lib/jvm/jdk1.7.0






4. Next run the following commands on terminal one by one

sudo update-alternatives --install "/usr/bin/java" "java" "/usr/lib/jvm/jdk1.7.0/bin/java" 1






sudo update-alternatives --install "/usr/bin/javac" "javac" "/usr/lib/jvm/jdk1.7.0/bin/javac" 1






sudo update-alternatives --install "/usr/bin/javaws" "javaws" "/usr/lib/jvm/jdk1.7.0/bin/javaws" 1






5. Next run the following command on terminal

sudo update-alternatives --config java






You will see output similar one below - choose the number of jdk1.7.0 - for example 3 in this list.

$sudo update-alternatives --config java
There are 3 choices for the alternative java (providing /usr/bin/java).

Selection Path Priority Status
————————————————————
* 0 /usr/lib/jvm/java-6-openjdk/jre/bin/java 1061 auto mode
1 /usr/lib/jvm/java-6-openjdk/jre/bin/java 1061 manual mode
2 /usr/lib/jvm/java-6-sun/jre/bin/java 63 manual mode
3 /usr/lib/jvm/jdk1.7.0/jre/bin/java 3 manual mode

Press enter to keep the current choice[*], or type selection number: 3
update-alternatives: using /usr/lib/jvm/jdk1.7.0/jre/bin/java to provide
/usr/bin/java (java)
*There have no list don't worry. It says no alternative jdk on your computer. It automatically updated current installed jdk.

6. Check the version of you new installation.







7. Run following code on terminal

sudo update-alternatives --config javac
sudo update-alternatives --config javaws







*There has no list don't worry. It says no alternative jdk on your computer. It automatically updated current installed jdk. But there hava a list to select alternative choose the number of jdk1.7.0

8. Now you finish install Oracle JDK on your computer.

ඇරඹුම

මෙය මගේ පළමු බ්ලොග් සටහන නොවෙයි. මම බ්ලොග් ලියන්න පටන් ගත්තේ මීට අවුරුදු දෙකකට විතර කලින්.හැබැයි ඒ  ලිපි දෙක තුනක් විතරයි. ලිවීම මට ඒ දවස් වලම එපා වුනා.

මම මහේෂ් ක්‍රිෂාන්ත. නැතිනම්  වැඩි දෙනෙක් දන්න විදිහට මයියා(අනිත් කාඩ් කියන්න ඔනේ නැහැනේ).දැන් වැඩිහරියක් ජීවත් වෙන්නේ කොළඹ. 2007  වසරේ වාරියපොල ශ්‍රී සුමංගල ජාතික පාසලෙන් වානිජ අංශයෙන් උසස්පෙල කරපු මම අද කොළඹ විශ්වවිද්‍යාලයේ පරිගණක අධ්‍යයනායතනයෙ (UCSC) හි BIT බාහිර උපාධිය හදාරන දෙවන වසර ශිෂ්‍යයෙක්.

ඊට අමතරව උසස් තාක්ෂණ ආයතනය මගින් පවත්වගෙන යන HNDIT(Higer National Diploma in Information Technology) පුර්ණ කාලීනව හදරා අවසන්.දැනට මම එහි පුහුණු කාලය සම්පූර්ණ කරමින් සිටින්නේ.

සිංහලෙන් වගේම ඉංග්‍රිසි වලිනුත් ලියන්න තමයි හිතාගන ඉන්නේ.හැබැයි ඉංග්‍රිසි ගැන ලොකු දනුමක් නම් නැහැ.වැරදි අඩුපාඩු ඕන තරම් තියෙන්න පුලුවන්.අපි තවම ඉගෙනගන්න පොඩි ළමයිනෙ නේද..වැඩි කතා නැතිව වැඩේ පටන් ගමු.බ්ලොග් එක සම්පුර්ණයෙන්ම වෙන් කරල තියෙන්න ජාවා වලට.කියවන්න වගේම කැමති නැත්තම් නොකියව ඉන්නත් ඕන කෙනෙකුට නිදහස තියෙනවා.කැමතිනම් විතරක් කමෙන්ට් එකක් දාලා ගියාට කමක් නැහැ.