implementato esercizio studenti con collegamento da client via interfaccia remota
This commit is contained in:
55
exercise1/EJB.java
Normal file
55
exercise1/EJB.java
Normal file
@@ -0,0 +1,55 @@
|
||||
package exercise1;
|
||||
|
||||
import javax.ejb.Local;
|
||||
import javax.ejb.LocalBean;
|
||||
import javax.ejb.Stateless;
|
||||
import javax.inject.Inject;
|
||||
import javax.persistence.EntityManager;
|
||||
import javax.persistence.PersistenceContext;
|
||||
import javax.persistence.Query;
|
||||
import javax.validation.constraints.NotNull;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Created by Giovanni on 24/05/2015.
|
||||
*/
|
||||
@Stateless
|
||||
@LocalBean
|
||||
public class EJB implements RemoteInterface {
|
||||
|
||||
@PersistenceContext(unitName = "exercise1")
|
||||
EntityManager em ;
|
||||
|
||||
|
||||
public void create_student (@NotNull Student s) {
|
||||
em.persist(s);
|
||||
}
|
||||
public void update_student (@NotNull Student s) {
|
||||
em.merge(s);
|
||||
em.persist(s);
|
||||
}
|
||||
public void delete_student (@NotNull Student s) {
|
||||
em.merge(s);
|
||||
em.remove(s);
|
||||
}
|
||||
|
||||
public @NotNull List<Student> findname (@NotNull String name) {
|
||||
Query Q = em.createNamedQuery("findname");
|
||||
Q.setParameter(1, name);
|
||||
return (List<Student>)Q.getResultList();
|
||||
}
|
||||
|
||||
public @NotNull List<Student> findsurname (@NotNull String name) {
|
||||
Query Q = em.createNamedQuery("findsurname");
|
||||
Q.setParameter(1,name);
|
||||
return (List<Student>)Q.getResultList();
|
||||
}
|
||||
|
||||
public @NotNull List<Student> findyear (@NotNull String name) {
|
||||
Query Q = em.createNamedQuery("findyear");
|
||||
Q.setParameter(1,name);
|
||||
return (List<Student>)Q.getResultList();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
40
exercise1/InitializeDB.java
Normal file
40
exercise1/InitializeDB.java
Normal file
@@ -0,0 +1,40 @@
|
||||
package exercise1;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
import javax.annotation.PreDestroy;
|
||||
import javax.ejb.*;
|
||||
import javax.inject.Inject;
|
||||
|
||||
/**
|
||||
* Created by Giovanni on 24/05/2015.
|
||||
*/
|
||||
|
||||
@Singleton
|
||||
@Startup
|
||||
public class InitializeDB {
|
||||
|
||||
private Student s1,s2;
|
||||
|
||||
@Inject
|
||||
private EJB e;
|
||||
|
||||
@PostConstruct
|
||||
void pupulate (){
|
||||
s1 = new Student("mario","marra","1");
|
||||
s2 = new Student("mario","maria","2");
|
||||
e.create_student(s1);
|
||||
e.create_student(s2);
|
||||
}
|
||||
|
||||
@PreDestroy
|
||||
void delete () {
|
||||
e.delete_student(s1);
|
||||
e.delete_student(s2);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
18
exercise1/RemoteInterface.java
Normal file
18
exercise1/RemoteInterface.java
Normal file
@@ -0,0 +1,18 @@
|
||||
package exercise1;
|
||||
|
||||
import javax.ejb.Remote;
|
||||
import javax.persistence.Query;
|
||||
import javax.validation.constraints.NotNull;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Created by Giovanni on 24/05/2015.
|
||||
*/
|
||||
@Remote
|
||||
public interface RemoteInterface {
|
||||
|
||||
@NotNull List<Student> findname (@NotNull String name);
|
||||
@NotNull List<Student> findsurname (@NotNull String name) ;
|
||||
@NotNull List<Student> findyear (@NotNull String name) ;
|
||||
|
||||
}
|
||||
52
exercise1/Student.java
Normal file
52
exercise1/Student.java
Normal file
@@ -0,0 +1,52 @@
|
||||
package exercise1;
|
||||
|
||||
import javax.ejb.Local;
|
||||
import javax.persistence.*;
|
||||
import javax.validation.constraints.NotNull;
|
||||
import java.io.Serializable;
|
||||
|
||||
/**
|
||||
* Created by Giovanni on 24/05/2015.
|
||||
*/
|
||||
@Entity
|
||||
@NamedQueries({
|
||||
@NamedQuery(name = "findname", query = "SELECT s FROM Student s WHERE s.name =?1"),
|
||||
@NamedQuery(name = "findsurname", query = "SELECT s FROM Student s WHERE s.surname =?1"),
|
||||
@NamedQuery(name = "findyear", query = "SELECT s FROM Student s WHERE s.years =?1")
|
||||
})
|
||||
public class Student implements Serializable {
|
||||
|
||||
@Id
|
||||
@GeneratedValue
|
||||
private Long id;
|
||||
|
||||
@NotNull
|
||||
private String name;
|
||||
@NotNull
|
||||
private String surname;
|
||||
@NotNull
|
||||
private String years;
|
||||
|
||||
|
||||
public Student(){}
|
||||
|
||||
public Student (@NotNull String in_name, @NotNull String in_surname, @NotNull String in_year){
|
||||
|
||||
name = in_name;
|
||||
surname = in_surname;
|
||||
years = in_year;
|
||||
|
||||
}
|
||||
|
||||
public @NotNull String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public @NotNull String getSurname() {
|
||||
return surname;
|
||||
}
|
||||
|
||||
public @NotNull String getYears() {
|
||||
return years;
|
||||
}
|
||||
}
|
||||
43
exercise1_client/client.java
Normal file
43
exercise1_client/client.java
Normal file
@@ -0,0 +1,43 @@
|
||||
package exercise1_client;
|
||||
|
||||
import exercise1.RemoteInterface;
|
||||
import exercise1.Student;
|
||||
|
||||
import javax.naming.Context;
|
||||
import javax.naming.InitialContext;
|
||||
import javax.naming.NamingException;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Created by Giovanni on 24/05/2015.
|
||||
*/
|
||||
public class client {
|
||||
|
||||
public static void main (String[] args) {
|
||||
|
||||
|
||||
Context ctx = null;
|
||||
try {
|
||||
ctx =new InitialContext();
|
||||
} catch (NamingException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
RemoteInterface r = null;
|
||||
try {
|
||||
r =(RemoteInterface) ctx.lookup("java:global/web/EJB!exercise1.RemoteInterface");
|
||||
List<Student>l = r.findname("mario");
|
||||
Iterator<Student> it = l.iterator();
|
||||
System.out.println(it.next().getSurname());
|
||||
System.out.println(it.next().getSurname());
|
||||
|
||||
|
||||
} catch (NamingException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
57
servlet/Servletexercise1.java
Normal file
57
servlet/Servletexercise1.java
Normal file
@@ -0,0 +1,57 @@
|
||||
package servlet;
|
||||
|
||||
import test3_persistence.store_class;
|
||||
import test3_persistence.test_class;
|
||||
|
||||
import javax.ejb.EJB;
|
||||
import javax.servlet.ServletException;
|
||||
import javax.servlet.annotation.WebServlet;
|
||||
import javax.servlet.http.HttpServlet;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.io.IOException;
|
||||
import java.io.PrintWriter;
|
||||
import java.util.HashMap;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Created by Giovanni on 25/04/2015.
|
||||
*/
|
||||
|
||||
@WebServlet(name = "Servletexercise1")
|
||||
public class Servletexercise1 extends HttpServlet {
|
||||
|
||||
|
||||
protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
|
||||
response.setContentType("text/html;charset=UTF-8");
|
||||
try (PrintWriter out = response.getWriter()) {
|
||||
/* TODO output your page here. */
|
||||
|
||||
out.println("<!DOCTYPE html>");
|
||||
out.println("<html>");
|
||||
out.println("<head>");
|
||||
out.println("<title>Servlet xg</title>");
|
||||
out.println("</head>");
|
||||
out.println("<body>");
|
||||
out.println("<h1>Servlet xg at " + request.getContextPath() + "</h1>");
|
||||
|
||||
|
||||
|
||||
out.println("</body>");
|
||||
out.println("</html>");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
|
||||
processRequest(request, response);
|
||||
|
||||
}
|
||||
|
||||
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
|
||||
processRequest(request, response);
|
||||
|
||||
}
|
||||
}
|
||||
@@ -47,6 +47,10 @@ public class Servlettest2 extends HttpServlet {
|
||||
class_with_builtin_constraints class_with_builtin_constraints;
|
||||
|
||||
|
||||
@Inject
|
||||
class_test_method class_test_method;
|
||||
|
||||
|
||||
protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
|
||||
response.setContentType("text/html;charset=UTF-8");
|
||||
try (PrintWriter out = response.getWriter()) {
|
||||
@@ -61,6 +65,10 @@ public class Servlettest2 extends HttpServlet {
|
||||
out.println("<h1>Servlet xg at " + request.getContextPath() + "</h1>");
|
||||
|
||||
|
||||
///////////////// check null return
|
||||
//class_test_method.test_method();
|
||||
//out.println("Test Null without validator");
|
||||
//out.println("<br>");
|
||||
|
||||
|
||||
///////////////// entire class validation
|
||||
@@ -123,7 +131,7 @@ public class Servlettest2 extends HttpServlet {
|
||||
|
||||
|
||||
out.println("<br>");
|
||||
//////////////// check single property
|
||||
//////////////// check single property without make an instance of the object
|
||||
Set<ConstraintViolation<class_with_builtin_constraints>> violations7 = validator.validateValue(class_with_builtin_constraints.class, "title", "aabb");
|
||||
if (violations7.size() > 0) out.println("error"); else out.println("tutto ok");
|
||||
|
||||
|
||||
14
test2_constraints_validation/class_test_method.java
Normal file
14
test2_constraints_validation/class_test_method.java
Normal file
@@ -0,0 +1,14 @@
|
||||
package test2_constraints_validation;
|
||||
|
||||
import javax.validation.constraints.NotNull;
|
||||
|
||||
/**
|
||||
* Created by Giovanni on 24/05/2015.
|
||||
*/
|
||||
public class class_test_method {
|
||||
|
||||
public @NotNull Object test_method(){
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user