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