创建人 | 李晓东 |
问题标题 | 书写错误 |
问题分类 | Web |
附件 | @WebServlet(name = "responsedemo") public class responsedemo extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { System.out.println("<---demo---> "); request.setAttribute("msg","response"); response.sendRedirect("/responsedemo2"); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doPost(request,response); } } |
问题答案 | 问题分析: @WebServlet(name = "responsedemo")处错误由于一开始建文件的时候创建的是Servlet文件,代码自动补全括号内忘记修改 |
问题解决方法: 修改代码 @WebServlet("/responsedemo") |
创建人 | 李晓东 |
问题标题 | 手懒错误 |
问题分类 | Web |
附件 | @WebServlet("/responsedemo") public class responsedemo extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { System.out.println("<---demo---> "); request.setAttribute("msg","response"); response.sendRedirect("/responsedemo2"); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { } } |
问题答案 | 问题分析: protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { } 处错误,由于代码自动补全的重写方法,里面的参数没有修改。 |
问题解决方法: 修改代码 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doPost(request,response); } |
创建人 | 李晓东 |
问题标题 | 格式问题 |
问题分类 | Web |
附件 | @WebServlet("/responsedemo") public class responsedemo extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { System.out.println("<---demo---> "); request.setAttribute("msg","response"); response.sendRedirect("/responsedemo2"); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doPost(request,response); } } @WebServlet("/responsedemo2") public class responsedemo2 extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { System.out.println("demo2222222........"); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doPost(request,response); } } |
问题答案 | 问题分析: responsedemo2控制台中未书写getAttribute();方法以及sout打印输出。所以未获取responsedemo中的setAttribute方法写下的数据。 |
问题解决方法:在doPost方法中补写代码 添加代码Object msg = request.getAttribute("msg"); System.out.println(msg); |
创建人 | 李晓东 |
问题标题 | 格式问题 |
问题分类 | Web |
附件 | @WebServlet("/responsedemo3") public class responsedemo3 extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.getRequestDispatcher("/responsedemo2").forward(request,response); } protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { super.doGet(req, resp); } } |
问题答案 | 问题分析: doGet方法是代码自动补全,有些需要修改的数据并未修改 |
问题解决方法:修改代码 正确代码protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {this.doPost(request,response); } |
创建人 | 李晓东 |
问题标题 | 格式问题 |
问题分类 | Web |
附件 | @WebServlet("/responsedemo4") public class responsedemo4 extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html,charset=utf-8"); //获取字符输出流 PrintWriter pw = response.getWriter(); //输出数据 pw.write("<h1>你好</h1> response"); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doPost(request,response); } } |
问题答案 | 问题分析: response.setContentType("text/html,charset=utf-8");处格式错误 |
问题解决方法:修改代码 正确代码 response.setContentType("text/html;charset=utf-8"); |
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) | 黑马程序员IT技术论坛 X3.2 |