您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 咨询培训 > 吴宇航_习题7_servlet部分
1.部署并运行一个简单的后台程序写一个简单的servlet,用writer对象输出一个静态的页面,上面有一个姓名框,有一个密码输入框,还有一个提交注册按钮。答:packagetest;importjava.io.IOException;importjava.io.Writer;importjavax.servlet.ServletException;importjavax.servlet.http.HttpServlet;importjavax.servlet.http.HttpServletRequest;importjavax.servlet.http.HttpServletResponse;publicclassListServletextendsHttpServlet{publicvoiddoGet(HttpServletRequestrequest,HttpServletResponseresponse)throwsServletException,IOException{doPost(request,response);}publicvoiddoPost(HttpServletRequestrequest,HttpServletResponseresponse)throwsServletException,IOException{request.setCharacterEncoding(utf-8);response.setCharacterEncoding(utf-8);response.setContentType(text/html);Writerw=response.getWriter();w.append(!DOCTYPEHTMLPUBLIC\-//W3C//DTDHTML4.01Transitional//EN\);w.append(body);w.append(formaction='Post.do'metion='post');w.append(姓名:inputtype='test'name='name'+br);w.append(密码:inputtype='password'name='pass'+br);w.append(inputtype='submit'name='sub'value='提交');w.append(/form);w.append(/BODY);w.flush();w.close();}}2.写一个servlet2,和上一题输出的页面连接,就是当提交按钮点击的时候就会把输入框的数据提交到这个servlet,在servlet2中得到和打印输入框里的值。答:packagetest;importjava.io.IOException;importjava.io.PrintWriter;importjava.io.Writer;importjavax.servlet.ServletException;importjavax.servlet.http.HttpServlet;importjavax.servlet.http.HttpServletRequest;importjavax.servlet.http.HttpServletResponse;publicclassPostServletextendsHttpServlet{publicvoiddoGet(HttpServletRequestrequest,HttpServletResponseresponse)throwsServletException,IOException{doPost(request,response);}publicvoiddoPost(HttpServletRequestrequest,HttpServletResponseresponse)throwsServletException,IOException{request.setCharacterEncoding(utf-8);response.setCharacterEncoding(utf-8);response.setContentType(text/html);Stringname=request.getParameter(name);Stringpass=request.getParameter(pass);Writerout=response.getWriter();out.append(!DOCTYPEHTMLPUBLIC\-//W3C//DTDHTML4.01Transitional//EN\);out.append(HTML);out.append(HEADTITLEAServlet/TITLE/HEAD);out.append(BODY);out.append(欢迎您:+name+br+您的登录密码为:+pass);out.append(/BODY);out.append(/HTML);out.flush();out.close();}}3.写一个servlet验证requestDispather的forward和response.sendRedirect自己说明体会答:packagetest;importjava.io.IOException;importjavax.servlet.ServletException;importjavax.servlet.http.HttpServlet;importjavax.servlet.http.HttpServletRequest;importjavax.servlet.http.HttpServletResponse;/***@authoryuh*两个转向de区别:*(1):*request.getRequestDispather().forward(),是服务器端的跳转,地址栏无变化;*response.sendRedirect()是客户端的跳转,地址栏发生变化。*(2):*request.getRequestDispather().forward()可以用requset.getParameter或或者requset.getAtrribute获得上一页面传递的参数;*response.sendRedirect()则不行。*(3):*response.sendRedirect()客户端再次发送请求,也就是相当于再浏览器地址栏里输入了另外一个地址,然后按了下回车一样;*request.getRequestDispather().forward()则不行,它只能在服务器端内查找转跳。*/publicclassJumpServletextendsHttpServlet{publicvoiddoGet(HttpServletRequestrequest,HttpServletResponseresponse)throwsServletException,IOException{doPost(request,response);}publicvoiddoPost(HttpServletRequestrequest,HttpServletResponseresponse)throwsServletException,IOException{response.setCharacterEncoding(utf-8);request.setCharacterEncoding(utf-8);response.setContentType(text/html);System.out.println(=======开始转跳!========);Stringname=request.getParameter(name);request.setAttribute(flag,name);//(1)语法格式----request.getRequestDispatcher().forward(request,response);request.getRequestDispatcher(T1.do).forward(request,response);//(2)语法格式----response.sendRedirect();//response.sendRedirect();}}4.在第三题这个程序的页面上加上一个静态页面,显示提交成功,用forward直接跳到这个页面。答:(1)forwaid页面packagetest;importjava.io.IOException;importjava.io.PrintWriter;importjava.util.Date;importjavax.servlet.ServletException;importjavax.servlet.http.HttpServlet;importjavax.servlet.http.HttpServletRequest;importjavax.servlet.http.HttpServletResponse;publicclassJumpServletextendsHttpServlet{publicvoiddoGet(HttpServletRequestrequest,HttpServletResponseresponse)throwsServletException,IOException{doPost(request,response);}publicvoiddoPost(HttpServletRequestrequest,HttpServletResponseresponse)throwsServletException,IOException{response.setCharacterEncoding(utf-8);request.setCharacterEncoding(utf-8);response.setContentType(text/html);PrintWriterout=response.getWriter();out.print(页面转前:+newDate());request.getRequestDispatcher(JJ.do).forward(request,response);out.print(页面转后:+newDate());}}(2)转跳的页面packagecom.servlet;importjava.io.IOException;importjava.io.PrintWriter;importjavax.servlet.ServletException;importjavax.servlet.http.HttpServlet;importjavax.servlet.http.HttpServletRequest;importjavax.servlet.http.HttpServletResponse;publicclassT1ServletextendsHttpServlet{publicvoiddoGet(HttpServletRequestrequest,HttpServletResponseresponse)throwsS8ervletException,IOException{doPost(request,response);}publicvoiddoPost(HttpServletRequestrequest,HttpServletResponseresponse)throwsServletException,IOException{request.setCharacterEncoding(utf-8);response.setCharacterEncoding(utf-8);response.setContentType(text/html);Pri
本文标题:吴宇航_习题7_servlet部分
链接地址:https://www.777doc.com/doc-5957394 .html