Request_获取请求参数中文乱码问题处理
中文乱码问题:
get方式:tomcat 8 已经将get方式乱码问题解决了
post方式:会乱码
解决:在获取参数前,设置request的编码:request.setCharacterEncoding("utf-8");
@WebServlet(name = "RequestDemo7", value = "/RequestDemo7") public class RequestDemo7 extends HttpServlet { @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doPost(request, response); } @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //设置编码 request.setCharacterEncoding("utf-8"); // post获取请求参数 String username = request.getParameter("username"); System.out.println(username); } }
Request_请求转发
请求转发:一种在服务器内部的资源跳转方式
1.步骤:
1、通过request对象获取请求转发器对象:RequestDispatcher getRequestDispatcher(String path);
2、使用RequestDispatcher对象来进行转发:forward(ServletRequest request,Response response);
demo8
@WebServlet(name = "RequestDemo8", value = "/RequestDemo8") public class RequestDemo8 extends HttpServlet { @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doPost(request, response); } @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { System.out.println("demo8....被访问了"); //转发到demo9资源 /*RequestDispatcher requestDispatcher = request.getRequestDispatcher("/RequestDemo9"); requestDispatcher.forward(request, response);*/ //链式编程 request.getRequestDispatcher("/RequestDemo9").forward(request, response); } }
demo9
@WebServlet(name = "RequestDemo9", value = "/RequestDemo9") public class RequestDemo9 extends HttpServlet { @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doPost(request, response); } @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { System.out.println("demo9....也被访问了"); } }
特点:
1、浏览器地址栏路径不会发生变化
2、只能转发到当前服务器内部资源中
3、转发是一次请求
标签:请求,Request,request,HttpServletResponse,乱码,转发,Override,response From: https://www.cnblogs.com/qihaokuan/p/16950436.html