HttpServletRequest表单提交基础模板中文乱码问题及基本表格

博客分享了基础的HTML表单代码,包括简易版、带表格、留言板式的表单页面,介绍了基本表单格式,如from的method和action用法,tr、td、th的作用。还提及处理表单页面的方法,通过request.getParamete获取表单name,用equals判断后重定向,以及处理中文乱码问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

以下这些代码都是一些基础代码,只是为了在后续在写代码的时“偷懒”有类似的处理的时候直接用,大家也可以随意使用,(本人编程小白,代码比较基础,还请大佬指点)

表单页面

一 简易版

在这里插入图片描述

<form method="post" action="LoginServlet1">
    用户名<input type="text" name="username" ><br>&numsp;<input type="password" name="passworld" ><br>
    <input type="submit" value="提交" >
</form>
二、 带表格

在这里插入图片描述

基本的表单格式
from里面的method用来说明提交方式,action用来跳转页面
tr和td和th只有在table里面才能生效。
一个tr标记对表示一行,
一个td标记对表示一个单元格
一个th标记对表示行标题(有加粗的作用)

<body bgcolor="aqua">
    <form name="from1" method="post" action="root02.jsp" >
        <table align="center" width="400" border="1" style="border: blue">
            <tr>
                <th align="center" height="60" colspan="2"><span style="font-size: 30px">用户登录</span></th>
            </tr>
            <tr height="30">
                <td align="righr">用户名:</td>
                <td align="center"><input type="text" name="username"></td>
            </tr>
            <tr height="30">
                <td align="righr">密  码:</td>
                <td align="center"><input type="password" name="password"></td>
            </tr>

            <tr align="center" height="30">
                <td colspan="2">
                    <input type="submit" value="登录" name="submit">
                    <input type="reset" value="重置" name="reset">
                </td>
            </tr>
        </table>
    </form>
三、留言板式

在这里插入图片描述

<form name="from1" method="post" action="checkMessage.jsp" >
    <table align="center" width="600" border="1" style="border: blue">
        <tr>
            <th align="center" height="60" colspan="2"><span style="font-size: 30px">欢迎同学们留言讨论</span></th>
        </tr>
        <tr height="30">
            <td align="righr" width="150">请输入姓名</td>
            <td align="center" width="200"><input type="text" name="username"></td>
        </tr>
        <tr height="30">
            <td align="righr" >请输入标题</td>
            <td align="center"><input type="text" name="ps"></td>
        </tr>
        <tr>
            <td align="righr">留言内容:</td>
            <td>
                <textarea cols="60" rows="10" name="message"></textarea>
                <br><br><br>
            </td>
        </tr>
        <tr align="center" height="30">
            <td colspan="2">
                <input type="submit" value="留言" name="submit">
            </td>
        </tr>
    </table>
</form>

处理表单页面

通过request.getParamete来获取到表单的name名字,然后通过if判断进行下一步
equals判断是否相同,然后重定向到新的页面

 		String User = request.getParameter("user");
        String Pass= request.getParameter("pass");

        if(User.equals("root") && Pass.equals("123456")){
            response.sendRedirect(request.getContextPath()+"welcome.html");//重定向到新的文件
        }else {
            response.sendRedirect(request.getContextPath()+"error.html");//重定向到新的文件
        }
    }

处理中文乱码问题

 response.setCharacterEncoding("UTF-8"); //设置服务器为utf-8
 request.setCharacterEncoding("UTF-8");//设置客户端为utf-8
 response.setContentType("text/html;charset=utf-8");//在响应头上设置下字符编码集就行

基本表单

在这里插入图片描述

<tr><td><h1 align="center">用户注册</h1></td></tr>
<table border="1" frame="void" align="center" bordercolor="red">
    <form name="form1" method="post" action="text02.jsp">

        <tr><td><p align="center">用户名:<input type="text" name="username"></p ></td></tr>
        <tr><td><p align="center">密  码:<input type="text" name="password"></p ></td></tr>
        <tr><td><p align="center">确认密码:<input type="text" name="password1"></p ></td></tr>
        <tr><td><p align="center">性  别:
            <input type="radio" name="education" value="1"><input type="radio" name="education" value="2"></p ></td></tr>
        <tr><td><p align="center">兴  趣:
            <input type="checkbox" name="hobby" value="1">看电影
            <input type="checkbox" name="hobby" value="2">敲代码
            <input type="checkbox" name="hobby" value="3">玩游戏
        </p ></td></tr>
        <tr><td><p align="center">家庭住址:
            <select name="add">
                <option value="1">--请选择--</option>
                <option value="2">山东</option>
                <option value="4">北京</option>
                <option value="5">广东</option>
            </select>
        </p ></td></tr>
        <tr><td><p align="center">
            <input type="submit" name="Submit" value="确定">
            <input type="reset" name="Cancel" value="取消">
        </td></tr>
        </p >
    </form>
</table>
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值