Servlet 学习 Day5

写在前面

  • 前面的一段时间中,因为学校安排多了起来。把Servlet的学习拖延了很久。本来想直接弃掉学框架去,后来想想这就像空中楼阁吧。还是老老实实先学我的Servlet。一个点一个点的坚持下去;

  • 知识依旧来源于 RUNOOB.COM 代码也是照着敲的。说实话还不是很懂,不过看着那些代码基本能明白它的意思;

  • 我觉得自己照着敲主要的还是理解、体会、练打字。自嘲的点就是,自己越来越会打字,越来越不会体会和理解。我觉得这里有惰性。OK,正经起来,思考一定得主动而积极。懒惰是做不了好程序员的。我这小白一定得做到多反思啊!

内容概要

  • POST方法
  • 传递复选框数据到Servlet
  • 读取所有表单的参数

POST方法

HelloForm2.java
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * Servlet implementation class HelloForm
 */
@WebServlet("/HelloForm2")
public class HelloForm2 extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public HelloForm2() {
        super();
    }

    /**
     * @see HttpServlet#doGet(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 设置响应内容类型
        response.setContentType("text/html; charset = UTF-8");

        PrintWriter out = response.getWriter();
        String title = "使用 POST 方法读取表单数据";

        // 处理中文
        String name = new String(request.getParameter("name").getBytes("ISO8859-1"), "UTF-8");
        String docType = "<!DOCTYPE html>\n";
        out.println(
                "<html>\n" +
                "<head><title>" + title + "</head></title>\n" +
                "<body bgcolor =\"#f0f0f0\">\n" +
                "<h1 align=\"center\">" + title + "</h1>\n" +
                "<ul>\n" +
                "  <li><b>站点名</b>:"
                + name + "\n" +
                "  <li><b>网址</b>:"
                + request.getParameter("url") + "\n" +
                "</ul>\n" +
                "</body></html>");
    }

    // 处理 POST 方法请求的方法
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
    {
        doGet(request, response);
    }
}
hello.html
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>Servlet POST 方法实例</title>
</head>
<body>
<form action="HelloForm" method="GET">
    网址名:<input type="text" name="name">
    <br />
    网址:<input type="text" name="url" />
    <input type="submit" value="提交" /> <br /> <br />
    <input type="submit" value="submit" />
</form>
</body>
</html>

复选框数据传递

CheckBox.java
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * Servlet implementation class Checkbox
 */
@WebServlet("/CheckBox")
public class CheckBox extends HttpServlet {
    public static final long serialVersionUID = 1L;

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
    {

        // 设置响应内容
        response.setContentType("text/html; charset = UTF - 8");

        PrintWriter out = response.getWriter();
        String title = "读取复选框数据";
        String docType = "<!DOCTYPE html> \n";
        out.println(docType +
                "<html>\n" +
                "<head><title>" + title + "</title></head>\n" +
                "<body bgcolor=\"#f0f0f0\">\n" +
                "<h1 align=\"center\">" + title + "</h1>\n" +
                "<ul>\n" +
                "  <li><b>测试一标识:</b>: "
                + request.getParameter("runoob") + "\n" +
                "  <li><b>测试二标识:</b>: "
                + request.getParameter("google") + "\n" +
                "  <li><b>测试三标识:</b>: "
                + request.getParameter("taobao") + "\n" +
                "</ul>\n" +
                "</body></html>");
    }

    // 处理 POST 方法请求的方法
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
    {
        doGet(request, response);
    }
}
checkbox.html
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>POST方法关联测试页面</title>
</head>
<body>
<form action = "Checkbox" method = "POST" target = "_blank">
    <input type = "checkbox" name = "test1" checked = "checked" /> 测试一
    <input type = "checkbox" name = "test2" checked = "checked" /> 测试二
    <input type = "checkbox" name = "test3" checked = "checked" /> 测试三
    <input type = "submit" value="站点选择测试" />
</form>
</body>
</html>

读取所有表单参数

ReadParams.java
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Enumeration;

/**
 * Servlet implementation class ReadParams
 */
@WebServlet("/ReadParams")
public class ReadParams extends HttpServlet {
    private static final long serialVersionUID = 2L;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public ReadParams()
    {
        super();
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
    {
        // 设置响应内容
        response.setContentType("text/html; charset = UTF-8");
        PrintWriter out = response.getWriter();
        String title = "读取所有的表单数据";
        String docType =
                "<!doctype html public \"-//w3c//dtd html 4.0 " +
                        "transitional//en\">\n";
        out.println(docType +
                "<html>\n" +
                "<head><meta charset=\"utf-8\"><title>" + title + "</title></head>\n" +
                "<body bgcolor=\"#f0f0f0\">\n" +
                "<h1 align=\"center\">" + title + "</h1>\n" +
                "<table width=\"100%\" border=\"1\" align=\"center\">\n" +
                "<tr bgcolor=\"#949494\">\n" +
                "<th>参数名称</th><th>参数值</th>\n"+
                "</tr>\n");

        Enumeration paramNames = request.getParameterNames();

        while (paramNames.hasMoreElements())
        {
            String paramName = (String)paramNames.nextElement();
            out.print("<tr><td>" + paramName + "</td>\n");
            String[] paramValues = request.getParameterValues(paramName);

            // 读取单个值的数据
            if (paramValues.length == 1)
            {
                String paramValue = paramValues[0];
                if (paramValue.length() == 0) out.print("<td><i>没有值</i></td>");
                else out.println("<td>" + paramValue + "</td>");
            }
            else
                // 读取多个值的数据
            {
                out.println("<td><ul>");
                for (int i = 0; i < paramValues.length; i++)
                {
                    out.println("<li>" + paramValues[i]);
                }
                out.println("</ul></td>");
            }
            out.print("</tr>");
        }
        out.println("\n</table>\n</body></html>");
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
    {
        doGet(request, response);
    }
}
param.html
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>拿所有表单数据</title>
</head>
<body>

<form action = "ReadParams" method = "post" target = "_blank">
    <input type = "checkbox" name = "math" checked = "checked" /> 数学
    <input type = "checkbox" name= "physics" checked = "checked" /> 物理
    <input type = "checkbox" name = "chemistry" checked = "checked"> 化学
    <input type = "submit" value = "选择学科" />
</form>

</body>
</html>

总结

  • 这三个页面逻辑结构一样,先声明继承;再写doGet方法;最后写doPost方法;
  • doGet方法当然要随着页面变化而做出不同的处理,不过处理顺序也很相似,都在代码里了;
  • 配置xml还有这个Demo项目的文件逻辑我还在搞,现在有点乱,我搞清楚之后,专门写一次总结来说这个。毕竟小白,不懂的细枝末节总是卡自己,所以今天写完代码干脆没部署,先简单总结一下。搞清部署之后再总结。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值