@WebServlet(urlPatterns = "/form")
public class FormServlet extends HttpServlet { protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String username = request.getParameter("username"); response.getWriter().write("哈哈"); System.out.println(username); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request,response); } }
@WebFilter(urlPatterns = "/*") public class ChinaFilter implements Filter { @Override public void init(FilterConfig filterConfig) throws ServletException {} @Override public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { //在过滤器中,设置request对象的编码表 servletRequest.setCharacterEncoding("utf-8"); //设置response缓冲区的编码表,通知浏览器的解码 servletResponse.setContentType("text/html;charset=utf-8"); //放行 filterChain.doFilter(servletRequest,servletResponse); } @Override public void destroy() {} }
<body> <form action="${pageContext.request.contextPath}/form" method="post"> <input type="text" name="username"> <input type="submit"> </form> </body>
原文地址:https://www.cnblogs.com/xiaomingVVV/p/11332802.html
时间: 2024-10-07 20:56:34