• Servlet的doGet与doPost方法的区别与使用


    Servlet的doGet与doPost方法的区别与使用

    一,区别

    在使用表单提交数据到服务器的时候有两张方式可共选择,一个是post一个是get。可在<form>中的method属性中指定提交的方式。如:<form action="inputForm"method="get">,如果不指定method属性,则会默认该属性为”get”方式。

    Get和post都能够提交数据,那么他们有什么不同呢?

    不同点一:

    通过get方式提交的数据有大小的限制,通常在1024字节左右。也就是说如果提交的数据很大,用get方法就可需要小心;而post方式没有数据大小的限制,理论上传送多少数据都可以。

    不同点二:

    通过get传递数据,实际上是将传递的数据按照”key,value”的方式跟在URL的后面来达到传送的目的的;而post传递数据是通过http请求的附件进行的,在URL中并没有明文显示。

    不同点三:

    通过Get方式提交的数据安全性不高,而Post方式的更加安全~

    二,使用

    下面举个例子说明:

    1.post提交--doPost方法

    login.jsp

     

    1. <%@ page language="java" contentType="text/html; charset=UTF-8"  
    2.     pageEncoding="UTF-8"%>  
    3. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  
    4. <html>  
    5. <head>  
    6. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  
    7. <title>登录</title>  
    8. </head>  
    9. <body>  
    10.       <h3>登录</h3>  
    11.       <hr>  
    12.       <form action="LoginServlet" method="post">  
    13.                 用户名:<input type="text" name="username"/><br>  
    14.                  密码:<input type="password" name="password"/><br>  
    15.               <input type="submit" />  
    16.       </form>  
    17.   
    18. </body>  
    19. </html>  
    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>登录</title>
    </head>
    <body>
          <h3>登录</h3>
          <hr>
          <form action="LoginServlet" method="post">
                    用户名:<input type="text" name="username"/><br>
                     密码:<input type="password" name="password"/><br>
                  <input type="submit" />
          </form>
    
    </body>
    </html>
    LoginServlet:

     

    1. protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {  
    2.         // TODO Auto-generated method stub  
    3.         request.setCharacterEncoding("UTF-8");  
    4.         response.setCharacterEncoding("UTF-8");  
    5.         //向服务器发送请求获取到参数  
    6.         String username=request.getParameter("username");  
    7.         String password=request.getParameter("password");  
    8.         System.out.println(username+"--"+password);  
    9.           
    10.         response.setHeader("Content-Type", "text/html; charset=UTF-8");  
    11.         Writer out=response.getWriter();  
    12.         out.write("用户名:"+username);  
    13.         out.write("密码:"+password);  
    14.         out.flush();  
    15.         out.close();      
    16.     }  
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    		// TODO Auto-generated method stub
    		request.setCharacterEncoding("UTF-8");
    		response.setCharacterEncoding("UTF-8");
    		//向服务器发送请求获取到参数
    		String username=request.getParameter("username");
    		String password=request.getParameter("password");
    		System.out.println(username+"--"+password);
    		
    		response.setHeader("Content-Type", "text/html; charset=UTF-8");
    		Writer out=response.getWriter();
    		out.write("用户名:"+username);
    		out.write("密码:"+password);
    		out.flush();
    		out.close();	
    	}
    效果图:


    这就是Post方式提交和doPost方法使用的效果,是不是更安全呢~~~

    2.get方式--doGet方法

    login.jsp:

     

    1. <%@ page language="java" contentType="text/html; charset=UTF-8"  
    2.     pageEncoding="UTF-8"%>  
    3. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  
    4. <html>  
    5. <head>  
    6. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  
    7. <title>登录</title>  
    8. </head>  
    9. <body>  
    10.       <h3>登录</h3>  
    11.       <hr>  
    12.       <form action="LoginServlet" method="get">  
    13.                 用户名:<input type="text" name="username"/><br>  
    14.                  密码:<input type="password" name="password"/><br>  
    15.               <input type="submit" />  
    16.       </form>  
    17.   
    18. </body>  
    19. </html>  
    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>登录</title>
    </head>
    <body>
          <h3>登录</h3>
          <hr>
          <form action="LoginServlet" method="get">
                    用户名:<input type="text" name="username"/><br>
                     密码:<input type="password" name="password"/><br>
                  <input type="submit" />
          </form>
    
    </body>
    </html>
    LoginServlet:

     

    1. @Override  
    2.     protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {  
    3.         // TODO Auto-generated method stub  
    4.         request.setCharacterEncoding("UTF-8");  
    5.         response.setCharacterEncoding("UTF-8");  
    6.         //向服务器发送请求获取到参数  
    7.         String username=request.getParameter("username");  
    8.         String password=request.getParameter("password");  
    9.         System.out.println(username+"--"+password);  
    10.           
    11.         response.setHeader("Content-Type", "text/html; charset=UTF-8");  
    12.         Writer out=response.getWriter();  
    13.         out.write("用户名:"+username);  
    14.         out.write("密码:"+password);  
    15.         out.flush();  
    16.         out.close();  
    17.     }  
    @Override
    	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    		// TODO Auto-generated method stub
    		request.setCharacterEncoding("UTF-8");
    		response.setCharacterEncoding("UTF-8");
    		//向服务器发送请求获取到参数
    		String username=request.getParameter("username");
    		String password=request.getParameter("password");
    		System.out.println(username+"--"+password);
    		
    		response.setHeader("Content-Type", "text/html; charset=UTF-8");
    		Writer out=response.getWriter();
    		out.write("用户名:"+username);
    		out.write("密码:"+password);
    		out.flush();
    		out.close();
    	}
    效果图:


     

    看这个效果图是不是觉得用户名和密码都暴露了呢~~这样很不安全~
    3.也可以post方式提交,然后在doGet方式写逻辑代码,不过要在doPost方法中调用doGet方法,同样get方式也是一样的道理~~~

    另外,除了doGet和doPost方法之外,还有doPutdoDeletedoTracedoHeaddoOptions,但使用的比较少。 

     

  • 相关阅读:
    JS Array转JSON
    js数组转字符串并用,分割
    java枚举类-根据key获取value及根据value获取key
    CSS文件引入顺序
    git pull之前要先commit
    FastJson中@JSONField注解使用
    @JsonFormat与@DateTimeFormat注解的使用
    Jackson 时间格式化,时间注解 @JsonFormat 用法、时差问题说明
    shell脚本使用
    ubuntu12.04 安装redis
  • 原文地址:https://www.cnblogs.com/nickup/p/9115479.html
Copyright © 2020-2023  润新知