NotesWhat is notes.io?

Notes brand slogan

Notes - notes.io

**klasa
@Stateful
@Singleton
@LocalBean
@Interceptors
@Startup

**polja
@EJB
SingletonKlasa sk;

@Resource
TimeServicets

**metode
@AroundInvoke
public Object increaseSmth(InvocationContext ctx) throws Exception{
out.println(ctx.getMethod());
Object result = ctx.proceed();
.........
return result;
}

@Interceptors(PovecajBroj.class)

@Timeout
updateFields(Timer t)

@PostConstruct
startTimer()

@Shedule(second ="*", minute="*/15", hour="*", persistent=false)



InvocationContext(getMethod, proceed)
************
Timeri

@Singleton
@LocalBean
@Startup
public class SingletonOglas {
int brojJavljanjaNaOglas = 0;
@Resource
TimerService ts;
private Timer tm;
private HashMap<Integer, Integer> brojJavljanja = new HashMap<Integer, Integer>();
private HashMap<String, Integer> pomZaJavlanja = new HashMap<String, Integer>();
List<Oglas> oglasi;

@PersistenceContext
EntityManager em;

/**
* Default constructor.
*/
public SingletonOglas() {
}

public void incBrojUkupnogJavljanja() {
brojJavljanjaNaOglas++;
}


@Timeout
public void updateFields(Timer t) {
System.out.println("Broj javjanja na oglase: " + brojJavljanjaNaOglas);
brojJavljanjaNaOglas = 0;
}

public List<Oglas> getOglasi() {
return oglasi;
}

public void setOglasi(List<Oglas> oglasi) {
this.oglasi = oglasi;
}

public HashMap<String, Integer> getPomZaJavlanja() {
return pomZaJavlanja;
}

public void setPomZaJavlanja(HashMap<String, Integer> pomZaJavlanja) {
this.pomZaJavlanja = pomZaJavlanja;
}

@PostConstruct
public void startTimer() {
TypedQuery<Oglas> t = em.createQuery("SELECT k FROM Oglas k", Oglas.class);
oglasi = t.getResultList();
for (Oglas oglas : oglasi) {
pomZaJavlanja.put(oglas.getText(), oglas.getIdOglas());
}
TimerConfig config = new TimerConfig();
config.setPersistent(false);

// a.set(115, 12, 1, 23, 59);

// Date(115, 12, 1, 23, 59);
// Long time = new Date().getTime();
// Date date = new Date(time - time % (24 * 60 * 60 * 1000));
// tm = ts.createIntervalTimer(date, 24*60*60*1000, config);
ScheduleExpression schedule = new ScheduleExpression();
schedule.month(1);
schedule.dayOfMonth(1);
schedule.minute(0);
schedule.hour(0);
schedule.second(1);
tm = ts.createCalendarTimer(schedule, config);

// kreiranje perzistentnog timer-a
// tm = ts.createTimer(0, 15000, null);
}

@Schedule(second = "*", minute = "*/15", hour = "*", persistent = false)
public void osveziBazu() {
System.out.println("Osvezavanje baze podataka!:");
TypedQuery<Oglas> query = em.createQuery("SELECT u FROM Oglas u ", Oglas.class);
List<Oglas> oglasiUBazi = query.getResultList();
/*
* //System.out.println("Neki broj ili nesto"+k.brojJavljanja.get(2));
* System.out.println(brojJavljanja.isEmpty());
* brojJavljanja.forEach((k,v) -> System.out.println(
* "Prva provera hash mape:"+"key: "+k+" value:"+v));
*/
if (oglasiUBazi != null && !brojJavljanja.isEmpty()) {
brojJavljanja
.forEach((k, v) -> System.out.println("ispis ako mapa nije prazna:" + "key: " + k + " value:" + v));
for (Oglas o : oglasiUBazi) {
int id = o.getIdOglas();
if (brojJavljanja.get(id) != null) {
int broj = brojJavljanja.get(id);
o.setBrojPregleda(o.getBrojPregleda() + broj);
try {
em.merge(o);
} catch (Exception ex) {
ex.printStackTrace();
brojJavljanja.clear();
System.out.println("Broj pregleda je resetovan u EXU!:");
return;
}
}
}
brojJavljanja.clear();
System.out.println("Broj pojedinacnih pregleda je resetovan NORMALNO!:");

} else {
System.out.println("Nula iz baze ili iz hashmape");
}
}

public HashMap<Integer, Integer> getBrojJavljanja() {
return brojJavljanja;
}

public void setBrojJavljanja(HashMap<Integer, Integer> brojJavljanja) {
this.brojJavljanja = brojJavljanja;
}
}

************
@PersistenceContext
EntityManager em;

***********
TypedQuery<obj> query = em.createQuery(
"SELECT u FROM obj u WHERE :username like u.username ",
obj.class);
query.setParameter("username", username);
obj user = query.getSingleResult();
if (user != null) {
userID = user;
return true;
}
return false;
***********
try {
Oglas o = new Oglas();
o.setText(text);
o.setBrojPregleda(0);
o.setOglasKorisnik(userID);
em.persist(o);
return true;
} catch (Exception ex) {
ex.printStackTrace();
return false;
}
***********
TypedQuery<Oglas> oglas = em.createQuery("SELECT k FROM Oglas k", Oglas.class);
return oglas.getResultList();
***********
web servlet

/**
* Servlet implementation class LoginServlet
*/
@WebServlet("/LoginServlet")
public class LoginServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
List<Oglas> searchResult;
@PersistenceContext
EntityManager em;
@EJB
SingletonOglas sing;

/**
* @see HttpServlet#HttpServlet()
*/
private static final String CART_SESSION_KEY = "shoppingCart";

public LoginServlet() {
super();
}

/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
* response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}

/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
* response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String type = request.getParameter("type");
KomunikacijaSaInterfaceomLocal cartBean = (KomunikacijaSaInterfaceomLocal) request.getSession()
.getAttribute(CART_SESSION_KEY);
if (cartBean == null) {
try {
InitialContext ic = new InitialContext();
cartBean = (KomunikacijaSaInterfaceomLocal) ic.lookup(
"java:global/OglasiEAR/OglasiEJB/KomunikacijaSaInterfaceom!rzk.KomunikacijaSaInterfaceomLocal");
request.getSession().setAttribute(CART_SESSION_KEY, cartBean);
boolean result = false;
String username = request.getParameter("userName");
String password = request.getParameter("password");
if (!username.equals("") && username != null && password != null && !password.equals("")) {
result = cartBean.login(username, password);

}
List<Oglas> oglasi = sing.getOglasi();
HttpSession session = request.getSession();
session.setAttribute("oglasi", oglasi);
request.setAttribute("result", result);
RequestDispatcher rd = getServletContext().getRequestDispatcher("/login.jsp");
rd.forward(request, response);
} catch (NamingException e) {
throw new ServletException(e);

}
} else if (type.equals("unos")) {
boolean result = false;
String textOglasa = request.getParameter("textOglasa");
result = cartBean.postaviOglas(textOglasa);
request.setAttribute("result", result);
RequestDispatcher rd = getServletContext().getRequestDispatcher("/unosOglas.jsp");
rd.forward(request, response);
} else if (type.equals("pretraga")) {
// boolean result=false;
searchResult = cartBean.izlistajOglase();
/*
* for(Oglas o:searchResult){ System.out.println("Servlet ispis: "
* +o.getText()); }
*/
request.setAttribute("searchResult", searchResult);
RequestDispatcher rd = getServletContext().getRequestDispatcher("/pretragaOglas.jsp");
rd.forward(request, response);
} else if (type.equals("javljanje")) {
String event = ((String) request.getParameter("oglas"));
System.out.println(event);
HashMap<String, Integer> pomZaJavlanja = sing.getPomZaJavlanja();
int id = pomZaJavlanja.get(event);
// String idOglasa=request.getParameter("idOglas");
boolean result = false;
/*
* int id=0; try{ id=Integer.parseInt(idOglasa); }catch(Exception
* e){ result=false; }
*/
Oglas o = em.find(Oglas.class, id);
String text = request.getParameter("textPrijava");
result = cartBean.prijaviseZaOglas(o, text);
request.setAttribute("result", result);
RequestDispatcher rd = getServletContext().getRequestDispatcher("/javljanjeOglas.jsp");
rd.forward(request, response);

} else if (type.equals("hiddenValue")) {
/*
* List<Oglas> result =sing.getOglasi();
* request.setAttribute("rezultat", result);
*/
RequestDispatcher rd = getServletContext().getRequestDispatcher("/javljanjeOglas.jsp");
rd.forward(request, response);
}
// unos,login,pretraga,javljanje
/*
* }else if(type.equals("unos")){ boolean result=false; String
* textOglasa=request.getParameter("textOglasa");
* result=kom.postaviOglas(textOglasa); request.setAttribute("result",
* result); RequestDispatcher rd =
* getServletContext().getRequestDispatcher("/unosOglas.jsp");
* rd.forward(request, response); }
*/

}
}

*****************
jsp
dodaj prvo <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>


<form action="/OglasWEB/pretragaOglas.jsp" method="get">
<input type="submit" value="Pretraga oglasa">
</form>
<form action="/OglasWEB/javljanjeOglas.jsp" method="get">
<input type="hidden" id="thisField" name="type" value="hiddenValue">
<input type="submit" value="Javljanje na oglas">
</form>

</form>
<form action="/OglasWEB/LoginServlet" method="get">

Izaberite oglas na koji zelite da se prijavite : <select name="oglas">
<c:forEach var="o" items="${oglasi}">
<option value="${o.text}">${o.text}</option>
</c:forEach>
</select> Textza prijavu na oglas <input type="text" name="textPrijava">
<input type="hidden" name="type" value="javljanje"> <input
type="submit" value="Dodaj prijavu na oglas">
</form>
<form action="/OglasWEB/index.jsp" method="get">
<input type="submit" value="NAZAD">
</form>
Postavljena prijava na oglas :
<%=request.getAttribute("result")%>
<c:if test="${!empty listaOglasa}">

<table>
<thead>
<td>Id</td>
<td>Text</td>
</thead>
<c:forEach var="zad2" items="${listaOglasa}">
<tr>
<td>${zad2.idOglas}</td>
<td>${zad2.text}</td>

</tr>

</c:forEach>

</table>
</c:if>

<form action="/OglasWEB/LoginServlet" method="get">
UserName: <input type="text" name="userName">
<input type="hidden" name="type" value="login">
Password: <input type="text" name="password">
<input type="submit" value="Login">
</form>
******
Logovanje : <%=request.getAttribute("result")%>
******
<form action="/OglasWEB/LoginServlet" method="get">
Pretraga Oglasa:
<input type="hidden" name="type" value="pretraga">
<input type="submit" value="Prikazi Oglase">
</form>
<form action="/OglasWEB/index.jsp" method="get">
<input type="submit" value="NAZAD">
</form>
<c:if test="${!empty searchResult}">

<table>
<thead><td>Id</td><td>Text</td></thead>
<c:forEach var="zad" items="${searchResult}">
<tr>
<td>${zad.idOglas}</td>
<td>${zad.text}</td>

</tr>

</c:forEach>

</table>
     
 
what is notes.io
 

Notes.io is a web-based application for taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000 notes created and continuing...

With notes.io;

  • * You can take a note from anywhere and any device with internet connection.
  • * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
  • * You can quickly share your contents without website, blog and e-mail.
  • * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
  • * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.

Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.

Easy: Notes.io doesn’t require installation. Just write and share note!

Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )

Free: Notes.io works for 12 years and has been free since the day it was started.


You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;


Email: [email protected]

Twitter: http://twitter.com/notesio

Instagram: http://instagram.com/notes.io

Facebook: http://facebook.com/notesio



Regards;
Notes.io Team

     
 
Shortened Note Link
 
 
Looding Image
 
     
 
Long File
 
 

For written notes was greater than 18KB Unable to shorten.

To be smaller than 18KB, please organize your notes, or sign in.