10、Servlet——Response对象

目录

一、response响应

二、response应用

1、 response主要方法

2、代码演示

3、解决中文输出乱码

(1)解决方式一

(2)解决方式二


一、response响应

response对象用于响应客户请求并向客户端输出信息

二、response应用

1、 response主要方法

2、代码演示

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>获取数据</title>
</head>
<body>
<form action="RequestServlet" method="post">
    <p><label>账号:</label><input type="text" name="user"></p>
    <p><label>密码:</label><input type="password" name="password"></p>
    <p><input type="submit" value="登录"></p>
</form>

</body>
</html>
package com.java.servlet;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet(name = "RequestServlet", value = "/RequestServlet")
public class RequestServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String user = request.getParameter("user");
        String password = request.getParameter("password");
        System.out.println("user:"+user+"\npassword:"+password);

        //获取输出流
        PrintWriter printWriter = response.getWriter();
        printWriter.println("<html>");
        printWriter.println("<head><title>login success</title></head>");
        printWriter.println("<body>");
        printWriter.println("<h2>amazing!</h2>");
        printWriter.println("</body>");
        printWriter.println("</html>");

    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);

    }
}

3、解决中文输出乱码

package com.java.servlet;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet(name = "RequestServlet", value = "/RequestServlet")
public class RequestServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        //获取输出流
        PrintWriter printWriter = response.getWriter();
        printWriter.println("<html>");
        printWriter.println("<head><title>login success</title></head>");
        printWriter.println("<body>");
        printWriter.println("<h2>纹身噶,黑社会?!</h2>");
        printWriter.println("</body>");
        printWriter.println("</html>");

    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);

    }
}

如果输出内容包含中文,则出现乱码,因为服务器默认采用ISO8859-1编码响应内容  

(1)解决方式一

设置服务器端响应的编码格式和设置客户端响应内容的头内容的文件类型及编码格式,这种方式不推荐

package com.java.servlet;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet(name = "RequestServlet", value = "/RequestServlet")
public class RequestServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //设置编码格式
        response.setCharacterEncoding("utf-8");
        response.setHeader("Content-type","text/html;charset=utf-8");
        //获取输出流
        PrintWriter printWriter = response.getWriter();
        printWriter.println("<html>");
        printWriter.println("<head><title>login success</title></head>");
        printWriter.println("<body>");
        printWriter.println("<h2>纹身噶,黑社会?!</h2>");
        printWriter.println("</body>");
        printWriter.println("</html>");

    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);

    }
}

(2)解决方式二

 同时设置服务端的编码格式和客户端响应的文件类型及响应时的编码格式,推荐此方式

package com.java.servlet;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet(name = "RequestServlet", value = "/RequestServlet")
public class RequestServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //设置编码格式
        response.setContentType("text/html;charset=utf-8");
        //获取输出流
        PrintWriter printWriter = response.getWriter();
        printWriter.println("<html>");
        printWriter.println("<head><title>login success</title></head>");
        printWriter.println("<body>");
        printWriter.println("<h2>纹身噶,黑社会?!</h2>");
        printWriter.println("</body>");
        printWriter.println("</html>");

    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);

    }
}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值