初学servlet之form表单

Posted 我想回家

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了初学servlet之form表单相关的知识,希望对你有一定的参考价值。

使用doget向页面写表单,使用dopost读取表单内容,包括了单选按钮,复选框,下拉列表等等的读取方法,form的action属性不写,默认表单会被提交给请求他时用的相同的URL

package app01b;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Enumeration;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 如何访问:
 *     http://localhost:端口号(使用tomcat没有修改过的是8080)/项目名/下面WebServlet中的urlPatterns
 *  如果使用的是web.xml配置文件,那么访问路径就变为:
 *  http://localhost:端口号(使用tomcat没有修改过的是8080)/项目名/web.xml中该servlet对应的url-pattern
 * @author Administrator
 *
 */
@WebServlet( name = "FormServlet", urlPatterns = "/form")
public class FormServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
    private static final String TITLE = "Order Form";
       

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html");
        PrintWriter writer = response.getWriter();
        writer.println("<html>");
        writer.println("<head>");
        writer.println("<title>" + TITLE + "</title></head>");
        writer.println("<body><h1>" + TITLE + "</h1>");
        // action属性默认,表示该表单会被提交给请求他时用的相同的URL
        writer.println("<form method = ‘post‘>");
        writer.println("<table>");
        writer.println("<tr>");
        writer.println("<td>Name:</td>");
        writer.println("<td><input name=‘name‘/></td>");
        writer.println("</tr>");
        writer.println("<tr>");
        writer.println("<td>Address:</td>");
        writer.println("<td><textarea name=‘address‘ "
                + "cols=‘40‘ rows=‘5‘></textarea></td>");
        writer.println("</tr>");
        writer.println("<tr>");
        writer.println("<td>Country:</td>");
      
        writer.println("<td><select name=‘country‘>");
        writer.println("<option>United States</option>");
        writer.println("<option>Canada</option>");
        writer.println("</select></td>");
        writer.println("</tr>");
        writer.println("<tr>");
        writer.println("<td>Delivery Method:</td>");
        // 单选按钮的不同,有value属性,意味着后台获取需要用到
        writer.println("<td><input type=‘radio‘ " +
                "name=‘deliveryMethod‘"
                + " value=‘First Class‘/>First Class");
        writer.println("<input type=‘radio‘ " +
                "name=‘deliveryMethod‘ "
                + "value=‘Second Class‘/>Second Class</td>");
        writer.println("</tr>");
        writer.println("<tr>");
        writer.println("<td>Shipping Instructions:</td>");
        writer.println("<td><textarea name=‘instruction‘ "
                + "cols=‘40‘ rows=‘5‘></textarea></td>");
        writer.println("</tr>");
        writer.println("<tr>");
        writer.println("<td>&nbsp;</td>");
        writer.println("<td><textarea name=‘instruction‘ "
                + "cols=‘40‘ rows=‘5‘></textarea></td>");
        writer.println("</tr>");
        writer.println("<tr>");
        writer.println("<td>Please send me the latest " +
                "product catalog:</td>");
        writer.println("<td><input type=‘checkbox‘ " +
                "name=‘catalogRequest‘/></td>");
        writer.println("</tr>");
        writer.println("<tr>");
        writer.println("<td>&nbsp;</td>");
        writer.println("<td><input type=‘reset‘/>" +
                "<input type=‘submit‘/></td>");
        writer.println("</tr>");
        writer.println("</table>");
        writer.println("</form>");
        writer.println("</body>");
        writer.println("</html>");
    }

    
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html");
        PrintWriter writer = response.getWriter();
        writer.println("<html>");
        writer.println("<head>");
        writer.println("<title>" + TITLE + "</title></head>");
        writer.println("</head>");
        writer.println("<body><h1>" + TITLE + "</h1>");
        writer.println("<table>");
        writer.println("<tr>");
        writer.println("<td>Name:</td>");
        // 使用getParameter(name)来获取表单数据
        writer.println("<td>" + request.getParameter("name")
                + "</td>");
        writer.println("</tr>");
        writer.println("<tr>");
        writer.println("<td>Address:</td>");
        writer.println("<td>" + request.getParameter("address")
                + "</td>");
        writer.println("</tr>");
        writer.println("<tr>");
        writer.println("<td>Country:</td>");
        // 下拉列表也使用getParameter来获取
        writer.println("<td>" + request.getParameter("country")
                + "</td>");
        writer.println("</tr>");
        writer.println("<tr>");
        writer.println("<td>Shipping Instructions:</td>");
        writer.println("<td>");
        // 两个文本域(textarea),还有包含多个值的select元素(允许选择多个选项并且用<select multiple>表示的select元素)发出的字符串数组,通过getParameterValues来获取
        String[] instructions = request
                .getParameterValues("instruction");
        if (instructions != null) {
            for (String instruction : instructions) {
                writer.println(instruction + "<br/>");
            }
        }
        writer.println("</td>");
        writer.println("</tr>");
        writer.println("<tr>");
        writer.println("<td>Delivery Method:</td>");
        writer.println("<td>"
                + request.getParameter("deliveryMethod")
                + "</td>");
        writer.println("</tr>");
        writer.println("<tr>");
        writer.println("<td>Catalog Request:</td>");
        writer.println("<td>");
        // 获取复选框(checkbox)的内容,选中返回on,没选中返回null
        if (request.getParameter("catalogRequest") == null) {
            writer.println("No");
        } else {
            writer.println("Yes");
        }
        writer.println("</td>");
        writer.println("</tr>");
        writer.println("</table>");
        writer.println("<div style=‘border:1px solid #ddd;" +
                "margin-top:40px;font-size:90%‘>");

        writer.println("Debug Info<br/>");
        // 获取所有的内容(暂时理解为键值对的形式吧)
        Enumeration<String> parameterNames = request
                .getParameterNames();
        while (parameterNames.hasMoreElements()) {
            String paramName = parameterNames.nextElement();
            writer.println(paramName + ": ");
            String[] paramValues = request
                    .getParameterValues(paramName);
            for (String paramValue : paramValues) {
                writer.println(paramValue + "<br/>");
            }
        }
        writer.println("</div>");
        writer.println("</body>");
        writer.println("</html>");
    }

}

以上是关于初学servlet之form表单的主要内容,如果未能解决你的问题,请参考以下文章

在html中建一个form表单 如何向servlet提交数据

在java中写jsp网页和servlet中,form表单的提交方法get与post的区别?

form表单提交数据到后台java

JSP页面Form表单文本框内容传到servlet乱码问题

java 之 servlet如何获取表单的数据

如何为Servlet类配置url以及form表单动作元素的写法。