From 59cb79b863c4d01303228b68330bb82e66917b2f Mon Sep 17 00:00:00 2001 From: Filip Nguyen Date: Sun, 20 Sep 2015 11:26:43 +0200 Subject: [PATCH] first seminar materials --- README.md | 2 + hello-java7/.classpath | 20 ++++++++++ hello-java7/.project | 23 ++++++++++++ .../org.eclipse.core.resources.prefs | 3 ++ .../.settings/org.eclipse.jdt.core.prefs | 5 +++ .../.settings/org.eclipse.m2e.core.prefs | 4 ++ hello-java7/pom.xml | 34 +++++++++++++++++ .../src/main/java/cz/fi/muni/pa165/App.java | 21 +++++++++++ hello-tom-web/README.txt | 14 +++++++ hello-tom-web/pom.xml | 29 +++++++++++++++ .../src/main/java/cz/pa165/TestServlet.java | 25 +++++++++++++ hello-tom-web/src/main/webapp/WEB-INF/web.xml | 8 ++++ hello-tom-web/src/main/webapp/index.jsp | 5 +++ pom.xml | 11 +++--- tasks/seminar01.md | 37 ++++++------------- 15 files changed, 210 insertions(+), 31 deletions(-) create mode 100644 hello-java7/.classpath create mode 100644 hello-java7/.project create mode 100644 hello-java7/.settings/org.eclipse.core.resources.prefs create mode 100644 hello-java7/.settings/org.eclipse.jdt.core.prefs create mode 100644 hello-java7/.settings/org.eclipse.m2e.core.prefs create mode 100644 hello-java7/pom.xml create mode 100644 hello-java7/src/main/java/cz/fi/muni/pa165/App.java create mode 100644 hello-tom-web/README.txt create mode 100644 hello-tom-web/pom.xml create mode 100644 hello-tom-web/src/main/java/cz/pa165/TestServlet.java create mode 100644 hello-tom-web/src/main/webapp/WEB-INF/web.xml create mode 100644 hello-tom-web/src/main/webapp/index.jsp diff --git a/README.md b/README.md index 38565b3f..42be8174 100755 --- a/README.md +++ b/README.md @@ -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. diff --git a/hello-java7/.classpath b/hello-java7/.classpath new file mode 100644 index 00000000..7c632d26 --- /dev/null +++ b/hello-java7/.classpath @@ -0,0 +1,20 @@ + + + + + + + + + + + + + + + + + + + + diff --git a/hello-java7/.project b/hello-java7/.project new file mode 100644 index 00000000..68f9609e --- /dev/null +++ b/hello-java7/.project @@ -0,0 +1,23 @@ + + + hello-java7 + + + + + + org.eclipse.jdt.core.javabuilder + + + + + org.eclipse.m2e.core.maven2Builder + + + + + + org.eclipse.jdt.core.javanature + org.eclipse.m2e.core.maven2Nature + + diff --git a/hello-java7/.settings/org.eclipse.core.resources.prefs b/hello-java7/.settings/org.eclipse.core.resources.prefs new file mode 100644 index 00000000..e9441bb1 --- /dev/null +++ b/hello-java7/.settings/org.eclipse.core.resources.prefs @@ -0,0 +1,3 @@ +eclipse.preferences.version=1 +encoding//src/main/java=UTF-8 +encoding/=UTF-8 diff --git a/hello-java7/.settings/org.eclipse.jdt.core.prefs b/hello-java7/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 00000000..714351ae --- /dev/null +++ b/hello-java7/.settings/org.eclipse.jdt.core.prefs @@ -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 diff --git a/hello-java7/.settings/org.eclipse.m2e.core.prefs b/hello-java7/.settings/org.eclipse.m2e.core.prefs new file mode 100644 index 00000000..f897a7f1 --- /dev/null +++ b/hello-java7/.settings/org.eclipse.m2e.core.prefs @@ -0,0 +1,4 @@ +activeProfiles= +eclipse.preferences.version=1 +resolveWorkspaceProjects=true +version=1 diff --git a/hello-java7/pom.xml b/hello-java7/pom.xml new file mode 100644 index 00000000..05a5b090 --- /dev/null +++ b/hello-java7/pom.xml @@ -0,0 +1,34 @@ + + + 4.0.0 + + cz.fi.muni.pa165 + eshop-parent + 1.0 + + cz.fi.muni.pa165 + hello-java7 + 1.0-SNAPSHOT + hello-java7 + http://maven.apache.org + + UTF-8 + + + + + + maven-compiler-plugin + 3.2 + + 1.7 + 1.7 + + + + + + + diff --git a/hello-java7/src/main/java/cz/fi/muni/pa165/App.java b/hello-java7/src/main/java/cz/fi/muni/pa165/App.java new file mode 100644 index 00000000..4c3da9cb --- /dev/null +++ b/hello-java7/src/main/java/cz/fi/muni/pa165/App.java @@ -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 messages = new ArrayList<>(); + messages.add("Hello" ); + messages.add("World" ); + System.out.println( messages); + } +} diff --git a/hello-tom-web/README.txt b/hello-tom-web/README.txt new file mode 100644 index 00000000..e0b55217 --- /dev/null +++ b/hello-tom-web/README.txt @@ -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 provided. 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 xnguyen@fi.muni.cz diff --git a/hello-tom-web/pom.xml b/hello-tom-web/pom.xml new file mode 100644 index 00000000..7d5b5272 --- /dev/null +++ b/hello-tom-web/pom.xml @@ -0,0 +1,29 @@ + + 4.0.0 + com.acme + my-webapp + war + 1.0-SNAPSHOT + my-webapp Maven Webapp + http://maven.apache.org + + + + org.apache.tomcat.maven + tomcat7-maven-plugin + 2.1 + + + my-webapp + + + + + org.apache.tomcat + tomcat-servlet-api + 7.0.42 + provided + + + diff --git a/hello-tom-web/src/main/java/cz/pa165/TestServlet.java b/hello-tom-web/src/main/java/cz/pa165/TestServlet.java new file mode 100644 index 00000000..a00a6912 --- /dev/null +++ b/hello-tom-web/src/main/java/cz/pa165/TestServlet.java @@ -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!"); + } + +} diff --git a/hello-tom-web/src/main/webapp/WEB-INF/web.xml b/hello-tom-web/src/main/webapp/WEB-INF/web.xml new file mode 100644 index 00000000..fc428759 --- /dev/null +++ b/hello-tom-web/src/main/webapp/WEB-INF/web.xml @@ -0,0 +1,8 @@ + + + + Archetype Created Web Application + + diff --git a/hello-tom-web/src/main/webapp/index.jsp b/hello-tom-web/src/main/webapp/index.jsp new file mode 100644 index 00000000..c38169bb --- /dev/null +++ b/hello-tom-web/src/main/webapp/index.jsp @@ -0,0 +1,5 @@ + + +

Hello World!

+ + diff --git a/pom.xml b/pom.xml index 7ca2a54f..45ded71c 100644 --- a/pom.xml +++ b/pom.xml @@ -1,7 +1,5 @@ - + + 4.0.0 cz.fi.muni.pa165 eshop-parent @@ -41,8 +39,9 @@ eshop-service eshop-api eshop-fe - + hello-java7 + - + \ No newline at end of file diff --git a/tasks/seminar01.md b/tasks/seminar01.md index 36c9d3d6..1189e213 100755 --- a/tasks/seminar01.md +++ b/tasks/seminar01.md @@ -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 unix@fi.muni.cz 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.