Tuesday, December 18, 2012

count character for number

/**
 *
 */
package com;

/**
 * @author Boomiraj
 *
 */
public class CountChar {

    /**
     * @param args
     */
    static int count = 0;
    public static void main(String[] args) {
        onecountCharacter();
    }
    public static void onecountCharacter(){
        String[] ones = {"zero","one","two","three","four","five","six","seven","eight","nine"};
        String[] ten = {"ten","eleven","twelve","thriteen","fourteen","fifteen","sixteen","seventeen","eighteen","nineteen"};
        String[] tens = {"twenty","thirty","fourty","fifty","sixty","seventy","eighty","nighty"};
      
 for(int i = 0; i < 100; i++){
//            Check whether the number is not greater then ten
            if(i<10){
                count = count + ones[i].length();
                System.out.println(ones[i] + " : " + i + " : " + count);
            }
//            Check whether the number is not greater than 9 and less than 20
            if(i>9 && i<20){
                int j = 10;
                j = i - j; //
                count = count + ten[j].length();
                System.out.println(ten[j] + ":" + i + " : " + count);
                j++;
            }
//            Check whether then number is greater then 20
            if(i>19){
                int k = i/10-2;
                int j = i % 10;
               
                if(j == 0){
                    count = count + tens[k].length();
                    System.out.println(tens[k] + " : "  + count);
                } else{
                    count = count + tens[k].length() + ones[j].length();
                    System.out.println(tens[k] + ones[j] + ":" + i + " :" + count);
                   
                }
                   
               
            }
           
        }
        System.out.println(count);
       
    }

}

Saturday, December 15, 2012

print the sentence from paragraph

public static String printSentance(String str) {
        StringBuffer strBuffer = new StringBuffer();
        for (int i = 0; i < str.length(); i++) {
            if (str.charAt(i) != '.' ) {
                strBuffer.append(str.charAt(i));

            } else {
                System.out.println(strBuffer);
                strBuffer = new StringBuffer();
            }
        }
        return strBuffer.toString();
    }

count number of sentence in paragraph(sentence end with '.' or '?')

public static int countNoOfSentance(String str){
        StringBuffer strBuffer = new StringBuffer();
        for (int i = 0; i < str.length(); i++) {
            if (str.charAt(i) != '.' && str.charAt(i) != '?') {
                strBuffer.append(str.charAt(i));

            } else {
                ++count;
                strBuffer = new StringBuffer();
            }
        }
        count++;
        System.out.println(count);
        return count;
    }


split the sentance when capital letter occur

//split the sentence manually 

public static String getSplit(String str){
        char[] str_1 = {'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'};
        StringBuffer sbuffer = new StringBuffer();
        for(int i = 0; i<str.length();i++){
            for(int j = 0; j<str_1.length; j++){
               
                if(str.charAt(i) == str_1[j]){
                    sbuffer.append(" ");
                }
            }
            sbuffer.append(str.charAt(i));
        }
        return sbuffer;
    }

//split the sentence using API method
public static String getSplitByMethod(String str){
        StringBuffer sbuffer = new StringBuffer();
        for(int i = 0;i<str.length();i++){
            if(Character.isUpperCase(str.charAt(i))){
                sbuffer.append(" ").append(str.charAt(i));
            }else{
                sbuffer.append(str.charAt(i));
            }
        }
        return sbuffer.toString();
    }

Thursday, December 13, 2012

sendRedirect() vs include() vs forward()

sendRedirect(myUrl1) : you are sending back a response to the client and asking the browser to make a new request with the provided url, here myUrl1

include() : Say in servletA you call include(/path/to/myView.jsp) : you are asking the web container to include in the response of servletA the response of myView.jsp.
ServletA is in control, I mean responsible for generating the response to the client and you just include as well the response of another web component (e.g jsp)

forward() : say in servletA you call forward(path/to/myView.jsp): here servletA passes the control to myView.jsp to generated the response to the client. The client will see only the content from myView.jsp nothing from servletA. 


Original source from coderanch 

Application server vs Web server

Application Server:
          •  It provide the software application with services
          •  It serve both http protocol and RMI protocol
          • Most of the application server have web server as internal part of them. So application server can do all the web server work.
          • Application server used dynamic content.

Web Server :
          •  It used to response the http request
          • It serve only with http protocol
          • Web server used static content 

    • Examples of WebServer:
              • Apache HTTP Server is Web Server

Thursday, December 6, 2012

getter and setter method for username password

/**
 *
 */
package dao;

/**
 * @author Boomiraj
 *
 */
public class LoginBean {
   
    private String username;
    private String password;
    private String firstname;
    private String lastname;
    private boolean valid;
    /**
     * @return the username
     */
    public String getUsername() {
        return username;
    }
    /**
     * @param username the username to set
     */
    public void setUsername(String username) {
        this.username = username;
    }
    /**
     * @return the password
     */
    public String getPassword() {
        return password;
    }
    /**
     * @param password the password to set
     */
    public void setPassword(String password) {
        this.password = password;
    }
    /**
     * @return the firstname
     */
    public String getFirstname() {
        return firstname;
    }
    /**
     * @param firstname the firstname to set
     */
    public void setFirstname(String firstname) {
        this.firstname = firstname;
    }
    /**
     * @return the lastname
     */
    public String getLastname() {
        return lastname;
    }
    /**
     * @param lastname the lastname to set
     */
    public void setLastname(String lastname) {
        this.lastname = lastname;
    }
    /**
     * @return the valid
     */
    public boolean isValid() {
        return valid;
    }
    /**
     * @param valid the valid to set
     */
    public void setValid(boolean valid) {
        this.valid = valid;
    }

}

Valid username and password

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1" import="dao.LoginBean"%>
<!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>
    <center>
    <% LoginBean lb =  (LoginBean)session.getAttribute("currentSessionUser"); %>
    Welcome :<%= lb.getFirstname() %>
    FirstName :<%= lb.getFirstname() %>
    LastName  :<%= lb.getLastname() %>
    Password  :<%= lb.getPassword() %>
    </center>
</body>
</html>

Invalid usename password message using 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>
 <center> Sorry, you are not a registered user! Please sign up first </center>
</body>
</html>

Loginjsp

<%@ 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="LoginServlet" method="post">
        UserName : <input type = "text" name = "username" value=""><br>
        Password : <input type = "password" name = "password" value=""><br>
        <input type = "submit" value = "Login">
       
    </form>
   </body>
</html>

Login servlet

package servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import dao.LoginBean;
import doa.UserDOA;

/**
 * Servlet implementation class LoginServlet
 */
public class LoginServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
     * Default constructor.
     */
    public LoginServlet() {
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stubt
       
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        PrintWriter out = response.getWriter();
        LoginBean lbean = new LoginBean();
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        lbean.setUsername(username);
        lbean.setPassword(password);
       
            
        lbean = UserDOA.login(lbean);
       
        if(lbean.isValid()){
            HttpSession session = request.getSession(true);
             session.setAttribute("currentSessionUser",lbean);
             response.sendRedirect("RegisteredUser.jsp"); //logged-in page
           }else{
            //out.println("<h1> Username and Password Not Matched </h1>");
            response.sendRedirect("InvalidUser.jsp"); //error page
        }
         
         
               
    }

}

JDBC connection for verifying user name and password

/**
 *
 */
package doa;

import java.sql.*;

import dao.LoginBean;

/**
 * @author Boomiraj
 *
 */
public class UserDOA {

    /**
     *
     */
    public UserDOA() {
        // TODO Auto-generated constructor stub
    }
    static Connection con ;
    static ResultSet rs ;
    static Statement stmt ;
    public static LoginBean login(LoginBean lb) {
        //LoginBean lbean = new LoginBean();
        //System.out.println("Test");
        try {
            Class.forName("com.mysql.jdbc.Driver");
            try {
                con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","admin");
                stmt = con.createStatement();
                //lb.setUsername("Boomiraj");
                //lb.setPassword("boomi@123");
                String uname = lb.getUsername();
                String pword = lb.getPassword();
                System.out.println(uname);
                System.out.println(pword);
                rs = stmt.executeQuery("select * from login where UserName= '"+uname+"' and PASSWORD='"+pword+"' ");
                boolean more = rs.next();
                if(!more){
                    System.out.println(" Not Matched ");
                }
                if(more){
                    String fname = rs.getString("FirstName");
                    lb.setFirstname(fname);
                    String last = rs.getString("LastName");
                    lb.setLastname(last);
                    lb.setValid(true);
                         }
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
           
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
       
       
        
        return lb;
    }
   
   
}