• 【servelt原理_9_servlet应用___】


    servlet应用***

    1.request对象
    是当客户端向服务器端发送请求时,服务器为本次请求创建request对象,并在调用Servlet的service方法时,将该对象传递给service方法。Request对象中封装了客户端发送过来的所有的请求数据。
    在这里插入图片描述
    ①:doGet()方法接收request数据
    编写html

    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="UTF-8">
    <title>Insert title here</title>
    </head>
    <body>
    	<form action="/SecondServlet/regist" method="get"> 
    		用户名:<input type="text" name="username"/>
    		密码:<input type="text" name="password"/>
    		<input type="submit" value="注册">
    	</form>
    </body>
    </html>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    编写doGet()方法

    @WebServlet("/regist")
    public class RegistServlet  extends HttpServlet{
    	
    	@Override
    	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    		// TODO Auto-generated method stub
    		String username = req.getParameter("username");
    		String password = req.getParameter("password");
    		
    		System.out.println(username + " " + password);
    	}
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    ②:doPost()方法接收request数据
    编写html

    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="UTF-8">
    <title>Insert title here</title>
    </head>
    <body>
    	<form action="/SecondServlet/regist" method="post"> 
    		用户名:<input type="text" name="username"/>
    		密码:<input type="text" name="password"/>
    		<input type="submit" value="注册">
    	</form>
    </body>
    </html>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    编写doPost()方法

    @WebServlet("/regist")
    public class RegistServlet  extends HttpServlet{
    	
    	
    	@Override
    	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    		// TODO Auto-generated method stub
    		String username = req.getParameter("username");
    		String password = req.getParameter("password");
    		
    		System.out.println(username + " " + password + "dopost");
    	}
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    ③:请求乱码问题
    由于request是接收来自用户的请求,服务器会根据编码格式将请求转换。服务器端默认的编码格式为ISO-8859-1(此编码不支持中文),而我们用户浏览器默认是utf-8的编码格式,所以往往会产生乱码。要想解决乱码问题,需要设置request当中的编码格式,告诉服务器以何种形式来解析数据。或者在接收到乱码以后,通过何种编码格式进行还原

    方式一:

    request.setCharacterEncoding("UTF-8");
    
    • 1

    该方法只针对POST有效(必须在接收数据之前设定)

    方式二:

    new String(request.getParameter(name).getBytes("ISO-8859-1"),"UTF-8"))
    
    • 1

    借助String对象的方法,该种方式对任何请求有效,都是通用的。
    Tomcat8以后的get请求时不会出现乱码的。

  • 相关阅读:
    canal集群部署及使用
    Salesforce ServiceCloud考证学习(6)
    Linux下为网卡分配IP的工具dhclient
    vue.js scss >>> 失效的问题
    SpringBoot属性注入
    【go从入门到精通】Context用法分析
    学习笔记---更进一步的双向链表专题~~
    笔试强训第16天
    linux常见环境安装
    Tomcat概述和基础优化
  • 原文地址:https://blog.csdn.net/edwer93/article/details/128143735