action /项目名/虚拟路径名
    image.png

    1. package com.itheima.web;
    2. import javax.servlet.*;
    3. import javax.servlet.http.*;
    4. import javax.servlet.annotation.*;
    5. import java.io.IOException;
    6. /**
    7. * 中文乱码问题解决方案
    8. */
    9. @WebServlet("/req4") //action /项目名/虚拟路径名
    10. public class RequestDemo4 extends HttpServlet {
    11. @Override
    12. protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    13. // 1. 解决乱码:POST,getReader()是读取流 获取的是一个输入流的数据,设置编码
    14. request.setCharacterEncoding("UTF-8"); // 设置字符输入流的编码
    15. // 2. 获取username getParameter是读取字符串
    16. String username = request.getParameter("username");// 获取表单中参数为name = username的对应值(就是文本框输入的用户名)
    17. System.out.println(username);
    18. String password = request.getParameter("password");
    19. System.out.println(password);
    20. }
    21. @Override
    22. protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    23. this.doGet(request, response);
    24. }
    25. }