Java Technology - 2018

I have been learning Java Technology since 3 months. During this time, I have a chance to learn on different aspects of Java Technology. Here are what I have learned till now:
1) Core Java
2) JDBC Connection
     2.1) Connection with postgresql
     Version: Enter SELECT version(); in PGAdmin III
     "PostgreSQL 9.5.9, compiled by Visual C++ build 1800, 64-bit"
     2.2) Connection with Oracle
     When I enter: SELECT * FROM V$VERSION, Oracle Sql Developer shows me the version that I am using:
        Oracle Database 11g Express Edition Release 11.2.0.2.0 - 64bit Production
        PL/SQL Release 11.2.0.2.0 - Production
        "CORE 11.2.0.2.0 Production"
       TNS for 64-bit Windows: Version 11.2.0.2.0 - Production
       NLSRTL Version 11.2.0.2.0 - Production
    2.3) SQL Server
3) Servlets
4)JSP
5)JSTL
6) Hibernate Framework
   6.1) Hibernate Query Language
7)Spring Framework:
 7.1) Spring Framework - Basics
 7.2) Spring MVC Framework with JDBC Connection - Oracle/PostgreSql/SQL Server
 7.3) Spring MVC Framework with Maven
 7.4) Spring MVC with Maven and Hibernate

------------------------------------------------------------------------------------------------------------------------
[Note: I was trying to execute an insert operation for registration purpose with Spring MVC Maven and JDBC Connection using Oracle. It didn't work directly. I have to download Maven, set path variables for maven and install the maven jar files using command prompt. But it didn't work. So I used postgresql for database. I didn't have to download anything or set any path variables. I just go directly to use whatever is there and it worked successfully.

--------------------------------------------------------------------------------------------------------------------------
Ok. Now I will start a Library Project.
-Spring MVC Maven JDBC - postgreSql
Steps:
 1) Create Home Page: When the project Loads- It should show Home Page like this:

fig: (1)

Criteria: When project loads with http://localhost:8080/LibraryProject/ or http://localhost:8080/LibraryProject/index whatever it should load HomePage as shown above.

For this:
 First step is to create a Maven Project - LibraryProject

pom.xml should be:

<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.spring.practice</groupId>
<artifactId>LibraryProject</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>LibraryProject Maven Webapp</name>
<url>http://maven.apache.org</url>
<properties>
<spring.version>4.2.5.RELEASE</spring.version>
<junit.version>4.11</junit.version>
<jdk.version>1.8</jdk.version>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<dependency>

<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>

</dependency>
<dependency>

<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring.version}</version>

</dependency>
<dependency>

<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>

</dependency>
<dependency>

<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>

</dependency>
<dependency>

<groupId>postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>9.1-901.jdbc4</version>

</dependency>

<dependency>

<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>

</dependency>

</dependencies>
<build>
<finalName>LibraryProject</finalName>
</build>
</project>

web.xml should be:

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
<display-name>Archetype Created Web Application</display-name>
<servlet>
<servlet-name>dispatcher</servlet-name>

<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

</servlet>

<servlet-mapping>

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

</servlet-mapping>
</web-app>

dispatcher-servlet should be:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">

<context:annotation-config />
<context:component-scan base-package="com.spring.practice" />
<bean id="jspViewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />

<property name="prefix" value="/" />
<property name="suffix" value=".jsp" />
</bean>

<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
p:location="/WEB-INF/jdbc.properties" />
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${jdbc.driverClassName}" />
<property name="url" value="${jdbc.databaseurl}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</bean>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"></property>
</bean>

<tx:annotation-driven />
<mvc:annotation-driven />

</beans>

The above dispatcher-servlet should be in the WEB-INF folder where web.xml resides.

Create jdbc.properties file in WEB-INF folder because it is defined in dispatcher-servlet in which folder it should be which is: p:location="/WEB-INF/jdbc.properties" like this:

jdbc.driverClassName=org.postgresql.Driver
jdbc.databaseurl=jdbc:postgresql://localhost:5432/MyDB
jdbc.username=(username of postgresql)
jdbc.password=(password of postgresql)

Create a controller class:
HomeController:

@Controller
public class HomeController {

@RequestMapping(value = "/")
public ModelAndView mainPage() {
return new ModelAndView("index"); // done
}

@RequestMapping(value = "/index")
public ModelAndView indexPage() {
return new ModelAndView("index"); // done
}

}

As shown above: HomeController should be defined as @Controller. As we see whether / or /index is called, it will try to check index.jsp as shown above. "index" in ModelAndView means there should be index.jsp page defined in webapp. It should be jsp page which is defined in <property name="suffix" value=".jsp" /> in dispatcher-servlet.xml. You may define other suffix here.

Now create index.jsp in webapp:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1" isELIgnored="false"%>
<html>
<head>
<title>Home Page</title>
</head>
<body>
<h2>Library</h2>
<h3>Welcome to my Library</h3>
<font color="blue">${message}</font>
<p>
<a href="${pageContext.request.contextPath}/mainLoginPage">Click here to Login and Registration.</a>

</p>
</body>
</html>

Note: You may not need ${message}. This is used to send any message from controller.

If we run this project in Tomcat server, it will show the index page as shown in fig. (1).

As you see here href is /mainLoginPage. So the controller will search for mainLoginPage in Controller if you click on "Click here to Login and Registration". Now if you click this hyperlink, you need to get the following page:

fig: (2)

As you see mainLoginPage will be loaded when you click "Click here to Login and Registration" from homepage. The controller will see if mainLoginPage is present or not and what to do if it exists. So the controller need to add the following code:

@RequestMapping(value = "/mainLoginPage")
public ModelAndView mainLoginPage() {
return new ModelAndView("mainLogin");
}

This will look for mainLogin.jsp. Here is how mainLogin.jsp should be:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1" isELIgnored="false"%>
<html>
<head>
<title>Home Page</title>
</head>
<body>
<h2>Hello World!</h2>
<h3>Welcome to SpringMVC JDBC Integration Project - Library Project</h3>
<font color="red">${message}</font>

<p>
<a href="${pageContext.request.contextPath}/adminLoginPage"> Click here for Admin Login.</a>
<br/>
<a href="${pageContext.request.contextPath}/librarianLoginPage"> Click here for Librarian Login.</a>

</p>

<p>
<a href="${pageContext.request.contextPath}/adminRegistrationPage"> Click here for Admin Registration.</a>
<br/>
<a href="${pageContext.request.contextPath}/librarianRegistrationPage"> Click here for Librarian Registration.</a>

</p>

<p>
<a href="${pageContext.request.contextPath}/index"> HomePage</a>
<br/>
</p>
</body>
</html>

If you click " Click here for Admin Registration.", it will look for adminRegistrationPage in the controller. So first let us finish how Library Admin Registration should proceeds.

Here is what you need to add to the home controller:

@RequestMapping(value = "/adminRegistrationPage", method = RequestMethod.GET)
public ModelAndView adminRegistrationPage() {
ModelAndView mvObject = new ModelAndView("adminRegistration");
mvObject.addObject("command", new AdminUser());
return mvObject;
}

adminRegistrationPage page should look like this:

fig (3)


This will send AdminUser() object to the adminRegistration.jsp.

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1" isELIgnored="false"%>
<%@taglib uri="http://www.springframework.org/tags/form" prefix="form" %>
<!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>Library Admin Registration Page</title>
</head>
<body>
<h2>New Library Admin</h2>
<p>Here you can add a new Library Admin.</p>
<form:form method="POST"
action="${pageContext.request.contextPath}/adminRegistrationNext">
<table>
<tr>
<td><form:label path="firstName">Firstname:</form:label></td>
<td><form:input path="firstName"></form:input></td>
</tr>
<tr>
<td><form:label path="lastName">Lastname:</form:label></td>
<td><form:input path="lastName"></form:input></td>
</tr>
<tr>
<td><form:label path="userName">Username:</form:label></td>
<td><form:input path="userName"></form:input></td>
</tr>
<tr>
<td><form:label path="password">Password:</form:label></td>
<td><form:input path="password"></form:input></td>
</tr>
<tr>
<td><form:label path="email">Email:</form:label></td>
<td><form:input path="email"></form:input></td>
</tr>
<tr>
<td><form:label path="address">Address:</form:label></td>
<td><form:input path="address"></form:input></td>
</tr>
<tr>
<td><form:label path="phone">Phone Number:</form:label></td>
<td><form:input path="phone"></form:input></td>
</tr>
<tr>
<td><input value="Submit" type="submit"></td>
<td></td>
</tr>
</table>
</form:form>
<p>
<a href="${pageContext.request.contextPath}/index">Home page</a>
<br/>
<a href="${pageContext.request.contextPath}/mainLoginPage">Login/Registration page</a>
</p>
</body>
</html>

All the input of the form is sent to adminRegistrationNext step. So the controller should add the following code in adminRegistrationNext to save the value obtained from the form to the postgresql database.

@RequestMapping(value = "/adminRegistrationNext", method = RequestMethod.POST)
public ModelAndView addAdminUser(
@ModelAttribute AdminUser adminUser) {
ModelAndView mvObject = new ModelAndView("mainLogin");
libraryAdminService.addLibraryAdmin(adminUser);
String message = "Admin was successfully added";
mvObject.addObject("message", message);
return mvObject;
}

The value from the form is now sent to libraryAdminService.  So you should add what libraryAdminService is:

@Autowired
LibraryAdminService libraryAdminService;

After adding this, you should define what LibraryAdminService is. Add following code to define LibraryAdminService.java 

package com.spring.practice.service;

import com.spring.practice.pojo.AdminUser;

public interface LibraryAdminService {
public void addLibraryAdmin(AdminUser adminUser);

}

This is an interface. Which has prototype that need to be defined using LibraryAdminServiceImpl.

package com.spring.practice.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.spring.practice.dao.LibraryAdminDao;
import com.spring.practice.pojo.AdminUser;

@Service
public class LibraryAdminServiceImpl implements LibraryAdminService {

@Autowired
private LibraryAdminDao libraryAdminDao;

public void addLibraryAdmin(AdminUser adminUser) {
libraryAdminDao.addLibraryAdmin(adminUser);

}

}

Note: The service is like Waiter. It has order taken from the customer. It will be sent to the Chef in the kitchen. Here Chef is  LibraryAdminDao.

So, let's define how LibraryAdminDao should look like:

package com.spring.practice.dao;

import com.spring.practice.pojo.AdminUser;

public interface LibraryAdminDao {
public void addLibraryAdmin(AdminUser adminUser);

}

This has same receipe as the Waiter have. So same function. And it needs to be implemented by LibraryAdminDaoImpl.

package com.spring.practice.dao;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import com.spring.practice.pojo.AdminUser;

@Repository
public class LibraryAdminDaoImpl implements LibraryAdminDao {
@Autowired
JdbcTemplate jdbcTemplate;

public void addLibraryAdmin(AdminUser adminUser) {
String query = "SELECT Max(ID) FROM LIBRARY_ADMIN";
int id = jdbcTemplate.queryForObject(query, Integer.class);
query = "insert into LIBRARY_ADMIN (ID,FIRSTNAME,LASTNAME,USERNAME,PASSWORD,EMAIL,ADDRESS,PHONE)"

+ " values(?, ?, ?, ?, ?, ?, ?, ?)";

jdbcTemplate.update(
query,
new Object[] { ++id, adminUser.getFirstName(),
adminUser.getLastName(), adminUser.getUserName(),
adminUser.getPassword(), adminUser.getEmail(),
adminUser.getAddress(), adminUser.getPhone() });
}
}

There is LIBRARY_ADMIN table in postgresql database. These all classes are @Autowired. So everything runs internally. If everything goes smoothly, it will return to the controller and print message: String message = "Admin was successfully added"; with the help of  mainLogin.jsp page. Now you are in the mainLogin.jsp from where you can register for Library Librarian or Login for Library Admin or Login for Library Librarian as shown in fig(2).

Comments

  1. It is very good blog and useful for students and developer,Thanks for sharing

    Java Online Training Bangalore

    ReplyDelete

Post a Comment

Popular posts from this blog

Jersey JAX RS Jar

Tomcat Installation Steps for Windows & add tomcat to eclipse

REST API