You are on page 1of 9

VEBE 07 MVC PROSLEIVANJE ATRIBUTA

Set i Get Atribute


U MVC modelu Servlet ima ulogu da kontrolie tok informacija i prosleuje odgovarajue podatke na
eljene stranice.
Objekat bilo kojeg tipa se iz Servleta moe poslati preko request objekta metodom:
request.setAttribute(ime atributa, objekat);
Na JSP stranici (ili Servlet-u) se objekat preuzima takoe preko request objekta metodom:
request.getAttribute(ime atributa);
Poto se sa getAttribute moe pokupiti bilo koji tip objekta neophodno je uraditi kastovanje ili naznaku
kojeg tipa je pokupljeni atribut.

PRIMER: Prosleivanje atributa msg iz Servleta i njegovo prihvatanje na JSP strani.


SERVLET
request.setAttribute("msg", msg);

JSP
String msg = (String)
request.getAttribute("msg");

Sa ovako osmiljenim prenosom atributa JSP se moe dizajnirati da predstavlja VIEW za odreeni tip
objekta ili njegovu kolekciju, stim to nije vano ko e proslediti objekat na ispis, pod pretpostavkom da
zna ime atributa koji se alje.
Svi podeeni atributi se iz servleta alju na eljenu JSP stranu ili Servlet sledeom komandom:
// prosledjivanje svih parametara i kreiranih atributa na index.jsp
request.getRequestDispatcher("index.jsp").forward(request, response);

// redirekcija bez prosledjivanja na index.jsp


response.sendRedirect("index.jsp");

Prikaz.jsp (u ovom sluaju ) je stranica na koju se alju svi set-ovani atributi.


NAPOMENE:
RequestDispatcher moe biti pozvan samo jednom unutar jednog servleta,
moe ih biti onoliko koliko ima i grana koda (if-else).
Ne sme da postoji grana koda koja nema svoj RequestDispatcher jer e se onda ostati u Servletu i
neemo se vratiti ni na jednu jsp stranicu

ZADATAK 1: Modifikovati zadatak sa prethodnog asa da ukoliko:

je sve zadovoljeno pri validaciji forme prosledi atribute na prikaz.jsp (view) stranicu gde e se
podaci ispisivati u formatu: Ja sam IME PREZIME i imam GODINA.

nije sve zadovoljeno odogovarajuu poruku vraa na index.stranu

Modifikacije:

Index.jsp dodavanje msg promenljive ispod forme koja se prikazuje ako postoji
ServletValidacija prosleivanje atributa na odgovarajue stranice

// index.jsp body deo


<!-- Generisanje dva jednocifrena broja -->
<%
int prvi = (int)(Math.random()*10);
int drugi = (int)(Math.random()*10);
%>
<form action="ServletValidacija" method="post">
<table>
<tr>
<th colspan="2">VALIDACIONA FOMRA</th>
</tr>
<tr>
<td align="right">IME:</td>
<td><input type="text" name="ime"/></td>
</tr>
<tr>
<td align="right">PREZIME:</td>
<td><input type="text" name="prezime"/></td>
</tr>
<tr>
<td align="right">GODINE:</td>
<td><input type="text" name="godine"/></td>
</tr>
<tr>
<td align="right">SIFRA:</td>
<td><input type="password" name="sifra"/></td>
</tr>
<tr>
<td align="right"><%=prvi %>+<%=drugi %>=</td>
<td>
<input type="text" name="zbir"/>
<!-- sakriveno slanje generisanih brojva -->
<input type="hidden" name="prvi" value="<%=prvi %>"/>
<input type="hidden" name="drugi" value="<%=drugi %>"/>
</td>
</tr>
<tr>
<td colspan="2" align="right"><input type="submit" value="Potvrdi"/></td>
</tr>

<!-- DODATO -->


<tr>
<td colspan="2" align="center">
<!-- Prihvatanje atributa msg -->
<%
String msg = (String)request.getAttribute("msg");
if (msg != null){
%>
<%= msg %>
<% } %>
</td>
</tr>
</table>
</form>
// ServletValidacija
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
PrintWriter out = response.getWriter();
// ucitavanje parametara
String ime = request.getParameter("ime");
String prezime = request.getParameter("prezime");
String godine = request.getParameter("godine");
String sifra = request.getParameter("sifra");
String zbir = request.getParameter("zbir");
String prvi = request.getParameter("prvi");
String drugi = request.getParameter("drugi");
// provera da li su svi parametri popunjeni
if (ime!= null && ime.length()>0 &&
prezime!= null && prezime.length()>0 &&
godine!= null && godine.length()>0 &&
sifra!= null && sifra.length()>0 &&
zbir!= null && zbir.length()>0 &&
prvi!= null && prvi.length()>0 &&
drugi!= null && drugi.length()>0 ){

// pokusaj pretvaranja godine i zbira u ceo broj


try {
int godInt = Integer.parseInt(godine);
int zbirInt = Integer.parseInt(zbir);
// provera sifre i zbira
if(sifra.equals("12345") &&
(zbirInt == new Integer(prvi)+ new Integer(drugi))){
// setovanje atributa
request.setAttribute("ime", ime);
request.setAttribute("prezime", prezime);
request.setAttribute("godine", godine);
// prosledjivanje na prikaz.jsp
request.getRequestDispatcher("prikaz.jsp").forward(request, response);
}else{
request.setAttribute("msg", "Pogresna sifra ili validacioni zbir !!!");
request.getRequestDispatcher("index.jsp").forward(request, response);
}
} catch (Exception e) {
request.setAttribute("msg", "Niste uneli ceo broj !!!");
request.getRequestDispatcher("index.jsp").forward(request, response);
}
}else {
request.setAttribute("msg", "Morate popuniti sva polja !!!");
request.getRequestDispatcher("index.jsp").forward(request, response);
}

// prikaz.jsp
<body>
<%
String ime = (String) request.getAttribute("ime");
String prezime = (String) request.getAttribute("prezime");
String godine = (String) request.getAttribute("godine");
%>
<%
if (ime != null && prezime != null && godine != null) {
%>
Ja sam <%=ime%>&nbsp<%=prezime%> i imam <%=godine%> godina !!!
<%
} else {
%>
Nema podataka o osobi za ispis !!!
<%
}
%>
</body>
// prikaz.jsp preko servleta

// direktan pristup
// http://localhost:8080/validacija/prikaz.jsp

REZIME TOKA PODATAKA:


Index.jsp => ServletValidacija => prikaz.jsp validacaija OK
Index.jsp => ServletValidacija => index.jsp validacaija ERROR
ServletValidacija : kontolie podatke i redirekciju
MVC:
VIEW
o Index.jsp
o Prikaz.jsp
CONTROLLER
o ServletValidacija.java

ZADATAK 2:
Kreirati klasu Auto sa :
parametrima: marka, cena, godite.
Konstuktorima
Get i set metodama
Kreirati stranicu index.jsp sa linkovima na:
Stranicu unos.jsp.
ServletAuto sa parametrom ispis=lista
ServletAuto sa parametrom ispis=jedan i br=3
Kreirati stranicu unos.jsp koja:
Sadri formu za prosleivanje marke, cene i godita na ServletAuto post metodom.
Kupi atribut msg i ispisuje ga ako postoji
Kreirati stranicu auto.jsp za ispis jednog prosleenog auta tako to:
Kupi atribut Auto auto.
Kreira odgovarajuu tabelu i ispisuje podatke auta
Kreirati stranicu listaAuta.jsp za ispis prosleene liste auta tako to:
Kupi atribut ArrayList<Auto>listaAuta.
Kreira odgovarajuu tabelu i ispisuje podatke svih auta
Kreirati servlet ServletAuto koji u metodama :
doPost kupi parametre sa forme, proverava format cene i godita, kreira auto i prosleuje ga na
auto.jsp preko imena atributra auto
doGet kupi parametar ispis, i ako je ispis jednako:
o lista kreira jednu listu auta i prosleuje je na listaAuta.jsp
o jedan kupi parametar br i prosleuje auto iz liste sa pozicije br na auto.jsp.
PROBATI ZA VEBU grupni rad na projektu:
GRUPA 1 : kreira, index.jsp, listaAuta.jsp
GRUPA 2: kreira unos.jsp, Auto.jsp,
GRUPA 3: kreira Auto.java, ServletAuto.java
Kada odgovarajua grupa zavri svoj deo kopira ga na server u odgovarajui zajedniki folder da ga
preuzmu druge dve grupe.
Ako se ispotuju pravila saradnje po kompletiranju projekta sve bi trebalo da radi !!!

REENJE:
public class Auto {
private String marka;
private int cena;
private int godiste;
public Auto(String marka, int cena, int godiste ) {
this.marka = marka;
this.cena = cena;
this.godiste = godiste;
}
public Auto(){}

public
public
public
public
public
public

String getMarka(){ return marka;}


void setMarka(String marka){this.marka = marka;}
int getCena(){return cena;}
void setCena(int cena){this.cena = cena;}
int getGodiste(){return godiste;}
void setGodiste(int godiste){this.godiste = godiste;}

index.jsp
<a href="unos.jsp"> UNOS </a><br>
<a href="ServletAuto?ispis=lista"> LISTA </a><br>
<a href="ServletAuto?ispis=jedan&br=3"> JEDAN </a>
unos.jsp
<form name="myform" action="ServletAuto" method=post>
<table>
<tr height="25">
<td align="right" width="20%"> MARKA :</td>
<td><input type="text" name="marka"></td>
</tr>
<tr height="25">
<td align="right" width="20%"> CENA :</td>
<td><input type="text" name="cena"></td>
</tr>
<tr height="25">
<td align="right" width="20%"> GODISTE :</td>
<td><input type="text" name="godiste"></td>
</tr>
<tr height="25">
<td align="center" width="20%" colspan="2">
<input type="submit" name="akcija" value="UNOS">
</td>
</tr>
<tr height="25">
<td colspan="2">
<a href="index.jsp"> poetna </a></td>
</tr>
<%
String msg = (String) request.getAttribute("msg");
if (msg != null && msg.length() > 0) {
%>
<tr height="25">
<td colspan="2"><%= msg%></td>
</tr>
<%
}
%>
</table>
</form>

auto.jsp
<%
Auto auto = (rs.Auto)request.getAttribute("auto");
%>
<table width="40%" align="center">
<tr>
<td colspan="3" align="center">PODACI O AUTOMOBILU</td>
</tr>
<tr align="center">
<td>MARKA</td>
<td>CENA</td>
<td>GODISTE</td>
</tr>
<tr align="center">
<td><%= auto.getMarka() %></td>
<td><%= auto.getCena() %></td>
<td><%= auto.getGodiste() %></td>
</tr>
</table>
listaAuta.jsp
<%
ArrayList<Auto> la = (ArrayList<Auto>) request.getAttribute("listaAuta");
%>
<table width="40%" align="center">
<tr>
<td colspan="3" align="center">PODACI O AUTOMOBILIMA</td>
</tr>
<tr align="center">
<td>MARKA</td>
<td>CENA</td>
<td>GODISTE</td>
</tr>
<%
for (Auto pom : la) {
%>
<tr align="center">
<td><%=pom.getMarka()%></td>
<td><%=pom.getCena()%></td>
<td><%=pom.getGodiste()%></td>
</tr>
<%
}
%>
</table>

ServletAuto.jsp
public class ServletAuto extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(
HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
ArrayList<Auto> la = new ArrayList<Auto>();
// simuliramo listu od 5 auta
la.add(new Auto("Skoda", 2000, 1998));
la.add(new Auto("BMW", 5800, 2004));
la.add(new Auto("Toyota", 3500, 2002));
la.add(new Auto("Audi", 4000, 2005));
la.add(new Auto("Kec", 750, 1998));
String ispis = req.getParameter("ispis");
if (ispis != null && ispis.length()>0){
if (ispis.equals("lista")){
// prosledjivanje liste na ispis
req.setAttribute("listaAuta",la);
req.getRequestDispatcher("listaAuta.jsp").forward(req, resp);
}else if (ispis.equals("jedan")){
String br = req.getParameter("br");
if (br != null && br.length()>0){
// prosledjivanje auta na ispis
req.setAttribute("auto",la.get(new Integer(br)));
req.getRequestDispatcher("auto.jsp").forward(req, resp);
}
}
}else {
req.setAttribute("msg","Parametri nisu dobrog formata !!!");
req.getRequestDispatcher("index.jsp").forward(req, resp);
}
}// kraj doGet
protected void doPost(
HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
String marka = req.getParameter("marka");
String cenaStr = req.getParameter("cena");
String godisteStr = req.getParameter("godiste");
if (marka != null && marka.length()>0 &&
cenaStr != null && cenaStr.length()>0 &&
godisteStr != null && godisteStr.length()>0){
try {

int cena = Integer.parseInt(cenaStr);


int godiste = Integer.parseInt(godisteStr);
Auto a = new Auto(marka, cena, godiste);
req.setAttribute("auto", a);
req.getRequestDispatcher("auto.jsp").forward(req, resp);

} catch (Exception e) {
req.setAttribute("msg","Cena, godiste nisu dobrog formata");
req.getRequestDispatcher("unos.jsp").forward(req, resp);
}
}else{
req.setAttribute("msg","morate popuniti sva polja !!!");
req.getRequestDispatcher("unos.jsp").forward(req, resp);
}
}// kraj doPost

MVC:
MODEL
o Auto.java
VIEW
o index.jsp
o unos.jsp
o auto.jsp
o listaAuta.jsp
CONTROLLER
o ServletAuto.java

You might also like