javaweb的request和response

request应用

java类

package com.yan.Request;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Arrays;

public class loginServelet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String username = req.getParameter("username");
        String password = req.getParameter("password");

        String[] hobbys = req.getParameterValues("hobbys");

        System.out.println(username);
        System.out.println(password);
        System.out.println(Arrays.toString(hobbys));

//        转发时不用加/
//        req.getRequestDispatcher("/response_war_exploded/success.jsp").forward(req,resp);
        req.getRequestDispatcher(req.getContextPath()+"/success.jsp").forward(req,resp);

       /*
       请求转发的时候,url不会发生变化:307
       重定向时url地址栏会发生变化:302
       重定向时前面必须加一个/,/代表当前的web项目
        //重定向的时候,一定要注意路径问题,否则可能会404
        resp.sendRedirect("/response_war_exploded/success.jsp");
        */
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}

web.xml

<servlet>
    <servlet-name>doGet</servlet-name>
    <servlet-class>com.yan.Request.loginServelet</servlet-class>
</servlet>

    <servlet-mapping>
        <servlet-name>doGet</servlet-name>
        <url-pattern>/doGet</url-pattern>
    </servlet-mapping>

index.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<title>登录</title>
<body>
<h2>Hello World!</h2>
<div style="text-align: center">

    <form action="${pageContext.request.contextPath}/login" method="post">
<%--        以post方式提交表单,提交到我们的login请求--%>
        用户名:<input type="text" name="username"><br>
        密码:<input type="password" name="password"><br>
        爱好:
        <input type="checkbox" name="girl" value="女孩"> 女孩
        <input type="checkbox" name="code" value="代码">代码
        <input type="checkbox" name="sing" value="唱歌">唱歌
        <input type="checkbox"name="电影" value="电影">电影
        <br>
        <input type="submit">
    </form>

</div>
</body>
</html>

success.jsp

<%--
  Created by IntelliJ IDEA.
  User: chaojixingyun
  Date: 2022/8/19
  Time: 15:15
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>success</title>
</head>
<body>
<h2>登录成功</h2>
</body>
</html>

response

  String username = req.getParameter("username");
        String pwd = req.getParameter("pwd");
        System.out.println(username + ":" + pwd);

        //重定向的时候,一定要注意路径问题,否则可能会404
        resp.sendRedirect("/response_war_exploded/success.jsp");
 resp.setContentType("text/http");
        resp.setCharacterEncoding("utf-8");

        //        1,获取下载文件的路径
      //  String realPath = this.getServletContext().getRealPath("/1.png");
        String realPath="F:\\IDEAstudy\\ServeletStudy\\response\\src\\main\\resources\\1.png";
        System.out.println("获取到的路径:"+realPath);
//        2,下载的文件名
        String filename = realPath.substring(realPath.lastIndexOf("//") + 1);
//        3,设置让浏览器能够支持下载我们需要的东西
        resp.setHeader("Content-disposition","attachment;filename=" + URLEncoder.encode(filename,"utf-8"));
//        4,获取下载文件的输入流
        FileInputStream in = new FileInputStream(realPath);
//        5,创建缓冲区
        int len=0;
        byte[] buffer = new byte[1024];
//        6,获取outputstrame的对象
        ServletOutputStream out = resp.getOutputStream();
//        7,将fileoutputstrame流写入缓冲区
//        8,使用outputstrame将缓冲区中的数据输出到客户端
        while((len=in.read(buffer))!=-1){
            out.write(buffer,0,len);
        }
        in.close();
        out.close();
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

祈愿lucky

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值