2021-05-24

form创建一个可提交的表单

<%--
  Created by IntelliJ IDEA.
  User: wyl
  Date: 2021/5/24
  Time: 8:52
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>request练习</title>
</head>
<body>
<form method="get" action="servlet.jsp">
    <table>
        <tr>
            <td>用户名</td>
            <td><input type="text" name="username" placeholder="请输入用户名"></td>
        </tr>
        <tr>
            <td>密码</td>
            <td><input type="password" name="password" placeholder="请输入密码"></td>
        </tr>
        <tr>
            <td>爱好</td>
            <td>
                <input type="checkbox" name="hobby" value="eat">吃
                <input type="checkbox" name="hobby" value="drink">喝
                <input type="checkbox" name="hobby" value="play">玩
                <input type="checkbox" name="hobby" value="happy">乐
            </td>
        </tr>
        <tr>
            <td><input type="submit" value="提交"></td>
            <td><input type="reset" value="重置"></td>
        </tr>
    </table>
</form>
</body>
</html>

接下来创建接收上面提交的数据当前页面为后端

<%@ page import="java.util.Arrays" %><%--
  Created by IntelliJ IDEA.
  User: wyl
  Date: 2021/5/24
  Time: 8:58
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>表单提交页面</title>
</head>
<body>
<%
    request.setAttribute("msg","当前用户是超级vip用户,一定要好好判断用户名和密码");
    /*设置请求的编码格式*/
    request.setCharacterEncoding("utf-8");
    /*在这个页面要获取index.jps 页面提交过来的数据*/
    /* 当前页面的角色是后端*/
    /* 因为前端发送了请求,所有的数据都在请求中,所以我们要通过请求来获取用户填写的数据*/
    /* 通过表单的名字来获取内容*/
    String username = request.getParameter("username");
    String password = request.getParameter("password");
    /* 获取name相同的数据,比如获取用户爱好*/
    String[] hobbies = request.getParameterValues("hobby");
    out.print("用户名"+username);
    out.print("密码"+password);
    out.print("用户的爱好"+ Arrays.toString(hobbies));
    /*获取属性,因为生命内容都可以设置所以内容是object的,所有要强转*/
    String msg = (String) request.getAttribute("msg");
    out.print("前台页面信息:"+msg);
    if (username.equals("admin")&&password.equals("1234567")){
        //这叫转发
        request.getRequestDispatcher("success.jsp").forward(request,response);
    }else{
        //这叫重定向
        response.sendRedirect("error.jsp");
    }
%>
</body>
</html>

转发的含义就是由第一个页面发到第二个页面然后第二个页面拿到第三个页面之后返回第三个页面

<%--
  Created by IntelliJ IDEA.
  User: wyl
  Date: 2021/5/24
  Time: 11:21
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<h1>
    欢迎回来<%out.print(request.getParameter("username"));%>
</h1>
</body>
</html>

重定向的含义是第一个页面提交到第二个页面第二个页面直接返回给第一个页面然后在提交一次转到

最后一个页面

<%--
  Created by IntelliJ IDEA.
  User: wyl
  Date: 2021/5/24
  Time: 11:19
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<h1 style="color: red;text-align: center">
    用户名或者密码不正确
</h1>
</body>
</html>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值