servlet 将输入内容通过拼接页面的方式显示出来
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <!-- charset="utf-8" 页面编码 --> <title>ReturnName</title> </head> <body> <form action="ReturnNameServlet" method="post"> <input type="text" name="textname"> <input type="submit" value="提交"> </form> </body> </html>
returnname.html
package controller; import java.io.IOException; 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; /** * Servlet implementation class ReturnNameServlet */ @WebServlet("/ReturnNameServlet") public class ReturnNameServlet extends HttpServlet { private static final long serialVersionUID = 1L; /** * Default constructor. */ public ReturnNameServlet() { // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub request.setCharacterEncoding("utf-8"); response.setCharacterEncoding("utf-8"); //设置编码字符集 String username=request.getParameter("textname"); response.setContentType("text/html"); PrintWriter out = response.getWriter(); out.print("<head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\"></head><body>"); out.print("<body>submitname is "); out.print(username); out.print("</body>"); out.flush(); out.close(); } }
ReturnNameServlet.java