HttpServletResponse响应对象发送响应消息体

getOutputStream()//获取字节流输出对象

这个实例将展示用户输入用户名、密码登录后,然后返回文本“登录成功,欢迎你”

实例
get.html




    
    get请求


    

用户登录

用户名:

密 码:

HelloServlet.java

package net.test.servlet;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(name="helloServlet",urlPatterns="/parameter")  //1、配置servlet
public class HelloServlet extends HttpServlet {  //2、继承HttpServlet

    //3、重写doPost方法
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    }

    //3、重写doGet方法
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取请求参数
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        System.out.println("用户名:"+username);
        System.out.println("密码:"+password);

        //响应
        String result="登录成功,欢迎你";
        ServletOutputStream outputStream = resp.getOutputStream();
        outputStream.write(result.getBytes());

    }
}

访问浏览器:
HttpServletResponse响应对象发送响应消息体_第1张图片
表单输入用户名、密码:

HttpServletResponse响应对象发送响应消息体_第2张图片
运行结果:
在这里插入图片描述
HttpServletResponse响应对象发送响应消息体_第3张图片

你可能感兴趣的:(javaweb)