package com.servlet;import java.io.IOException;import java.io.PrintWriter;import java.net.URLDecoder;import javax.servlet.ServletException;import javax.servlet.annotation.WebServlet;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;/** * Servlet implementation class ajaxServlet */@WebServlet("/ajaxServlet")public class ajaxServlet extends HttpServlet {private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public ajaxServlet() { super(); // TODO Auto-generated constructor stub }@Overrideprotected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {System.out.println(request.getContextPath());request.setCharacterEncoding("utf-8");response.setContentType("text/html;charset=utf-8");PrintWriter out =response.getWriter();String param1=request.getParameter("param1");String param2=request.getParameter("param2");//解决GET方式提交时候的中文乱码问题if(request.getMethod().equalsIgnoreCase("GET")){param1=URLDecoder.decode(request.getParameter("param1"),"utf-8");param2=URLDecoder.decode(request.getParameter("param2"),"utf-8");}System.out.println(param1+" "+param2);// System.out.println(request.getParameter("param2"));out.write("前台传来了参数:param1="+param1+",param2="+param2);out.flush();out.close();}}
html与servlet用ajax以url传参数乱码问题解决
最新推荐文章于 2019-02-22 21:55:50 发布