Web jsp开发学习——Servlet提交表单时用法
Web jsp开发学习——Servlet提交表单时用法
实现提交表单以后判断输入的信息是否符合条件
若符合条件
先新建servlet
再到web.xml里注册
register.jsp就是表单的界面
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" errorPage="error.jsp"%> <% String ser_msg = (String)request.getAttribute("server_info"); %> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> </head> <link href="css/style.css" rel="stylesheet" type="text/css"/> <style> dl{width: 500px;} dt{width: 80px; float: left; height: 40px; text-align: right;} dd{width: 360px; float: left; height: 40px; margin: 0px 0px 0px 10px;} input[type=text],input[type=password]{width: 300px; height: 25px; border-radius: 10px; border solid 2px #999;} #main{ width:400px; margin:auto; } #main h2{ text-aligh:center; margin-left:100px; } #info{color:#f00; font-weight:bold;} </style> <script> </script> <body> <div id="main" > <h2>用户注册</h2> <!-- 提交,发生动作 reg_action.jsp--> <form action="reg" method="post"> <dl> <dt>用户名:</dt><dd><input type="text" name="usercode"></dd> <dt>密码:</dt><dd><input type="password" name="userpass"></dd> <dt>确认密码:</dt><dd><input type="password" name="confpass"></dd> <dt>姓名:</dt><dd><input type="text" name="username"></dd> <dt>Email:</dt><dd><input type="text" name="email"></dd> <dt>性别:</dt><dd><input type="radio" name="gender" value="0" checked> 男 <input type="radio" name="gender" value="1">女</dd> <dt>职业:</dt><dd><select name="occupation"> <option value="a">战士(Warrior)</option> <option value="b">坦克(Tank)</option> <option value="c">刺客(Assassin)</option> <option value="d">法师(Mage)</option> <option value="e">射手(Archer)</option> <option value="f">辅助(Support)</option> </select></dd> <dt>兴趣爱好:</dt><dd><input type="checkbox" name="hobby" value="吃饭">吃饭 <input type="checkbox" name="hobby" value="睡觉">睡觉 <input type="checkbox" name="hobby" value="豆豆">打豆豆</dd> <dt>出生日期:</dt><dd><input type="date" name="birthday"></dd> <dt>照片:</dt><dd><input type="file" name="photo"></dd> <dt>自我介绍:</dt><dd><textarea name="introduce"></textarea></dd> <dt></dt><dd><input type="checkbox" name="read" value="1">我已阅读《用户协议》</dd> <%=ser_msg == null? "":"<dt></dt><dd><span id='info'>"+ser_msg+"</span></dd>"%> <dt></dt><dd><input type="submit" value="注 册"> <input type="reset" value="取 消"></dd> </dl> </form> </div> </body> </html>
在servlet里的doGet里写判断语句
前端register.jsp获取servlet传来的msg
doGet代码
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub request.setCharacterEncoding("utf-8"); response.setCharacterEncoding("utf-8"); String ucode = request.getParameter("usercode"); String msg; RequestDispatcher rd = request.getRequestDispatcher("register.jsp"); PrintWriter out = response.getWriter(); out.print("usercode: " + ucode); if(ucode.equals("管理员")){ //response.sendRedirect("register.jsp");页面跳转 msg = ucode + "不能使用!"; request.setAttribute("server_info", msg); rd.forward(request, response); } if(ucode.length()<8 || ucode.length()>20){ msg = ucode+"长度不符合要求"; request.setAttribute("server_info", msg); rd.forward(request, response); } if(!Tools.checkChar(ucode)) { msg = ucode+"包含非法字符"; request.setAttribute("server_info", msg); rd.forward(request, response); } }
Tools.java是我用来专门处理不符合条件的一个java代码,全写进servlet里太乱了,之后要写入到Tools里
servlet前面导入一下
Tools.java的代码如下:
package com.xx17.cys.base; public class Tools { public static int getNum(String str) { int result = 0; try{ if(str.length()>0) { result = Integer.parseInt(str); } }catch(Exception e){ } return result; } /* * 判断是否包含非法字符 * 返回:包含(false)、不包含(true) * 修改:cys,2019-4-4 * */ public static boolean checkChar(String str) { boolean result = true; String validStr="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; for(int i=0; i<str.length();i++) { String s = str.substring(i, i+1); if(validStr.indexOf(s)==-1) { System.out.println(s+"非法字符!!!"); result = false; break; } } return result; } }