Advanced JAVA Part-II

Share Embed Donate

Short Description

Advance java part II...


(1) Simple Server-Side Programming using Servlets (A) Aim: Write a servlet that accepts single-valued (Name, Mobile No) as !ell as multivalued ("anguages #no!n, $obbies) parameters li#e chec# bo%es and multiple selection list bo%es &rom $'M" document and outputs them to the screen Solution: Servlet $'M" ode: html* head* title*+title* meta charset.'/-0* meta namevie!port content!idthdevice-!idth, initial-scale1* +head* bod2* div* &orm method  get action  Servletode* table border  1* tr*td*Name:+td* td*input t2pe  te%t name  Name:*+td*+tr* tr*td*Mobile No:+td* td*input t2pe  te%t name  Mobile3No:*+td*+tr* tr*td*"anguage #no!n:+td* td*input t2pe  chec#bo% name  "anguages3#no!n: value  4nglish*4nglish+td* td*input t2pe  chec#bo% name  "anguages3#no!n: value  Marathi*Marathi+td* td*input t2pe  chec#bo% name  "anguages3#no!n: value  $indi*$indi+td*+tr* tr*td*$obbies+td* td*select multiple si5e  6 name  $obbies:* option*Singing option*ric#et option*omputer 7ames option*8eading option*S!imming +select*+td*+tr* tr* td*input t2pe  submit value  Submit*+td* td*input t2pe  submit value  8eset*+td* +tr* +table* +&orm* +div* +bod2* +html*

Servlet 9avaode: import import import import import import import

avaio;uest= ava%servlethttp$ttpServlet8esponse=

public class Servletode e%tends $ttpServlet ? protected void process8e>uest($ttpServle process8e>uest($ttpServlet8e>uest t8e>uest re>uest, $ttpServlet8esponse response) thro!s Servlet4%ception, ;uestgetParameter(t%t"oc)= c)= s>r  insert into 4P' values(FdnoF, KFdnameFK, KFdlcFK)=   lass&orName(orgapachederb2dbclientriver)= onnection cn 

riverManagergetonnection(dbc:derb2:++localhost:16J+ompan2)= Statement st  cncreateStatement()=   ste%ecute.pdate(s>r)= H catch(4%ception e) ?   outprint(egetMessage())= H outprintln(@uest($ttpServlet8e>uest t8e>uest re>uest, $ttpServlet8esponse response) thro!s Servlet4%ception, ;uestgetonte%tPath() F +h1*)= String ans  (String) re>uestgetSession(&alse re>uestgetSession(&alse)getAttribute(un)= )getAttribute(un)=   outprintln(ans)= outprintln(+bod2*)=   outprintln(+html*)= H H H

(O) Simple Server-Side programming using 9SP O Aim: Write 9SP &ile that accepts a number &rom $'M" &orm and � ispla2 ispla2s s !hether !hether it� s even even or odd odd � ;t�s &ac &acto tori rial al � Multiplication table Solution: $'M" ode: html*   head*   title*+title* meta http-e>uivontent-'2pe contentte%t+html= charset.'/-0*   +head*   bod2* &orm action4ven/actMulsp method74'* hO*alculate /actorial , 4ven+uivontent-'2pe contentte%t+html= charset.'/-0* title*9SP Page+title*   +head*   bod2*  String str= int n=   strre>uestgetParameter(num)=   n;ntegerparse;nt(str)=* hO*ispla2 /actorial +hO*  tr2 ? int &act1,=   &or(1=n=FF) ?   &act&actL= H outprint(/actorial o& F nF is F&act)= H catch(4%ception e) ? H * @ int m= * hO*ispla2 4E4N+uest= ava%servlethttp$ttpServlet8esponse=

WebServlet(name7cd"cmServlet, urlPatterns?+7cd"cmServletH) public class 7cd"cmServlet e%tends $ttpServlet ?   49I private 7cd"cmIean ob= protected void do7et($ttpServlet8e>uest re>uest, $ttpServlet8esponse response) thro!s Servlet4%ception, ;uestg a;ntegerparse;nt(re>uestgetParameter(t1))= etParameter(t1))= int b;ntegerparse;nt(re>uestg b;ntegerparse;nt(re>uestgetParameter(tJ))= etParameter(tJ))= outprintln(7 o& t!o numbers is: Fobgcd(a, b))=   outprintln(br*)= outprintln("M o& t!o numbers is: Foblcm(a, b))= H &inall2 ?   outclose()= H H H 9SP ode: page content'2pete%t+html page4ncoding.'/-0* @lL=

import ava%!sWebService= import ava%!sWebMethod= import ava%!sWebParam= WebService(serviceName  'rade3WS) public class 'rade3WS ?   WebMethod public String give8ate(WebParam(name  item) String item) ? String rate1= onnection connull= tr2 ? lass&orName(orgapachederb2dbc4mbeddedriver)= conriverManagergetonnection(dbc:derb2:++localhost:16J+db1)= H catch(4%ception e) ? S2stemerrprintln(e)= H tr2 ? Statement stconcreateStatement()= 8esultSet rsste%ecuteUuer2(Select L &rom trade !here ;nde%K F item F K)= !hile(rsne%t()) ? ratersgetString(J)= H H catch(SU"4%ception e) ? S2stemerrprintln(e)= H return rate= H H

9SP ode 1: page content'2pete%t+html page4ncoding.'/-0* @uestgetParameter(selitem)= tr2 ? trade!s'rade3WS port  ne! trade!s'rade3WS()= String resultportgive8ate(str)= outprintln(8ate is  Fresult)= H catch(4%ception e)?H * +bod2* +html*

9SP ode J: page content'2pete%t+html page4ncoding.'/-0* @uivontent-'2pe contentte%t+html= charset.'/-0* title*9SP Page+title* +head* bod2* &orm actionNAA3'estsp methodP
View more...


Copyright ©2017 KUPDF Inc.