《2023年高级网站设计实验报告.docx》由会员分享,可在线阅读,更多相关《2023年高级网站设计实验报告.docx(15页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。
1、高级网站设计实验报告班级:信管 姓名:实验时间:2 0 2 3年5月2日指导教师:一、实验目的式.掌握Servlet J av a Bean的应用。2 .掌握EL表达式。3 .掌握MVC应用。二、实验内容1 .编写S e rvlel,显示一个Exc e I或者PDF的成绩单表格。2 .编写JavaBean,实现用户登录解决。3 .运用MVC架构,实现用户登录、注册解决。三、操作环节1、编写Ser v let,显示一个Exc e 1或者PDF的成绩单表格p ackag e Zhu 1 in;imp o rt java, i o .IOEx c e p tio n ;import ja v a.
2、io.P r in t W r i t e r;impo r t j a vax.s e r vlet.ServletE x c eption;impo r t javax.serv 1 ct. anno t a t i on.Wc b Servlet;i mpo r t j a v a x .servle t ,h( t p. HttpServ let;i mport ja v ax.se r vlet. http.HttpSe r vl e tReq u est;import javax.ser v 1 et.ht t p .HttpSe r vletRe s po n se;/ * Se
3、r v let imp 1 e m e nta t i o n cla s s mm欢迎登录欢迎您v%=n a me %/ h tm 1 Tc s t sever 1 et.jsp% p a ge lang u a ge=java conte n t Typ e = text /html; ch a rsef=UTF-8 page E n cod i n g = UTF-8% I n s ert t i t 1 e h e ret it 1 e welcom e tab 1 e wid t h=3 78 borde r =0 a 1 i g n= center 欢迎登录 用户名 V/ 1 ab
4、e 1 密 码td h eig h t =7 2 co 1 span= 2 a 1 i gn= c还没注册?先 注册v/ a 吧v/td欢迎登录用户名密码提且重置还没注册?先注册吧log i n Acti o n. j avap a c k a g e a c t i on;i m port java.io. IOE x cept i on;import j ava x.servlet. Req u e s tDi s pa t c h e r;import j a v ax.s e rv 1 et.Se r vl e tE x ce p tio n ; i m p ort j a v ax.
5、servlet, http. Http S e r vl e t ;import ja v a x.s e rv 1 e t.htt p .Htt p Se r vletRcquest;i m p o rt j a vax.ser v let.http.Htt p S e rvle t Resp o nse;impor t Dao. us e rDao;i mp o r t U se r . u se r ;publi c class 1 o g inAc t ion ex t ends HttpS e r vlet p r ivate s t atic fina 1 long s e r i
6、 al V ersionUID = 1L;boo 1 ean isE x it = f a Is e ;叩 u blic 1 oginAction() 。 super();)r ot e etc d vo i d d oGct ( H ttp S e r v 1 ctRc q u c st r e q ues t ,HttpSe r v 1 etRes p o nse respo n se) t hr o w s S e rvl e tExc e pt i o n, I OExcc p t i o n d oP o s t (re q ues t , re s p o n s e);p r o
7、 t e c t ed voi d do P o st (HttpSer v letR e qu e s t r e ques t ,。 0Ht tpSe r vletResp o n s e respons e ) t hr o w s Se r v le t E x ceptio n , I 0 Exce p t i o n rcque s t.setCharact e rEnc o ding(utf-8);s resp o nse. set C h aract e rEn c oding ( u tf-8);e spo n s c.se t C o ntcntTy p c(tcxt/ht
8、ml; c harsct= utf 8 ); user u = new user();g u s e r Dao u dg u s e r Dao u dnew use rDao ();。i sEx i t = false;gS t ring name = r e quest.ge t P a r a mete r (us e rn);S t r i ng pwd = reque s t.getParameter (pw d );i f(name= I I pwd= ) r espon s e.se n dRedirect(servlelTe s l.jsp);。 ret urn; e 1 s
9、e ou.setName(nam e );g u. s elPwd(pw d );gisE x i t = ud.quc r y User(u);if( i sExi t)(。 e quest.set A ttribut e (user u, u);。R equestDisp ate her d i s pa t ch e r = req u e s t。 .g e tRe q uest D i s pate h er( admin, j sp);g d ispatche r .forw a rd (request, r e sp o nse);o。 e I s e 。 respon s e.
10、 s endRedire c t( f ail. j s p );System.out. pr i n t ln(isExi t + * act i on );)Rcgist e r A c t ion. java pac k a ge a cti o n; im p ort j ava. i o.IO E x c e p t i o n;impo r t jav a x. s e rvl e t.Requ e stDi spa tcher;impo r t ja v ax. se r v I e t .Se r vie t Excep t ion;import jav a x. s e r
11、vlet. http. Htt p S e rv 1 ct;i m p ort java x . t tpServletR e q u e s t;im p ort j a va x . s crvlct. h t t p.Ht t p S erv 1 e t R e spon s c;i m port D a o. u s e rDao;import User.use r ;public clas s r egisterA c ti o n e x t en d s Ht t pS e rvle t 叩r i v a t e stati c f inal long seri a 1 V e
12、rsi o n U ID = IL;o p ublic regi s terAction() supc r ();0)s p rotected v oid doG e t(H npServ 1 e tRequ e s t reques t ,。 HttpSer v 1 e tRe s ponse res p o n s e ) throw s S e r v 1 e tE x ce p tion, IO E xception 。d oPost( r equest, response);0)aprotected void doPo s t(HttpSe r vlctR e quest r e q
13、 u est,HttpSer v 1 e tRe s pons e re s pons e ) t h r o w s S e rvl e tExceptio n , 10 E x c e pti o n geq uest.se (Character E neo d i n g (u t f -8);resp o n se. s etC h aracterEnc o ding( utf- 8 );。res pons e . s etC o ntentT y p e (t e x t/h t ml;chars e t= ut f -8);。u se r u = new us e r ();u s
14、e r Dao ud = new userD a o();。 St r i n g name = r e quest.getPar a met c r( user );S t ri n g pwd = requesi.gelParameier(pwd);i f (name =| I pwd= or espons e ,se n JR e d ire c t(s e r v 1 e tT e st. jsp);g re t ur n ;g els e )ou. s ctNamc(nam e );u.s e t P wd(pw d );。 Boole a n isSave = ud.saveUse
15、r( u );g if( i sSa v e) 3r equ e s t.set A ttribute (user, u);。 RequestDi s pate her dispatcher = req u e stwM.geiRequ e s tD i spa t cher(admi n .js p ):。dispa t c her.fo r ward(r e quest, response);g)els e or e sponse.send R cdircct(fail. jsp);b us e rDAO. j avapa c k age Dao; import java.s q 1. C
16、 onnection;im p ort j a va.s q 1. Prepare d S tatem ent;impo r t ja v a. s q 1 . Re s ultS e t;import java.s ql.SQLEx c e pti o n;imp ort u t i LDB;imp ort U s e r. u s e r;pu b lie c 1 ass use r Dao boo 1 ea n b = fal s e;pu b 1 ic boolean q uery U ser(user u) ab = f a Ise;aS t ri n g sql = s elect
17、 * from user where user name=? and passw o r d =?dC o n n e ctio n con n = DB. g e tCo n n ();。 Pre p a r e d Sta t emen t p s t m t = nu 1 1;wpstmt = DB. p repare(con n , sql);Mr y p stmt.setString (1, u.getName();opstmt.se t St r i ng(2, u.getP wd();。 Results e t rs = pstmt.e x ecut e Query ();&wh
18、ile (rs. next() ) 。 b = tr u e;。 System.out.p r intln (u. g e t Pw d () + DAO );g catch (SQL Exc e ption e) 。 e.prin t StackTrac e ();1rctu r n b;p u b 1 i c bo o 1 ean save User(user u) 。 b = fa 1 s e ;s String s q 1 = i nsert into user v a lues ( n ul 1 ,?,?)Connec t i o n c o n n = D B. g ctC o n
19、 n ();Prep a r e d S t at e ment p stmt = n ull;叩stm t = DB.prepar e ( c onn, sq 1 );try (conn.setAu t oCommit(fa 1 se);pstmt. s e tSt r i n g( 1, u.ge t Name。);p s t m t . s e i S tri n g(2, u. g e t Pwd();p s tmt.execu t eUpd a t e ();c onn. s e t Au t o C o mmit ( t rue);= t r ue; catch (SQ L Exc
20、ep t i on e)。 e. p rintStac k T r ace();。 r etu r n b ;*User.javapa c k a g e Use r ;p ublic cla s s us e r aSt r i ng usernam e = null;String pwd = nu 1 1 ;publi c S t r ing g ctNamc () are t u rn us e rname;)public void sctNam e (Stri n g us e rnamc) 。t his.use r nam e = username;)。p ublic S tr i
21、ng g e tPw d () return p wd;0)pu b 1 i c void s e tPwd(Str i ng pwd) wthis.pwd = pwd;0)I四、实验收获和建议*/ WebServlet( / d ownsco r e )publ i c c 1 a s s mm ext e nd s Ht t p S e r v 1 e t (叩 riv a te s t atic f i nal lo n g serialVe r s i on UI 77 = 1 L ;priva t e s ta t i c fina 1 S t r i ng CONT BNT_TYP
22、E= a pp 1 i cati o n / v n d .ms-down 1 oad; c h arset=GB 1 80 3 0 ;叩ri v ate st a ti c f inal String CONTE NT_ FILE_NAME = a t tachinent;f i lename=score.xl s ;。p rotected v oid s e rvice( H t t p Se r v 1 etRe q uestr e que s t ,0HHp S e rvle t R e spons e resp o n s e ) throws Se r v1 etE x c e p
23、tio n,IOEx ceplion g r e s p o nse. s e tCont en t T y pe(C ON TENT_T YPE); re s po n s e. s e tHea d e r ( Co n t ent-D i s p osit i on, CONTENT_FI LE_NAME);3 P r i n tWr i t er out = response, ge t Writer();axmt.printl n ( );out.p r intln姓名 V / td 英语成绩v/tdx/tr);&out.println(K-FLV / tdxtd 8 9);out.
24、prin t王梦92V/t d );out.print 1 n (H);ao u t.clo s e();您要打开还是保存此文件?您要打开还是保存此文件?称型源类score.xlsMicrosoft Excel 工作表localhost打开 保存6| 取消里贴板1Al麴I 1 件开 苒 些要 某不2、编写JavaBean,实现用户登录解决。Use r . javapa c k a ge m y model;publi c cl a s s User privat e String u s ername;叩riva t c S tr i ng p a s sw o rd;p ub 1 i c S
25、tri n g getU s ern a me() arctu r n u s ern a me;)op u b 1 ic voi d se t Us e rn a m e (Str i ng us e rname)(thi s . usern a me = usem a me;pub 1 i c Strin g g e tPa s swo rd () r et u rnr et u rnP as s word;叩u b lie voids et P ass w o r d(S t ri n g pas s word) s sword = passw o rd:IExelogi n jsp I
26、 n ser t tit 1 c he r e v%S tri n g url = j dbc:mys q 1: / /I o ca 1 host / mmH;St r i ng u s r = r o ot;String p wd 二 “ 007”;Nry Connec t ion co n n = D r ivcrMan a g e r .gctConncction(urI, usr, p wd);String s q 1 = M s e 1 ect i d from user w h ere use r n a m e =? a nd pas s wo r d =?n;Pre p a r
27、e d S t a t ement p s = conn, pre p areS t atement(sql);o ps.sctSt r ing(l, us e r.g e tU s ername();ops.setStrin g(2, use r . g e (Pass w or d ();。 Res u ItS e t r s = p s. exe c utcQ u ery();gif (rs.n e xt ()gq e spo n se. s e n d R e dir e ct(a d min. j sp);a elseeres p onse.se ndR e d irec t (1
28、o gin. js p );c o n n.c 1 ose(); ca t ch (Ex c e p tion e) e. p r intSta c kTra c e();。 %Login. j s p用户名密码I濯直I重置用户登录v/t i tle 用户名密码i n pu t t ype=,5 ubmitv a 1 u e =登录”/htm 1 恭喜你登录成功。 I nse r 11 i de h e re恭喜你登录成功。3、运用MVC架构,实现用户登录、注册解决R e g i s ter.jsp m e ta h ttp- e quiv= Content-Ty pe con t e nt=
29、 te x t / htm 1; char se t =UTF Inser t titl e h ere/ headh e a d we 1 c ome/ h e a d 欢迎登录 用户名密 码i n pu t ty p e= rese t M name= Submit2 v a lu e =/ 还没注册?先 Va h r ef=M re gis t erjsptarget=_b / a成注册吧v/1 dAdm i n .jsp% p age 1 anguage= Java co n ten t Type= te xt/h t ml; c hars e t =UTF-8 ”叩a g eEnc o d ing=U7F-8% page im p ort= Use r .user0/o ! D OCTYPEhtml P UBL I C ,-/W 3 C/ / D T D HTML 4.01 Tra n s itional/ZENMH