9
Java Servlets and Filters Software Required: JDK 1.7 NetBeans 8 with Tomcat 8 Java Servets are server side com!onents in Java that runs on "Servet #ontainer$ ena%ed we% server such as To mcat& Jett'& (e%S!here etc. Java Servets are !art of the Java )nter!rise )dition *Java ))+ and are used to deveo! d'namic we% a!!ications. Servet is a we% com!onent that is de!o'ed on the server to create d'namic we% !a,e. )nvironment Setu! -!en Net%eans D)& #reate a new Java (e% /!!ication 0rovide 0roect Name as "ServetsNfi ters0ro$ and set runtime as foowin,:  /dd #ic2 3 inish Button Net%eans creates we% a!!ication with a !a,e caed "index.html $ fie #reatin, De!o'ment Descri!tor *we%.4m+ R#ic2 (e% 0a,es  New  -ther  (e%  Standard De!o'ment Descri!tor *we%.4m+ and Run the 0roect

RESTwithSpringMVC4_Angular_MySQL.docx

Embed Size (px)

Citation preview

7/25/2019 RESTwithSpringMVC4_Angular_MySQL.docx

http://slidepdf.com/reader/full/restwithspringmvc4angularmysqldocx 1/9

Java Servlets and FiltersSoftware Required:

• JDK 1.7

• NetBeans 8 with Tomcat 8

Java Servets are server side com!onents in Java that runs on "Servet #ontainer$ ena%ed we% server such as Tomcat&Jett'& (e%S!here etc.Java Servets are !art of the Java )nter!rise )dition *Java ))+ and are used to deveo! d'namic we% a!!ications.Servet is a we% com!onent that is de!o'ed on the server to create d'namic we% !a,e.

)nvironment Setu! -!en Net%eans D)& #reate a new Java (e% /!!ication

0rovide 0roect Name as "ServetsNfiters0ro$ and set runtime as foowin,:

 /dd #ic2 3inish ButtonNet%eans creates we% a!!ication with a !a,e caed "index.html $ fie#reatin, De!o'ment Descri!tor *we%.4m+R#ic2 (e% 0a,es New -ther (e% Standard De!o'ment Descri!tor *we%.4m+ and Run the 0roect

7/25/2019 RESTwithSpringMVC4_Angular_MySQL.docx

http://slidepdf.com/reader/full/restwithspringmvc4angularmysqldocx 2/9

Creating a ServletR#ic2 Source 0ac2a,es New -ther (e% Servet#assName: 5'Servet0ac2a,e: com.'notss.we%ServetName: 5'Servet6R 0attern: 5'Servet

Remove the foowin, #ode in 5'Servet.ava fie9(e%Servet*name ;5'Servet;& ur0atterns <;5'Servet;=+

#onfi,ure Servet in we%.4m fie>servet?  >servet@name?5'Servet>servet@name?  >servet@cass?com.'notss.we%.5'Servet>servet@cass?  >servet?  >servet@ma!!in,?  >servet@name?5'Servet>servet@name?  >ur@!attern?3irstDemo>ur@!attern?  >servet@ma!!in,?

Run the Servethtt!:ocahost:8A8ServetsNfiters0ro3irstDemo

Servlet Filters / fiter is an o%ect that is invo2ed at the !re!rocessin, and !ost !rocessin, of a request. t is !u,,a%e& i.e. its entr' isdefined in the we%.4m fie& if we remove the entr' of fiter from the we%.4m fie& fiter wi %e removed automatica' and wedonCt need to chan,e the servet. So it wi %e easier to maintain the we% a!!ication.

 / servet fiter can interce!t requests %oth for servets& JS0Cs& T5 fies or other static content& as iustrated in thedia,ram %eow:

Usage of Filter Recordin, a incomin, requests& o,s the 0 addresses of the com!uters from which the requests ori,inate& #onversiondata com!ression& encr'!tion and decr'!tion& in!ut vaidation etc.

 /dvanta,e of 3iter: 3iter is !u,,a%e.Filter API i2e servet fiter have its own /0.The ava4.servet !ac2a,e contains the three interfaces of 3iter /03iter& 3iter#hain& 3iter#onfi,1+ 3iter interface3or creatin, an' fiter& 'ou must im!ement the 3iter interface. 3iter interface !rovides the ife c'ce methods for a fiter.!u%ic void init*3iter#onfi, confi,+: init*+ method is invo2ed on' once it is used to initiaiEe the fiter.!u%ic void do3iter*tt!ServetRequest request&tt!ServetRes!onse res!onse& 3iter#hain chain+: do3iter*+ method isinvo2ed ever' time when user request to an' resource& to which the fiter is ma!!ed.t is used to !erform fiterin, tas2s.!u%ic void destro'*+:This is invo2ed on' once when fiter is ta2en out of the service.

Creating a Filter R#ic2 Source 0ac2a,es New -ther (e% 3iter3iterName: Sim!e3iter 

7/25/2019 RESTwithSpringMVC4_Angular_MySQL.docx

http://slidepdf.com/reader/full/restwithspringmvc4angularmysqldocx 3/9

!u%ic cass Sim!e3iter im!ements 3iter <  !u%ic Sim!e3iter*+ <  =

!u%ic void do3iter*ServetRequest request& ServetRes!onse res!onse& 3iter#hain chain+throws -)4ce!tion& Servet)4ce!tion <  Strin, i!/ddress request.,etRemote/ddr*+F

S'stem.out.!rintn*;Do 3iter;+F  S'stem.out.!rintn*;0 ;G i!/ddress G ;& Time ; G new Date*+.toStrin,*++F

chain.do3iter*request& res!onse+F  =

  !u%ic void destro'*+ <S'stem.out.!rintn*;3iter Destro';+F

  =

  !u%ic void init*3iter#onfi, fiter#onfi,+ <S'stem.out.!rintn*;3iter nitia;+F

  ==(hen the servet fiter is oaded the first time& its init*+ method is caed& ust i2e with servets.(hen a TT0 request arrives at 'our we% a!!ication which the fiter interce!ts& the fiter can ins!ect the request 6R& therequest !arameters and the request headers& and %ased on that decide if it wants to %oc2 or forward the request to thetar,et servet& JS0 etc.

Using MySQL#onnect 5'SH with R--T user 

m'sq? #R)/T) 6S)R Cs!rin,userC9CocahostC D)NT3)D BI Cs!rin,user!wCFHuer' -K& A rows affected *A.A sec+

m'sq? #R)/T) D/T/B/S) n,s!rin,FHuer' -K& 1 row affected *A.AA sec+

m'sq? R/NT / -N n,s!rin,.L to s!rin,user9ocahostFHuer' -K& A rows affected *A.A sec+

)4it and connect with s!rin,user 

?m'sq @u s!rin,user @!)nter !assword: LLLLLLLLLLLLm'sq? use n,s!rin,FData%ase chan,ed

m'sq? #R)/T) T/B) coe,e*id int*M+ !rimar' 2e' autoincrement& name varchar*MA+& code varchar*MA++FHuer' -K& A rows affected *A.18 sec+m'sq? insert into coe,e *name& code+ vaues*C5'#oe,eC& C5#AA1C+F

RESTful Web Services(e% services are distri%uted a!!ication com!onents that are e4terna' avaia%e.(e% services are an,ua,e and !atform inde!endent %ecause vendors have a,reed on common we% service standards.Severa !ro,rammin, modes are avaia%e to we% service deveo!ers.

These modes fa into two cate,ories:O S-/0(SD@%ased. n traditiona we% service modes& we% service interfaces are e4!osed throu,h (SD documents*a t'!e of P5+& which have 6Rs. Su%sequent messa,e e4chan,e is in S-/0& another t'!e of P5 document. 3or moredetais& see S-/0@%ased (e% Services.O R)ST@%ased. R)!resentationa State Transfer is a new wa' to create and communicate with we% services. n R)ST&resources have 6Rs and are mani!uated throu,h TT0 header o!erations.R)ST@%ased *;R)STfu;+ we% services are coections of we% resources identified %' 6Rs. )ver' document and ever'!rocess is modeed as a we% resource with a unique 6R. 5essa,e e4chan,e can %e conducted in an' formatQP5&JS-N& T5& etc. n man' cases a we% %rowser can serve as the cient.TT0 is the !rotoco in R)ST. -n' four methods are avaia%e: )T& 06T& 0-ST& and D))T).

7/25/2019 RESTwithSpringMVC4_Angular_MySQL.docx

http://slidepdf.com/reader/full/restwithspringmvc4angularmysqldocx 4/9

Spring MC Fra!e"or#S!rin, is an )nter!rise /!!ication 3ramewor2. ersion .A is the atest maor reease of the S!rin, 3ramewor2 and thefirst to fu' su!!ort Java 8 features. S!rin, framewor2 consists of more than A modues ,rou!ed to,ether. These arear,e' cate,oriEed as #ore #ontainer& Data /ccessnte,ration& (e%& /-0 */s!ect -riented 0ro,rammin,+&nstrumentation& and Test.

$dvantages of Spring MC Fra!e"or#• Su!!orts R)STfu 6Rs.

•  /nnotation %ased confi,uration*i.e. 'ou ma' reduce the metadata fie or ess of confi,uration+.

• Su!!orts to !u, with other 5# framewor2s i2e Struts& Struts& (e%(or2s etc.

• 3e4i%e in su!!ortin, different view t'!es i2e JS0& veocit'& P5& 0D3& Ties etc.

Spring MC% &ro'ect Setup#reate a new Java (e% 0roect

 /dd S!rin, i%raries /dd Jac2son@[email protected]& Jac2son@[email protected]  /dd m'sqconnector.ar fie

Spring MC% Configuration ()ML*1. #reate and 5odif' de!o'ment descri!tor as %eow:>servet?  >servet@name?rest>servet@name?  >servet@cass?or,.s!rin,framewor2.we%.servet.Dis!atcherServet>servet@cass?  >servet?  >servet@ma!!in,?  >servet@name?rest>servet@name?  >ur@!attern?a!iL>ur@!attern?  >servet@ma!!in,?

. S!rin, #onfi,uration 3ie *[email protected]+ in we%@inf foder. R#ic2 (e%0a,es  New -ther S!rin, S!rin, P5 #onfi,uration fie& seect conte4t names!ace.

. /dd mvc names!ace %' t'!in, in [email protected] fie>U4m version;1.A; encodin,;6T3@8;U?

>%eans 4mns;htt!:www.s!rin,framewor2.or,schema%eans;  4mns:4si;htt!:www.w.or,AA1P5Schema@instance;  4mns:conte4t;htt!:www.s!rin,framewor2.or,schemaconte4t;  +!lns,!vc-./ttp,00"""1springfra!e"or#1org0sc/e!a0!vc.  4si:schemaocation;htt!:www.s!rin,framewor2.or,schema%eanshtt!:www.s!rin,framewor2.or,schema%eanss!rin,@%[email protected]  htt!:www.s!rin,framewor2.or,schemaconte4t htt!:www.s!rin,framewor2.or,schemaconte4ts!rin,@[email protected]  http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd ;?  >conte4t:com!onent@scan %ase@!ac2a,e;com.'notss.controer;?  >mvc:annotation@driven?

7/25/2019 RESTwithSpringMVC4_Angular_MySQL.docx

http://slidepdf.com/reader/full/restwithspringmvc4angularmysqldocx 5/9

>%eans?(ith #om!onentScan ta,& S!rin, auto scans a eements in the !rovided %ase !ac2a,e and a its chid !ac2a,es for#ontroer servet. /so& we have used >mvc:annotation@driven? ta, instead of iewResover& with which we can direct'send res!onse data from the controer.

#reatin, a 5ode #ass!ac2a,e com.'notss.modeF

!u%ic cass #oe,e <  !rivate int idF  !rivate Strin, nameF  !rivate Strin, codeF

  !u%ic #oe,e*+ <  =

  !u%ic #oe,e*int id& Strin, name& Strin, code+ <  this.id idF  this.name nameF  this.code codeF  =

  !u%ic int ,etd*+ <

  return idF  =

  !u%ic void setd*int id+ <  this.id idF  =

  !u%ic Strin, ,etName*+ <  return nameF  =

  !u%ic void setName*Strin, name+ <  this.name nameF

  =

  !u%ic Strin, ,et#ode*+ <  return codeF  =

  !u%ic void set#ode*Strin, code+ <  this.code codeF  ==

#reate a D/- #ass!ac2a,e com.'notss.daoF

im!ort com.'notss.mode.#oe,eFim!ort ava.sq.#onnectionFim!ort ava.sq.Driver5ana,erFim!ort ava.sq.ResutSetFim!ort ava.sq.StatementFim!ort ava.uti./rra'istF

!u%ic cass #oe,eDao <  !rivate #onnection ,et#onnection*+ <  #onnection con nuF  tr' <  #ass.forName*;com.m'sq.d%c.Driver;+F

7/25/2019 RESTwithSpringMVC4_Angular_MySQL.docx

http://slidepdf.com/reader/full/restwithspringmvc4angularmysqldocx 6/9

  con Driver5ana,er.,et#onnection*;d%c:m'sq:ocahostn,s!rin,;& ;s!rin,user;& ;s!rin,user!w;+F  =catch*)4ce!tion e+ <  S'stem.out.!rintn*;)rror...; G e+F  =  return conF  =

!u%ic /rra'ist>#oe,e? ,et/#oe,es*+ throws )4ce!tion <  /rra'ist>#oe,e? coe,es new /rra'ist>#oe,e?*+F  #onnection con ,et#onnection*+F  Statement stmt con.createStatement*+F  ResutSet rs stmt.e4ecuteHuer'*;S))#T L 3R-5 coe,e;+F  whie*rs.ne4t*++ <  #oe,e c new #oe,e*rs.,etnt*;id;+& rs.,etStrin,*;name;+& rs.,etStrin,*;code;++F  coe,es.add*c+F  =  return coe,esF  ==

#reate a #ontroer #ass!ac2a,e com.'notss.controerF

im!ort com.'notss.dao.#oe,eDaoF

im!ort com.'notss.mode.#oe,eFim!ort ava.uti./rra'istFim!ort ava.uti.istFim!ort ava4.servet.htt!.tt!ServetRes!onseFim!ort or,.s!rin,framewor2.we%.%ind.annotation.Request5a!!in,Fim!ort or,.s!rin,framewor2.we%.%ind.annotation.Request5ethodFim!ort or,.s!rin,framewor2.we%.%ind.annotation.Request0aramFim!ort or,.s!rin,framewor2.we%.%ind.annotation.Rest#ontroerF

9Rest#ontroer 9Request5a!!in,*;m'coe,e;+!u%ic cass #oe,e#ontroer <  9Request5a!!in,*methodRequest5ethod.)T& vaue;ist;& !roduces ;a!!icationson;+

  !u%ic ist>#oe,e? ,et/#oe,es*+ <  ist>#oe,e? coe,es nuF  tr' <  #oe,eDao c,dao new #oe,eDao*+F  coe,es c,dao.,et/#oe,es*+F  =catch*)4ce!tion e+ <  coe,es new /rra'ist>#oe,e?*+F  =  Lres!onse.seteader*;/ccess@#ontro@/ow@-ri,in;& ;L;+F  res!onse.seteader*;/ccess@#ontro@/ow@5ethods;& ;0-ST& )T& -0T-NS& D))T);+F  res!onse.seteader*;/ccess@#ontro@5a4@/,e;& ;VAA;+F  res!onse.seteader*;/ccess@#ontro@/ow@eaders;& ;4@requested@with;+FL  return coe,esF  ==-ne of the new features of /0 im!rovements is new 9Rest#ontroer annotation which is inherited from the 9#ontroerannotation.

0rior to the version .A& a the S!rin, 5# com!onents has to use the common 9#ontroer annotation to mar2 that asthe controer servet. (hen 'ou im!ement a R)STfu we% services& the res!onse woud %e awa's sent with theres!onse %od'. To ma2e this sim!e& S!rin, .A has !rovided a s!eciaiEed version of controer. oo2 at the definition ofthe 9Rest#ontroer im!ementation.9Request5a!!in,C annotation is used for definin, incomin, request urs for cass and method eves.

Testing Web Service-!en Browser and T'!e: htt!:ocahost:8A8S!rin,5'SqN,a!im'coe,eist

7/25/2019 RESTwithSpringMVC4_Angular_MySQL.docx

http://slidepdf.com/reader/full/restwithspringmvc4angularmysqldocx 7/9

[{"id":1,"name":"MyCollege","code":"MC001"}]

Creating $ngularJS Client1. Setu! /n,uar )nvironment. The com!ete 0roect Structure as %eow:

>WD-#TI0) htm?>htm n,@a!!;5'/!!;?  >head?  >tite?#oe,e 5ana,ement>tite?  >in2 re;st'esheet; href;css%ootstra!.min.css;?  >scri!t src;san,uar.min.s;?>scri!t?  >head?  >%od'?

  >h1?S!rin, #oe,e R)ST )4am!e>h1?  >div n,@controer;#oe,e#ontroer;?  >ta%e cass;ta%e ta%e@%ordered ta%e@condensed;?  >tr?  >th?#oe,e Name>th?>th?#ode>th?>th?/ction>th?  >tr?  >tr n,@re!eat;coe,e in coe,es;?  >td?<<coe,e.name==>td?  >td?<<coe,e.code==>td?  >td?  >a href;avascri!t:void*A+; n,@cic2;deete*coe,e.id+;?Deete>a?>td?  >tr?  >ta%e?  <<messa,e==  >div?  >scri!t?  var m'a!! an,uar.modue*;5'/!!;&XY+F  m'a!!.controer*C#oe,e#ontroerC& function*Zsco!e& Zhtt!+<  Zsco!e.messa,e ;;F  Zhtt!.,et*Ca!im'coe,eistC+.success*function*data+ <  Zsco!e.coe,es dataF

Zsco!e.messa,e ;;F  =+F  =+F  >scri!t?  >%od'?

7/25/2019 RESTwithSpringMVC4_Angular_MySQL.docx

http://slidepdf.com/reader/full/restwithspringmvc4angularmysqldocx 8/9

>htm?Consu!ing $ngularJS client fro! ot/er 2o!ain

1. #reate a #-RS *#ross@-ri,in Resource Sharin,+ 3iter !ac2a,e com.'notss.controerF

im!ort ava.io.-)4ce!tionFim!ort ava4.servet.3iterFim!ort ava4.servet.3iter#hainFim!ort ava4.servet.3iter#onfi,Fim!ort ava4.servet.Servet)4ce!tionFim!ort ava4.servet.ServetRequestFim!ort ava4.servet.ServetRes!onseFim!ort ava4.servet.htt!.tt!ServetRes!onseF

!u%ic cass Sim!e#-RS3iter im!ements 3iter<  9-verride  !u%ic void init*3iter#onfi, fiter#onfi,+ throws Servet)4ce!tion < 

=

  9-verride  !u%ic void do3iter*ServetRequest request& ServetRes!onse res!& 3iter#hain chain+ throws -)4ce!tion&Servet)4ce!tion <

  tt!ServetRes!onse res!onse*tt!ServetRes!onse+ res!F  res!onse.seteader*;/ccess@#ontro@/ow@-ri,in;& ;L;+F  res!onse.seteader*;/ccess@#ontro@/ow@5ethods;& ;0-ST& )T& -0T-NS& D))T);+F  res!onse.seteader*;/ccess@#ontro@5a4@/,e;& ;VAA;+F  res!onse.seteader*;/ccess@#ontro@/ow@eaders;& ;4@requested@with;+F

chain.do3iter*request& res!+F  =  9-verride  !u%ic void destro'*+ < 

= =

. #onfi,ure 3iter >U4m version;1.A; encodin,;6T3@8;U?>we%@a!! 4mns;htt!:4mns.c!.or,4mnsavaee;

4mns:4si;htt!:www.w.or,AA1P5Schema@instance; 4si:schemaocation;htt!:4mns.c!.or,4mnsavaee htt!:4mns.c!.or,4mnsavaeewe%@a!!1.4sd; version;.1;?

  >servet?  >servet@name?rest>servet@name?  >servet@cass?or,.s!rin,framewor2.we%.servet.Dis!atcherServet>servet@cass?  >servet?  >servet@ma!!in,?  >servet@name?rest>servet@name?  >ur@!attern?a!iL>ur@!attern?

  >servet@ma!!in,?  >fiter? >fiter@name?cors>fiter@name?  >fiter@cass?com.'notss.controer.Sim!e#-RS3iter>fiter@cass? >fiter? >fiter@ma!!in,? >fiter@name?cors>fiter@name? >ur@!attern?a!iL>ur@!attern? >fiter@ma!!in,?  >session@confi,?  >session@timeout? A >session@timeout?  >session@confi,?>we%@a!!?

7/25/2019 RESTwithSpringMVC4_Angular_MySQL.docx

http://slidepdf.com/reader/full/restwithspringmvc4angularmysqldocx 9/9

Creating 3eneric 2ata $ccess 4b'ect and its 5!ple!entation!ac2a,e com.'notss.daoF

im!ort ava.io.SeriaiEa%eFim!ort ava.uti.istF

!u%ic interface enericDao>)& 0K e4tends SeriaiEa%e? <

) save*) newnstance+F

) u!date*) transient-%ect+F

) save-r6!date*) transient-%ect+F

void deete*) !ersistent-%ect+F

) findB'd*0K id+F

ist>)? find/*+F

ist>)? find/B'0ro!ert'*Strin, !ro!ert'Name& -%ect vaue+F

=

GenericDaoImpl.java!u%ic a%stract cass enericDaom!>)& 0K e4tends SeriaiEa%e? im!ements enericDao>)& 0K? <

[ [. / the 0ersistence Stuff [..=

6sin, in 0roect!ac2a,e com.'notss.daoFim!ort ava.uti.istFim!ort com.'notss.mode.#oe,eF!u%ic interface #oe,eDao e4tends enericDao>#oe,e& nte,er? <

ist>#oe,e? find/ctive#oe,es*+F

=

Implementation!u%ic cass #oe,eDaom! e4tends enericDaom!>#oe,e& nte,er? im!ements #oe,eDao <

!rotected #ass>#oe,e? ,et)ntit'#ass*+ <#ass>#oe,e? resut #oe,e.cassFreturn resutF

=

9Su!!ress(arnin,s*;unchec2ed;+!u%ic ist>#oe,e? find/ctive#oe,es*+ <

tr' <.. [ #ode returns ist of #oe,es [.

= catch *)4ce!tion e+ <e.!rintStac2Trace*+Fthrow eF

==

=