/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ import java.io.IOException; import java.io.PrintStream; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * * @author Administrator */ @WebServlet(name = "firstServlet", urlPatterns = {"/FirstServlet"}) public class FirstServlet extends HttpServlet { /** * * @param request * @param response * @throws ServletException * @throws IOException */ @Override protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // 设置解码方式 request.setCharacterEncoding("UTF-8"); response.setContentType("text/html;charset=UTF-8"); // 获取name的请求参数值 String name = request.getParameter("name"); // 获取gender的请求参数值 String gender = request.getParameter("gender"); // 获取color的请求参数值 String[] color = request.getParameterValues("color"); // 获取country的请求参数值 String national = request.getParameter("country"); // 获取页面输出流 PrintStream out = new PrintStream(response.getOutputStream()); //输出HTML页面标签 out.println("<html>"); out.println("<head>"); out.println("<title>Servlet测试</title>"); out.println("</head>"); out.println("<body>"); // 输出请求参数的值:name out.println("您的名字:" + name + "<hr/>"); // 输出请求参数的值:gender out.println("您的性别:" + gender + "<hr/>"); // 输出请求参数的值:color out.println("您喜欢的颜色:"); for (String c : color) { out.println(c + " "); } out.println("<hr/>"); out.println("您喜欢的颜色:"); // 输出请求参数的值:national out.println("您来自的国家:" + national + "<hr/>"); out.println("</body>"); out.println("</html>"); } }