Web jsp開發學習——Servlet提交表單時用法
阿新 • • 發佈:2019-04-05
obb valid border har 圖片 stub req parse ror
實現提交表單以後判斷輸入的信息是否符合條件
若符合條件
先新建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; } }
Web jsp開發學習——Servlet提交表單時用法