Servlet 学习4 GET方法

知识来源

RUNNOOB.COM

Get方法

  • GET 方法向页面请求发送已编码的用户信息。页面和已编码的信息中间用 ? 字符分隔。例如:

    http://localhost:8080/t/HelloForm?name=Form小可爱&url=www.nonameming.com

  • GET 方法是默认的从浏览器向 Web 服务器传递信息的方法,它会产生一个很长的字符串,出现在浏览器的地址栏中;

  • 不适合传密码等敏感信息;

  • GET 方法有大小限制:请求字符串中最多只能有 1024 个字符;

  • 这些信息使用 QUERY_STRING 头传递,并可以通过 QUERY_STRING 环境变量访问,Servlet 使用 doGet() 方法处理这种类型的请求。

Servlet读取表单数据例子

Servlet:

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;

/**
 * @author: runnob
 * @printer: ming
 * Servlet Implementation Class HelloForm
 */
@WebServlet("/HelloForm")
public class HelloForm extends HttpServlet {
    private static final long serialVerisonUID = 1L;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public HelloForm()
    {
        super();
        // TODO Auto-generated constructor stub
    }

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

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

        //处理中文
        String name =new String(request.getParameter("name").getBytes("ISO8859-1"),"UTF8");
        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>:"
                + name + "能不能显示" + "\n" +
                "  <li><b>网址</b>:"
                + request.getParameter("url") + "\n" +
                "</ul>\n" +
                "</body></html>");
    }

    // 处理 POST 方法请求的方法
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
    {
        doGet(request, response);
    }
}

个人xml配置 — 因人而异

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
         version="4.0">
    <servlet>
        <servlet-name>HelloForm</servlet-name>
        <servlet-class>HelloForm</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>HelloForm</servlet-name>
        <url-pattern>/HelloForm</url-pattern>
    </servlet-mapping>
</web-app>

表单html

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>HelloForm</title>
</head>
<body>
<form action="HelloForm" method="GET">
网址名:<input type="text" name="name">
<br />
网址:<input type="text" name="url" />
<input type="submit" value="提交" />
</form>
</body>
</html>

总结

  • 要熟悉创建、编码、配置、部署这四步。

  • 如何创建项目之前的博客有发,多重复就记住了。

  • 编码中遇到了一些方法:

    1.servlet:**getParameter():**调用 request.getParameter() 方法来获取表单参数的值;

    2.html:form 中 的 action 指向我的 Servlet,method 表明它是 GET 方法;网址名 name 和 网址 url 与 HelloForm 中的 String 名对应。

  • 配置因人而异,和自己的创建时的文件结构有关,错误时 IDE 会报出,这点我还不是很熟练;

  • 本地部署的话,就是 IDEA 中 Run 自己的项目了,记得看 IDE 有没有报错信息,我看的是 IDEA 中的 Output,记得第一次部署报错就是看 Output 找到并改正的。接下来就是去自己的浏览器输入相应地址了。拿这个例子和我的电脑来讲 **http://localhost:8080/t/HelloForm?name=Form小可爱&url=www.nonameming.com ** 可以直接访问 HelloForm 中写好的界面,我一开始直接输入 http://localhost:8080/t/HelloForm 浏览器 GET 不到他它想要的报NullPointerException 我还是先把 name 设为 固定的一个 String 访问到、看懂了自己照敲的代码才明白自己逻辑上的错误的。所以说基础知识还是挺重要的哈。在这之后的东西我都了解过一点点,所以没出什么问题。那今天就先总结到这里。

猜你喜欢

转载自blog.csdn.net/qq_40677350/article/details/90722938