Creating Google Cloud project in Eclipse

Today, I installed Google Cloud sdk on my ubuntu OS and then created a Cloud project in Eclipse.

  • Firstly, you need to create an account in Google cloud platform( https://cloud.google.com/) where you need to link your credit/debit card.
  • Then install Google cloud sdk in your systems. Follow the installation steps from this link, for respective OS. I just ran few commands for Ubuntu OS.
  • After successful installation, open your Eclipse.
  • Go to Help -> Eclipse Marketplace. A dialog appears.
  • Search for ‘google cloud’ and click Go. Install Google Cloud Tools which appears in search results.Screenshot from 2017-07-22 10-38-39

 

  • After the installation of tools, an icon for Google cloud platform appears in Menu bar.
  • Click that icon and select Create New Project -> Maven-based Google app engine standard java project.
  • Fill the Group ID and Artifact ID. Check the App engine API in the Libraries to add to build path section.Screenshot from 2017-07-22 10-52-20
  • Select Hello World template in next section.  Then click Finish.Screenshot from 2017-07-22 10-54-57
  • After the project is created in Eclipse, come to Google cloud platform.
  • Create a new project.Screenshot from 2017-07-22 10-58-41
  • After project is created, come back to eclipse.
  • Select the Project you created. Click on Google cloud platform icon in Menu bar and click on Deploy to app Engine Standard.
  • A dialog appears showing all your projects in Cloud platform. Select the one which you created now in google cloud platform.
  • Click Deploy.
  • Your project will be deployed on cloud.

Integration of API.AI with Facebook messenger

It was interesting thing which I explored today. I integrated my agent which I created previous day, with Facebook messenger.

It was not at all difficult. Firstly, you need to create a Facebook page related to the work which your agent does. For example, if you created an agent for table reservation in a restaurant, you need to create a page in Facebook related to that only. Once you are done get started just follow this. Believe it’s easy if you follow every step carefully.

I was stuck in a problem though. In Facebook, I didn’t add my phone number. When you will go through the tutorial for which I provided the link earlier; at the step Create a Facebook App, I clicked Create App Id button by filling the app name of my choice. Then after security check, it redirected to Facebook to add phone number. I was doing as it was written over there for the confirmation of phone number. But I didn’t receive any confirmation code. I repeated this 2-3 times but nothing happened. I was doing this all in laptop. Then, at last I added number using Facebook app in phone and that solved the issue without the need of confirmation code 🙂 .  So, it’s better that you add phone number in Facebook before proceeding to integration.

A verse with API.AI

Today, I got to know a very new topic and it was api.ai. I have seen chat bots in various websites specifically the course enquiry websites. I wondered at that time, how they can reply so fast. That thing is possible through Artificial intelligence.

API.AI provides you a bot(agent) to communicate with humans using natural language. Be it any website, whosoever visits, have queries regarding the same domain. A lot of messages are flooded in the website. Handling so many messages and giving quick replies is not that possible and efficient by humans that’s why an agent is trained in such a way as if a human to human interaction is there.

Very good explanation is provided in the official docs- https://api.ai/docs/getting-started/building-your-first-agent.

Welcome the world of AI 🙂

Spring MVC

Hello viewers ! Today, I learnt about Spring MVC framework which is much used widely. We have three important parameters in any web application-

  • Model: Application data represented as POJO/Bean class.
  • View: Front-end showing data after execution of business logic
  • Controller: Which actually performs the business operations

Spring MVC allows the loose coupling between them and allows to manage them independently.

As, I have discussed the advantages of using Maven this blog, so I will tell you how to integrate both of them i.e. Spring MVC and Maven.

  • Create a Maven project

I have told about this in my previous blog. Just go through that.

  • Add the required dependencies in pom.xml

You need to include three dependencies- Spring core, Spring web, Spring webmvc. Go to https://mvnrepository.com/ and add the dependencies for three of them in pom.xml.

Second thing, Maven allows you to specify the version of any dependency in separate tag . You can specify the version variable name as I have specified as and used this in required dependencies as a variable. This gives  a great advantage of changing the version for new release at single place rather than going to every dependency and modifying. Cool! 😎

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.mitaly</groupId>
  <artifactId>SpringMVC</artifactId>
  <packaging>war</packaging>
  <version>0.0.1-SNAPSHOT</version>
  <name>SpringMVC Maven Webapp</name>
  <url>http://maven.apache.org</url>

   <properties>
  	<spring-version>4.3.9.RELEASE</spring-version>
  </properties>

  <dependencies>

    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>

       <!-- https://mvnrepository.com/artifact/org.springframework/spring-core -->
	<dependency>
	    <groupId>org.springframework</groupId>
	    <artifactId>spring-core</artifactId>
	    <version>${spring-version}</version>
	</dependency>

	<!-- https://mvnrepository.com/artifact/org.springframework/spring-web -->
	<dependency>
	    <groupId>org.springframework</groupId>
	    <artifactId>spring-web</artifactId>
	    <version>${spring-version}</version>
	</dependency>

    <!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
	<dependency>
	    <groupId>org.springframework</groupId>
	    <artifactId>spring-webmvc</artifactId>
	    <version>${spring-version}</version>
	</dependency>

  </dependencies>
  <build>
    <finalName>SpringMVC</finalName>
  </build>
</project>

 

  • Check the project structure

Once you have created the Maven project, see the project structure. If your project structure is something like the shown below then you need to modify this.

projectStrOld

Actually, Java Resources here only contains src/main/resources and Libraries. Though it should contain src/main/java where we create our beans and controller.

If you have this problem, go through this stackoverflow answer. Then project structure will look something like this.

projectStrNew

  • Create index.jsp page

Create the index page where you will write the code which needs to be submitted to Controller which will process the data.

See, the action is hello. This is  actually the pattern which you specify for particular controller method which needs to be called. We’ll see that later.

<html>
<body>
<h2>Spring MVC</h2>
<form action="hello" method="post">
		Enter Name

		<input type="text" name="name">

		Enter Message

		<input type="text" name="message">

		<input type="submit" value="Enter">
	</form>

</body>
</html>

 

  • Create the controller class ‘HelloWorld.java’

As previously we had Servlets for receiving the user data and process that. Now, we have a simple class which acts as Controller. Use the @Controller annotation for that.

Define the @RequestMapping annotation for the business method to be executed. Here, we provide the mapping name to be /hello. Remember we used the form action to be hello. This means when form is submitted this method is executed.

Through @RequestParam we are getting the form input fields’ value.

The method must return ModelAndView object. In this object we specify all the data that needs to be transferred to view helloworld.jsp. We passed this view name in constructor of ModelAndView but we need not to specify .jsp, the reason for this we’ll see later.

package com.mitaly;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class HelloWorld {

	@RequestMapping("/hello")
	public ModelAndView showMessage(
			@RequestParam(value = "name", required = false, defaultValue = "World") String name,
			@RequestParam(value = "message", required = false, defaultValue = "Message") String message) {

		ModelAndView mv = new ModelAndView("helloworld");
		mv.addObject("message", message);
		mv.addObject("name", name);
		return mv;
	}
}

 

  • Create the view file ‘helloworld.jsp’

Create a folder named views, though it can be named anything. Create a jsp file and the ModelAndView object’s data.

<%@ page language="java" contentType="text/html; charset=UTF-8"     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>Insert title here</title>
</head>
<body>
	Welcome, ${name}

	Message : ${message}
</body>
</html>

 

  • Configure web.xml

Even if we don’ create any Servlet, a servlet known as DispatcherServlet gets created when application is loaded. We provide the entry for this servlet in web.xml file.  This servlet class is responsible for invoking the required controller class or more specifically which business method in the controller class.

An xml file named dispatcher-servlet.xml is created in WEB-INF directory whose entry has to be specified in web.xml as context parameter.

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">

	<display-name>Archetype Created Web Application</display-name>

	<servlet>
		<servlet-name>dispatcher</servlet-name>
		<servlet-class>
			org.springframework.web.servlet.DispatcherServlet
		</servlet-class>
		<load-on-startup>1</load-on-startup>
	</servlet>

	<servlet-mapping>
		<servlet-name>dispatcher</servlet-name>
		<url-pattern>/</url-pattern>
	</servlet-mapping>

	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>/WEB-INF/dispatcher-servlet.xml</param-value>
	</context-param>

									<listener>
										<listener-class>
			org.springframework.web.context.ContextLoaderListener
		</listener-class>
	</listener>
</web-app>

 

  • Create and configure the ‘dispatcher-servlet.xml’

Here , we need to specify context:component-scan which is nothing but the package name where DispatcherServlet will search for the required Controller.

There’s entry of InternalResourceViewResolver class which is a view resolver whose task is to pick and show the desired view. We here, specify prefix of view file as directory in which it is contained and suffix as .jsp. Remember, at the time of specifying the view name in ModelAndView constructor, there was no need of specifying .jsp because in this file we specify the suffix as .jsp.

<beans xmlns="http://www.springframework.org/schema/beans" 	xmlns:context="http://www.springframework.org/schema/context" 	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 	xsi:schemaLocation="http://www.springframework.org/schema/beans 	http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 	http://www.springframework.org/schema/context 	http://www.springframework.org/schema/context/spring-context-3.0.xsd">

	<context:component-scan base-package="com.mitaly" />
        <bean 		class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix">
			<value>/WEB-INF/views/</value>
		</property>
		<property name="suffix">
			<value>.jsp</value>
		</property>
	</bean>
</beans>

This was all till now, but not the end 😉 . Now, I have to merge all the concepts like Hibernate, JDBC, AOP, Services etc. in this.

Maven

Today, I came across one new term Maven. I am going to share what I learnt about it.

Introduction to Maven:

If we are to create a project say a web application, there are lots of jars need to be added, example for Hibernate, Spring, Servlet, Driver etc. Sometimes in future, we wish to replace our jar with latest version. It becomes very difficult to search for particular jar in the pool of so many jar files.

Second problem arises when in our project some dependencies are to be added and some of the dependencies, say, are dependent on few more dependencies.  It that case also, it becomes very difficult to manage all such dependencies and adding them in such a way such that there is no mismatch of versions.

Maven solves all such problem and much more also. It is an automated build tool. Maven is also used for project management. It generates reports for the project on its own.


Integration of Maven with eclipse:

    1. Open Eclipse. Do, New-> Maven Project. Make your Java EE perspective is on.
    2. A window will open as shown below. Just click Next.1
    3. In the Next screen, select catalog as ‘Internal’ and select the archetype which you require. I am creating a web application, so I have selected maven-archetype-webapp. Click Next.Archetype is project structure basically. In the introduction, I mentioned that maven is used as project management tool. Here, there comes its application. Maven will automatically create the project structure which you specify as archetype; like for web application, it will create src, webapp, WEB_INF, web.xml etc.2
    4. In next screen, fill the Group Id and Artifact Id. Group Id is reverse of domain name. Artifact Id is name of output of project(jar, war, ear etc. For Web Application, output is war file).  Click Finish.3
    5. Now, project is created. Expand your project structure and expand the target folder. You will see a file named pom.xml.POM(Project Object Model), is an XML file containing details of the project like project name, version, package type, dependencies, Maven plugins, etc.Switch to its source view, you will see something like this.4
    6. You must have noticed an error in your project in project structure. Go to src -> main -> webapp. Open the error containing file, index.jsp(in most cases). When you will hover the cursor on red cross in file(at first line), it says-“The superclass “javax.servlet.http.HttpServlet” was not found on the Java Build Path”.This error comes because Servlet dependency is not included in the project. Just add the dependency in pom.xml. Then press Ctrl+S.5

You will see error has gone. Write your Web application code and enjoy with maven 🙂

If you want to go ahead with Maven and learn more about it, go for this video tutorial.