Skip to content

Commit

Permalink
first seminar materials
Browse files Browse the repository at this point in the history
  • Loading branch information
minicuts committed Sep 20, 2015
1 parent 09c9d92 commit 59cb79b
Show file tree
Hide file tree
Showing 15 changed files with 210 additions and 31 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
An open-sourced Java EE project created for teaching purposes of a subject PA165 at Faculty of Informatics, Masaryk University.

Folder tasks contains tasks that should be carried out in each of the seminars.
20 changes: 20 additions & 0 deletions hello-java7/.classpath
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" output="target/classes" path="src/main/java">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="output" path="target/classes"/>
</classpath>
23 changes: 23 additions & 0 deletions hello-java7/.project
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>hello-java7</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.m2e.core.maven2Builder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
<nature>org.eclipse.m2e.core.maven2Nature</nature>
</natures>
</projectDescription>
3 changes: 3 additions & 0 deletions hello-java7/.settings/org.eclipse.core.resources.prefs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
eclipse.preferences.version=1
encoding//src/main/java=UTF-8
encoding/<project>=UTF-8
5 changes: 5 additions & 0 deletions hello-java7/.settings/org.eclipse.jdt.core.prefs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8
org.eclipse.jdt.core.compiler.compliance=1.8
org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning
org.eclipse.jdt.core.compiler.source=1.8
4 changes: 4 additions & 0 deletions hello-java7/.settings/org.eclipse.m2e.core.prefs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
activeProfiles=
eclipse.preferences.version=1
resolveWorkspaceProjects=true
version=1
34 changes: 34 additions & 0 deletions hello-java7/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?xml version="1.0"?>
<project
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>cz.fi.muni.pa165</groupId>
<artifactId>eshop-parent</artifactId>
<version>1.0</version>
</parent>
<groupId>cz.fi.muni.pa165</groupId>
<artifactId>hello-java7</artifactId>
<version>1.0-SNAPSHOT</version>
<name>hello-java7</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.2</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
</dependencies>
</project>
21 changes: 21 additions & 0 deletions hello-java7/src/main/java/cz/fi/muni/pa165/App.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package cz.fi.muni.pa165;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
* Hello world!
*
*/
public class App
{
public static void main( String[] args )
{
List<String> messages = new ArrayList<>();
messages.add("Hello" );
messages.add("World" );
System.out.println( messages);
}
}
14 changes: 14 additions & 0 deletions hello-tom-web/README.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
This sample shows how to use Maven tomcat7 plugin. It is not inteded to show how your projects should look like!

The project contains 2 items: TestServlet and index.jsp.

The TestServlet is a standard servlet coded according to Servlet 3.0 specification, the index.jsp is just static content.

As you can see in the pom, only thing needed to use the plugin is to add plugin tomcat7-maven-plugin. In order to develop Servlet, I also had to add dependency on Servlet API (some jar that contains e.g. WebServlet annotation). It is important to note here that this Servlet API is provided for each Servlet container and should NOT be packaged together with the application. Thats why I set it to scope <scope>provided</scope>. By having it at this scope, the depdendecy is used only during the development and after packaging and deploying to server it uses the server's Servlet API.

To run this maven exapmle:
1) mvn clean install tomcat7:run
2) access static content on: http://localhost:8080/my-webapp/
3) access the servlet on: http://localhost:8080/my-webapp/hello

Should you have any questions regarding this sample, send me an e-mail to [email protected]
29 changes: 29 additions & 0 deletions hello-tom-web/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<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.acme</groupId>
<artifactId>my-webapp</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<name>my-webapp Maven Webapp</name>
<url>http://maven.apache.org</url>
<build>
<plugins>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.1</version>
</plugin>
</plugins>
<finalName>my-webapp</finalName>
</build>

<dependencies>
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-servlet-api</artifactId>
<version>7.0.42</version>
<scope>provided</scope>
</dependency>
</dependencies>
</project>
25 changes: 25 additions & 0 deletions hello-tom-web/src/main/java/cz/pa165/TestServlet.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package cz.pa165;

import java.io.IOException;

import javax.servlet.GenericServlet;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebServlet;

@WebServlet(value="/hello")
public class TestServlet extends GenericServlet {

/**
*
*/
private static final long serialVersionUID = 1L;

@Override
public void service(ServletRequest arg0, ServletResponse arg1)
throws ServletException, IOException {
arg1.getWriter().println("Hello World!");
}

}
8 changes: 8 additions & 0 deletions hello-tom-web/src/main/webapp/WEB-INF/web.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<!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>

</web-app>
5 changes: 5 additions & 0 deletions hello-tom-web/src/main/webapp/index.jsp
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<html>
<body>
<h2>Hello World!</h2>
</body>
</html>
11 changes: 5 additions & 6 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
<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">
<?xml version="1.0" encoding="UTF-8"?>
<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>cz.fi.muni.pa165</groupId>
<artifactId>eshop-parent</artifactId>
Expand Down Expand Up @@ -41,8 +39,9 @@
<module>eshop-service</module>
<module>eshop-api</module>
<module>eshop-fe</module>
</modules>
<module>hello-java7</module>
</modules>

<dependencies>
</dependencies>
</project>
</project>
37 changes: 12 additions & 25 deletions tasks/seminar01.md
Original file line number Diff line number Diff line change
@@ -1,34 +1,21 @@
## Seminar 01 Tasks
This first seminar tries to be as minimalistic as possible. You will work with in-memory database and just one Main Class (MainJavaSe.java). You will be asked to implement parts of methods. Then you will run the method by modifying the Main method. For example if I want to try method corresponding to task07(), then I will modify the main method this way:
```java
emf = Persistence.createEntityManagerFactory("javaSeUnit");
// BEGIN YOUR CODE
task06();
// END YOUR CODE
emf.close();
```
** Task 01 ** Open IDE. Main IDE for this course is Netbeans 7.3.1. This IDE is installed in B130 and accessible through modules. Use the following commands to run IDE (its good to firstly delete temporal directories ~/.netbeans and ~/.personal_domain . Create hello world Java application and run it using the IDE.
```
module add netbeans-7.3.1-loc
netbeans &
```

**Task 01** Your first task is to locate and download JPA 2.1 specification (JSR 338). It is a PDF file.

**Task 02** Try to run cz.fi.muni.pa165.MainJavaSe from NetBeans
** Task 02 ** Find out which version of Maven is installed on your machine. You MUST use terminal where you added the module from Task 01. Notes: Maven was covered on the lecture. The module from Task 01 adds the Maven on your system path. This means that you must add module from Task 01 in EVERY terminal that you will be using for running netbeans or for running any Maven commands. Other notes: your local maven repo is in /tmp/maven-repo-$LOGIN.

**Task 03** Run the main method also from command line (using Maven exec:java
target) see documentation for Maven here http://mojo.codehaus.org/exec-maven-plugin/java-mojo.html
** Task 03 ** Create hello world Java application using Maven from command line. Use "archetype" plugin and goal "generate". Then modify the printed text (from command line) to Hello PA165. Run this application from commandline using Maven exec plugin goal java

**Task 04** Add configuration property to persistence.xml so that Hibernate writes all generated SQL statements to console. See A.2 section of
https://docs.jboss.org/hibernate/orm/4.3/devguide/en-US/html_single/ . Rerun **Task 03** to confirm that you can now see the SQL
** Task 04 ** Create an acount on https://github.com/. Create a repository there. Import the hello world application from Task 03 to this repository. Hints: http://readwrite.com/2013/09/30/understanding-github-a-journey-for-beginners-part-1

**Task05** Put call to a method 'task05' under line "// BEGIN YOUR CODE". Implement TODO in task05 method. If you do everything correctly, you will see the following output "Succesfully found Electronics and Musical!" after you run the Main Method. If not you should debug your solution and find out why it doesn't work.
** Task 05 ** Tomcat is installed in /usr/local/share/Modules/netbeans-7.3.1/apache-tomcat-7.0.34 Work from IDE. Add Apache Tomcat to your servers in IDE (Servers section in Services tab). You must create a user there (e.g. admin/admin). Do not forget to set Catalina base directory to writable, empty and existing directory on your local disk e.g. /tmp/tomcatworkdir. Start and stop your tomcat from services tab. it should start without any exceptions.

**Task06** This task requires you to work with a detached entity. To start working on a task, just add call to task06() method into your main method. Then implement the task06() according the comments in there.
** Task 06 ** Create a hello world web application from IDE. Then deploy it to your Tomcat in IDE.

**Task07** This task is associated with method task07. Parts of the method are commented out, because the implementation of entity Product is not complete yet. Look into comments in task07 and then uncomment the "testing code" in the task07.
** Task 07 ** Clone git branch TODO. This branch contains 2 projects. hello-java7 and hello-tom-web. The first one is not buildable (mvn compile should fail) because it contains code compliant only with Java 7. Your task is to modify pom.xml so that compiler plugin uses target version of Java 1.7.

**Task08** This task requires you to correctly implement equals and hashcode methods of Product entity. Note that you should use business equivalence. Look into the method comments for more instructions.
** Task 08 ** Now your task is to use embedded (through tomcat7 maven plugin. It has goal run) to run web Java application from Task 07 using command line and maven. Firstly you must package the app using Maven and then use tomcat7 plugin. After the web app is started you can use web browser to test it works. Hints: Use documentation of tomcat7 plugin. Also make sure some other Tomcat is not running e.g. the one in IDE or from some other process "ps -ef | grep tomcat". If you find out there is running tomcat of some other user logged into the machine the only solution is hard restart (which is not allowed in B130) or you have to switch computers. Please always report such Tomcats to CVT: send an e-mail to [email protected] with your machine name and ask them to kill the tomcat on the machine.

**Task 09** Quiz. You can check your answers after you take the quiz in file answ-s1.md
1. What is the main configuration file for JPA in your application?
2. Where is the following text used and what is the effect of it (use Hibernate dev guide to find answer)? "hibernate.format_sql"
3. What is hibernate.hbm2ddl.auto property in persistence.xml file?

**Task 10** This task requires more investigation from you and work with Hibernate documentation. For the rest of the seminar you can try to configure your persistence unit to use database in your Netbeans. First you will have to create new database in netbeans (Derby) then reconfigure the persistence unit to connect to this database on localhost and set username and password.

0 comments on commit 59cb79b

Please sign in to comment.