input.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>InputServlet</title>
</head>
<body>
<form action="InputServlet" method="post">
输入内容:<input type="text" name="info" >
<input type="submit" value="提交">
</form>
</body>
</html>
InputServlet.java
package im.weitang;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class InputServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charSet=UTF-8");
String info = request.getParameter("info");
PrintWriter out = response.getWriter();
out.println("<html>");
out.println("<head>");
out.println("<title>InputServlet</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1>" + info + "</h1>");
out.println("</body>");
out.println("</html>");
out.close();
}
}
修改web.xml,添加下面的代码
<servlet>
<servlet-name>InputServlet</servlet-name>
<servlet-class>im.weitang.InputServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>InputServlet</servlet-name>
<url-pattern>/InputServlet</url-pattern>
</servlet-mapping>