0
点赞
收藏
分享

微信扫一扫

JAVAWEB入门简介,GET与POST方法简介

东言肆语 2022-03-11 阅读 68

JAVAWEB入门应用,GET与POST方法简介

1.GET与POST的区别

  • 1.Get是不安全的,因为在传输过程,数据被放在请求的URL中;Post的所有操作对用户来说都是不可见的。
  • 2.Get传送的数据量较小,这主要是因为受URL长度限制;Post传送的数据量较大,一般被默认为不受限制。
  • 3.GET一般用于下载,POST一般用于上传

2.HttpServlet

Login.java

package IMUST;

import jakarta.servlet.*;
import jakarta.servlet.http.*;
import jakarta.servlet.annotation.*;

import java.io.IOException;
import java.io.PrintWriter;

//@WebServlet(name = "Login", value = "/Login")
public class Login extends HttpServlet {
    //@Override
    //get方法
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        PrintWriter out = response.getWriter();
        out.println("<h1 style='color:blue'>" + "This is a GET method!!!" + "</h1>");
    }

    //@Override
    //post方法
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        PrintWriter out = response.getWriter();
        out.println("<h1 style='color:yellow'>" + "This is a POST method!!!" + "</h1>");
    }
}

同时,我们需要修改配置文件如下:

<?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>loginWay</servlet-name>
        <servlet-class>IMUST.Login</servlet-class>
    </servlet>

    <servlet-mapping>
        <servlet-name>loginWay</servlet-name>
        <url-pattern>/Login</url-pattern>
    </servlet-mapping>
</web-app>

前端(index.jsp)的三种测试get与post方法的代码:

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
  <head>
    <title>登录 - IMUST</title>
  </head>
  <body>
  <a href="/Login">点击跳转~</a>
  <hr>
  <h3>get方法表单</h3>
    <form action="/Login" method="get">
      用户名:<input type="text" name="userName"><br>
      密码:<input type="password" name="passWord"><br>
      重新输入密码:<input type="password" name="agPassword">
      <input type="submit" name="Submite">
    </form>
  <hr>
  <h3>post方法表单</h3>
  <form action="/Login" method="post">
    用户名:<input type="text" name="userName"><br>
    密码:<input type="password" name="passWord"><br>
    重新输入密码:<input type="password" name="agPassword">
    <input type="submit" name="Submite">
  </form>
  </body>
</html>


3.测试结果

在这里插入图片描述

举报

相关推荐

0 条评论