Servlet learning 4 GET method

Knowledge Source

RUNNOOB.COM

Get method

  • GET method request information transmitted encoded user to the page. Page and intermediate information encoded by? Characters separated. E.g:

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

  • GET method is the default method of conveying information from the browser to the Web server, it will produce a long string, it appears in the address bar of your browser;

  • Not suitable for sensitive information such as passwords pass;

  • GET method is limited in size: the request string can only have up to 1024 characters;

  • QUERY_STRING using header information transfer, and can be accessed through the QUERY_STRING environment variable, Servlet using doGet () method to handle this type of request.

Examples of the form data read 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);
    }
}

Personal xml configuration - varies

<?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 form

<!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>

to sum up

  • To be familiar with creating, coding, configuration, deployment of four steps.

  • How to create a blog before the project has, multiple repeats remember.

  • Coding encountered in a number of ways:

    1.servlet: ** getParameter (): ** request.getParameter call () method to get the value of the form parameter;

    2.html: form of action points to my Servlet, method indicate that it is GET method; URL name and URL name and url String name HelloForm corresponding.

  • Configuration varies, and when the file structure of your own creation about, when they report an error IDE, which I is not very skilled;

  • Local deployment, then, is the IDEA Run your own project, and I remember watching IDE have no information being given, I think is the Output IDEA, I remember the first deployment is to see Output error to find and correct. The next step is to go to your browser enter the address. Take this example and in terms of my computer ** HTTP: // localhost: 8080 / t / Form HelloForm name = url = www.nonameming.com small cute &? ** direct access to the written HelloForm interface, I started direct input http: // localhost: 8080 / t / HelloForm browser GET message that it wants to see his NullPointerException I'd better name to fixed access to a String, read their code according to knock came to understand their own wrong logical. So the basics still very important Ha. Something after that I had to understand a little bit, so not out of any problem. That concludes today on the first here.

Guess you like

Origin blog.csdn.net/qq_40677350/article/details/90722938