以下这些代码都是一些基础代码,只是为了在后续在写代码的时“偷懒”有类似的处理的时候直接用,大家也可以随意使用,(本人编程小白,代码比较基础,还请大佬指点)
表单页面
一 简易版
<form method="post" action="LoginServlet1">
用户名<input type="text" name="username" ><br>
密  码<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>