美文网首页
Servlet与表单

Servlet与表单

作者: kylelin | 来源:发表于2014-08-06 19:11 被阅读34次

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>

相关文章

网友评论

      本文标题:Servlet与表单

      本文链接:https://www.haomeiwen.com/subject/ivketttx.html