Thursday, December 22, 2016

Basic R Programming Commands Part-1

R is a language and environment for statistical computing and graphics. It is a GNU project which is similar to the S language and environment which was developed at Bell Laboratories (formerly AT&T, now Lucent Technologies) by John Chambers and colleagues. R can be considered as a different implementation of S. There are some important differences, but much code written for S runs unaltered under R.
R provides a wide variety of statistical (linear and nonlinear modelling, classical statistical tests, time-series analysis, classification, clustering, …) and graphical techniques, and is highly extensible. The S language is often the vehicle of choice for research in statistical methodology, and R provides an Open Source route to participation in that activity.[Ref:www.r-project.org]
One of R’s strengths is the ease with which well-designed publication-quality plots can be produced, including mathematical symbols and formulae where needed. Great care has been taken over the defaults for the minor design choices in graphics, but the user retains full control.
R is available as Free Software under the terms of the Free Software Foundation’s GNU General Public License in source code form. It compiles and runs on a wide variety of UNIX platforms and similar systems (including FreeBSD and Linux), Windows and Mac OS [Ref:www.r-project.org].
Basic R programming Commands
1.   3+7   
This statement simply adds 3 with 7 and gives output as below
[1] 10

2. X<-3 nbsp="" span="">
This statement assigns value 3 to variable X. To see the value of x, you can just type x on the prompt.
> x
[1] 3     // this is the output.
3. X=3                            //Assigns value 3 to variable X
This is also another way of assigning value to a variable.
4.Y=c(3,4,5,6)                 //creating vector with four values
Here, the vector name is Y. and values are 3,4,5,6. You can see the values of Y by just typing Y on the command prompt.
5. y=c(1:50)                    // create a vector y containing values 1 to 50
The above vector y contains values from 1 to 50.
i.e.  z=(1,2,3) can also be written as          z=c(1:3)
6.c(y,7,Y)                       // it will place 7 between the results of two vectors
7.a<-sqrt a="" elements="" finding="" nbsp="" of="" root="" span="" square="" vector="" z="">
8.  a=c(1:4)                //this creates vector a with values 1,2,3,4
    b=c(1,10)              // this creates vector b with values 1 and 10
   a+b                              //adding two vectors. Here elements of one vector should be evenly                                         distributed to other vector. i.e . vector a has 4 elements which is evenly                                                 divisible by no of values in vector b. otherwise the addition will result in an                                           error
9.  1:20                             //displays numbers from 1 to 20
10.  pi:10                            // displays numbers from 3.141593 to 9.141593( incr. by 1)



Saturday, December 17, 2016

Creating Login and Registration page using JSP.


First create your table. let us assume the table is created as hereunder. (in mysql)

CREATE TABLE members
(
  id int(10) unsigned NOT NULL auto_increment,
  first_name varchar(45) NOT NULL,
  last_name varchar(45) NOT NULL,
email varchar(45) NOT NULL,
  uname varchar(45) NOT NULL,
  pass varchar(45) NOT NULL,
  regdate date NOT NULL,
  PRIMARY KEY  (`id`)
)

Now create a jsp page (in Eclipse) which can also contain html code. The below code
snippet can be used for login page.( name it as index.jsp). In fact any
name can be given to the page but i have named it as index.jsp.The code
written in the page is given below.

index.jsp


<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>LOGIN PAGE</title>
</head>
<body>
<br/>
<br/>
<form method="post" action="login.jsp">
<center>
<table border="1" width="50%" cellpadding="3">
<thead>
<tr>
<th colspan="2">Login Here</th>
</tr>
</thead>
<tbody>
<tr>
<td>User Name</td>
 <td><input type="text" name="uname" value="" /></td>
 </tr>
 <tr>
 <td>Password</td>
 <td><input type="password" name="pass" value="" /></td>
 </tr>
 <tr>
 <td><input type="submit" value="Login" /></td>
 <td><input type="reset" value="Reset" /></td>
 </tr>
 <tr>
 <td colspan="2">Yet Not Registered!! <a href="reg.jsp">Register Here</a></td>
 </tr>
 </tbody>
 </table>
 </center>
 </form>
 </body>
</html>


The above code snippet contains html tags which are used to create a login
page with two fields. One for username and one for password. Finally two buttons
are used and named as submit and reset as shown below.



Now create another jsp page and name it as login.jsp. Write the following code there.

login.jsp( This page is invoked when the user clicks submit button)

<%@ page import ="java.sql.*" %> <% String userid = request.getParameter("uname"); String pwd = request.getParameter("pass"); Class.forName("com.mysql.jdbc.Driver"); Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/dbname", "root", "give your password here"); Statement st = con.createStatement(); ResultSet rs; rs = st.executeQuery("select * from members where uname='" + userid + "' and pass='" + pwd + "'"); if (rs.next()) { session.setAttribute("userid", userid); response.sendRedirect("success.jsp"); } else { out.println("Invalid password try again"); } %>


success.jsp (This page is invoked if the login is successful) <% if ((session.getAttribute("userid") == null) || (session.getAttribute("userid") == "")) { %> You are not logged in
index.jsp
">Please Login <%} else { %> Welcome <%=session.getAttribute("userid")%> Log out <% } %>

logout.jsp (This page is invoked when the user clicks log out) <% session.setAttribute("userid", null); session.invalidate(); response.sendRedirect("index.jsp"); %>

reg.jsp (This page is invoked when the user clicks Register here Link)

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Registration Page</title>
</head>
 <body>
<form method="post" action="registration.jsp">
<center>
<table border="1" width="30%" cellpadding="5">
<thead>
<tr>
<th colspan="2">Enter Details Here</th>
</tr>
</thead>
<tbody>
<tr>
<td>First Name</td>
<td><input type="text" name="fname" value="" /></td>
</tr>
<tr>
<td>Last Name</td>
<td><input type="text" name="lname" value="" /></td>
</tr>
<tr>
<td>Email</td>
<td><input type="text" name="email" value="" /></td>
</tr>
<tr>
<td>User Name</td>
<td><input type="text" name="uname" value="" /></td>
</tr>
<tr>
<td>Password</td>
<td><input type="password" name="pass" value="" /></td>
</tr>
<tr>
<td><input type="submit" value="Submit" /></td>
<td><input type="reset" value="Reset" /></td>
</tr>
<tr>
<td colspan="2">Already registered!! <a href="index.jsp">Login Here</a></td>
</tr>
</tbody>
</table>
</center>
</form>
</body>
</html>

The above code will create registration page as shown below.



registration.jsp (This page is invoked when the user clicks submit button in registration page)


<%@ page import ="java.sql.*" %>
<%
    String user = request.getParameter("uname");    
    String pwd = request.getParameter("pass");
    String fname = request.getParameter("fname");
    String lname = request.getParameter("lname");
    String email = request.getParameter("email");
    Class.forName("com.mysql.jdbc.Driver");
    Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/dbname",
            "root", "give your password here");
    Statement st = con.createStatement();
   
    int i = st.executeUpdate("insert into members(first_name, last_name, email, uname, pass, regdate) values ('" + fname + "','" + lname + "','" + email + "','" + user + "','" + pwd + "', CURDATE())");
    if (i > 0) {
   
    response.sendRedirect("welcome.jsp");
   
    } else {
        response.sendRedirect("index.jsp");
    }
%>

welcome.jsp
Registration is Successful. Please Login Here Go to Login


Run your project. Before running the project make sure that eclipse is connected to the tomcat server.
Enjoy programming.