标签:tag att 医疗 dispatch 不同 check pattern ref public
近期在老师的引导下我们学习了利用Javaweb技术制作简单的管理系统,其中涉及到的技术很多,由于大多都是自学
对这些技术的理解还太浅显但能实现一些相关功能的雏形。
(一).登录功能
在登陆功能中通过与数据库的连接实现了验证用户账号与密码是否正确的功能,并添加了简易的权限设置使
不同用户登录成功后所呈现的主界面是不同的;同时还有验证码功能能检测验证码是否输入正确。
数据库:
jsp:
1 <%@ page language="java" contentType="text/html; charset=utf-8" 2 pageEncoding="utf-8"%> 3 <!DOCTYPE html> 4 <html> 5 <head> 6 <meta charset="utf-8"> 7 <title>登录</title> 8 9 </head> 10 <body> 11 <% 12 Object message = request.getAttribute("message"); 13 if (message != null && !"".equals(message)) { 14 %> 15 <script type="text/javascript"> 16 alert("<%=request.getAttribute("message")%>"); 17 </script> 18 <% 19 } 20 %> 21 <form action="Log" name="log" method="post"> 22 <div align="center"> 23 <h1>请登录</h1> 24 用户名:<input id ="username" name="username" type="text" value="" onChange="chang1(this.value)"> 25 <br> 26 密码:<input id ="password" name="password" type="password" value="" onChange="chang2(this.value)"> 27 <br> 28 验证码:<input type="text" id="inputCode" value="" onChange="validateCode()"> 29 <div id="checkCode" class="code" onclick="createCode(4)" style="display:inline" ></div> 30 <br> 31 <input type="submit" value="确定" > 32 33 </div> 34 </form> 35 <script type="text/javascript"> 36 window.onload=function(){ 37 createCode(4); 38 } 39 40 //生成验证码的方法 41 function createCode(length) { 42 var code = ""; 43 var codeLength = parseInt(length); //验证码的长度 44 var checkCode = document.getElementById("checkCode"); 45 ////所有候选组成验证码的字符,当然也可以用中文的 46 var codeChars = new Array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 47 ‘a‘,‘b‘,‘c‘,‘d‘,‘e‘,‘f‘,‘g‘,‘h‘,‘i‘,‘j‘,‘k‘,‘l‘,‘m‘,‘n‘,‘o‘,‘p‘,‘q‘,‘r‘,‘s‘,‘t‘,‘u‘,‘v‘,‘w‘,‘x‘,‘y‘,‘z‘, 48 ‘A‘, ‘B‘, ‘C‘, ‘D‘, ‘E‘, ‘F‘, ‘G‘, ‘H‘, ‘I‘, ‘J‘, ‘K‘, ‘L‘, ‘M‘, ‘N‘, ‘O‘, ‘P‘, ‘Q‘, ‘R‘, ‘S‘, ‘T‘, ‘U‘, ‘V‘, ‘W‘, ‘X‘, ‘Y‘, ‘Z‘); 49 //循环组成验证码的字符串 50 for (var i = 0; i < codeLength; i++) 51 { 52 //获取随机验证码下标 53 var charNum = Math.floor(Math.random() * 62); 54 //组合成指定字符验证码 55 code += codeChars[charNum]; 56 } 57 if (checkCode) 58 { 59 //为验证码区域添加样式名 60 checkCode.className = "code"; 61 //将生成验证码赋值到显示区 62 checkCode.innerHTML = code; 63 } 64 } 65 66 //检查验证码是否正确 67 function validateCode() 68 { 69 //获取显示区生成的验证码 70 var checkCode = document.getElementById("checkCode").innerHTML; 71 //获取输入的验证码 72 var inputCode = document.getElementById("inputCode").value; 73 //console.log(checkCode); 74 //console.log(inputCode); 75 if (inputCode.length <= 0) 76 { 77 alert("请输入验证码!"); 78 } 79 else if (inputCode.toUpperCase() != checkCode.toUpperCase()) 80 { 81 alert("验证码输入有误!"); 82 createCode(4); 83 } 84 85 } 86 var reg=/^[a-zA-Z]\w*$/; 87 var flag1=false,flag2=false,flag3=false; 88 function chang1(obj) 89 { 90 var pattern = /^\w+$/i; 91 var arr = obj.match(pattern); 92 if(arr) 93 {} 94 else 95 alert("用户名格式不正确,4-10位数字/字母/下划线"); 96 if(obj.length<4||obj.length>10) 97 { 98 alert("用户名格式不正确,4-10位数字/字母/下划线"); } 99 } 100 function chang2(obj) 101 { 102 103 var pattern = /^\w+$/i; 104 var arr = obj.match(pattern); 105 if(arr) 106 {} 107 else 108 alert("密码格式不正确,4-10位数字/字母/下划线"); 109 if(obj.length<4||obj.length>10) 110 { 111 alert("密码格式不正确,4-10位数字/字母/下划线"); } 112 113 } 114 115 116 </script> 117 </body>
118 </html>
servlet:
1 package servlet; 2 3 import java.io.IOException; 4 import javax.servlet.ServletException; 5 import javax.servlet.annotation.WebServlet; 6 import javax.servlet.http.HttpServlet; 7 import javax.servlet.http.HttpServletRequest; 8 import javax.servlet.http.HttpServletResponse; 9 import javax.servlet.http.HttpSession; 10 11 import dao.UserDao; 12 13 /** 14 * Servlet implementation class Login 15 */ 16 @WebServlet("/Log") 17 public class Log extends HttpServlet { 18 19 20 /** 21 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) 22 */ 23 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 24 // TODO Auto-generated method stub 25 request.setCharacterEncoding("utf-8"); 26 String name=request.getParameter("username"); 27 String password=request.getParameter("password"); 28 UserDao x=new UserDao(); 29 int flag=x.checkUsers(name, password); 30 if(flag==1){ 31 System.out.println(name+"-"+password); 32 HttpSession session=request.getSession(); 33 session.setAttribute("name", name); 34 session.setAttribute("password", password); 35 response.sendRedirect("main_menu.jsp"); 36 } 37 else if(flag==-1){ 38 System.out.println("false"); 39 response.sendRedirect("log.jsp"); 40 } 41 else if(flag==2) 42 { 43 System.out.println("false"); 44 response.sendRedirect("create.jsp"); 45 } 46 } 47 48 49 /** 50 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) 51 */ 52 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 53 54 doPost(request, response); 55 } 56 57 }
Dao层:
1 package dao; 2 import bean.Account; 3 import bean.User; 4 import util.DButil; 5 import java.sql.*; 6 7 import java.sql.Connection; 8 import java.sql.PreparedStatement; 9 import java.sql.ResultSet; 10 import java.sql.SQLException; 11 import java.sql.Statement; 12 import java.util.ArrayList; 13 import java.util.List; 14 15 16 import bean.Account; 17 import util.DButil; 18 19 public class UserDao { 20 DButil DB =new DButil(); 21 private static UserDao userDao =new UserDao(); 22 23 public static UserDao getUserDao() 24 { 25 return userDao; 26 27 } 28 /*public boolean checkUser(String username ,String password) { 29 DButil DB = new DButil(); 30 String sql="select username,password from account where username=? and password=?"; 31 //String sql = "select username,password,jurisdiction from account where username=‘"+username+"‘ and password=‘"+password+"‘"; 32 Connection conn = null; 33 PreparedStatement ps = null; 34 boolean flag = false; 35 try { 36 conn = DB.getConn(); 37 ps = conn.prepareStatement(sql); 38 ps.setString(1, username); 39 ps.setString(2, password); 40 //ps.setInt(3, jurisdiction); 41 ResultSet rs = ps.executeQuery(); 42 if(rs.next()) 43 { 44 45 flag=true; 46 } 47 48 } catch (Exception e) { 49 50 flag=false; 51 e.printStackTrace(); 52 } finally { 53 DB.close(conn); 54 DB.close(ps); 55 } 56 return flag; 57 }*/ 58 public int checkUsers(String username ,String password) { 59 DButil DB = new DButil(); 60 String sql="select username,password ,jurisdiction from account where username=? and password=?"; 61 //String sql = "select username,password,jurisdiction from account where username=‘"+username+"‘ and password=‘"+password+"‘"; 62 Connection conn = null; 63 PreparedStatement ps = null; 64 int flag = -1; 65 try { 66 conn = DB.getConn(); 67 ps = conn.prepareStatement(sql); 68 ps.setString(1, username); 69 ps.setString(2, password); 70 //ps.setInt(3, jurisdiction); 71 ResultSet rs = ps.executeQuery(); 72 if(rs.next()) 73 { 74 75 flag=rs.getInt("jurisdiction"); 76 } 77 78 } catch (Exception e) { 79 80 flag=-1; 81 e.printStackTrace(); 82 } finally { 83 DB.close(conn); 84 DB.close(ps); 85 } 86 return flag; 87 } 88 89 90 public boolean addUser(User user) { 91 DButil DB = new DButil(); 92 String sql = "insert into middle_text(name,sex,nation,time,age,outlook,service) values(?,?,?,?,?,?,?)"; 93 Connection conn = null; 94 PreparedStatement ps = null; 95 boolean flag = false; 96 int a = 0; 97 98 try { 99 conn = DB.getConn(); 100 ps = conn.prepareStatement(sql); 101 ps.setString(1, user.getName()); 102 ps.setString(2, user.getSex()); 103 ps.setString(3, user.getNation()); 104 ps.setString(4, user.getTime()); 105 ps.setString(5, user.getAge()); 106 ps.setString(6, user.getOutlook()); 107 ps.setString(7, user.getService()); 108 a = ps.executeUpdate(); 109 110 if (a != 0) { 111 flag = true; 112 } 113 } catch (Exception e) { 114 System.out.println( "添加失败"); 115 flag=false; 116 e.printStackTrace(); 117 } finally { 118 DB.close(conn); 119 DB.close(ps); 120 } 121 return flag; 122 } 123 124 125 126 127 128 129 public boolean deleteUser(String name) { 130 DButil DB = new DButil(); 131 Connection conn = null; 132 Statement state = null; 133 String sql = ""; 134 boolean flag = false; 135 try { 136 conn = DB.getConn(); 137 sql = "delete from middle_text where name =‘" + name + "‘"; 138 state = conn.createStatement(); 139 int a = 0; 140 a = state.executeUpdate(sql); 141 if (a != 0) { 142 flag = true; 143 } 144 } catch (Exception e) { 145 // TODO: handle exception 146 } finally { 147 DB.close(conn); 148 DB.close(state); 149 } 150 151 return flag; 152 } 153 //? 154 /*public boolean loginUser(String userName, String password) { 155 156 DButil DB = new DButil(); 157 boolean flag = false; 158 Connection con = null; 159 MD5_Salt ms = new MD5_Salt(); 160 try { 161 con = DB.getConn(); 162 Statement state = con.createStatement(); 163 ResultSet rs = state.executeQuery("select * from user where username=‘" + userName + "‘"); 164 if (rs.next()) { 165 String dbPassword = rs.getString("dbpassword"); 166 String salt1 = rs.getString("salt1"); 167 String salt2 = rs.getString("salt2"); 168 flag = ms.TF_Password_DBpassword(password, dbPassword, salt1, salt2); 169 } 170 171 } catch (SQLException e) { 172 // TODO ???????? catch ?? 173 e.printStackTrace(); 174 } 175 return flag; 176 177 }*/ 178 179 // ???????? 180 public boolean resetUserPassword(String name,String sex,String outlook,String service) { 181 182 DButil DB = new DButil(); 183 boolean flag = false; 184 Connection conn = null; 185 Statement state = null; 186 int a = 0; 187 try { 188 String sql="update middle_text set sex=‘" + sex 189 + "‘,outlook=‘"+outlook+"‘,service=‘"+service+"‘where name= ‘" + name + "‘"; 190 conn = DB.getConn(); 191 state = conn.createStatement(); 192 a = state.executeUpdate(sql); 193 if (a > 0) { 194 flag = true; 195 } 196 197 } catch (SQLException e) { 198 // TODO ???????? catch ?? 199 e.printStackTrace(); 200 } 201 return flag; 202 203 } 204 /*public boolean searchuser(String username) 205 { 206 User user=new User(); 207 DButil DB = new DButil(); 208 boolean flag = false; 209 Connection conn = null; 210 Statement state = null; 211 int a = 0; 212 try { 213 214 //"select * from User"; 215 //select ename ,sal from emp where ename=‘SMITH‘; 216 String sql="select * from text where username= ‘"+username+"‘"; 217 //String sql="select password from text where username=‘" + username + "‘"; 218 conn = DB.getConn(); 219 state = conn.createStatement(); 220 ResultSet rs = state.executeQuery(sql); 221 222 if (rs != null) { 223 while(rs.next()) { 224 user.setId(rs.getString("id")); 225 user.setUsername(rs.getString("username")); 226 user.setPassword(rs.getString("password")); 227 } 228 System.out.println(user.getPassword()); 229 flag = true; 230 231 } 232 233 } catch (SQLException e) { 234 // TODO ???????? catch ?? 235 e.printStackTrace(); 236 } 237 return flag; 238 239 240 }*/ 241 242 // ??????? 243 /*public boolean resetUserPassword(String userName, String newPassword) { 244 245 DButil DB = new DButil(); 246 Salt salt = new Salt(); 247 MD5_Salt ms = new MD5_Salt(); 248 boolean flag = false; 249 Connection conn = null; 250 Statement state = null; 251 int a = 0; 252 try { 253 String salt1 = salt.getSalt(); 254 String salt2 = salt.getSalt(); 255 String dbPassword = ms.get_MD5Salt(newPassword, salt1, salt2); 256 String sql = "update user set dbpassword=‘" + dbPassword + "‘, salt1=‘" + salt1 + "‘, salt2= ‘" + salt2 257 + "‘where username= ‘" + userName + "‘"; 258 259 conn = DB.getConn(); 260 state = conn.createStatement(); 261 a = state.executeUpdate(sql); 262 if (a > 0) { 263 flag = true; 264 } 265 266 } catch (SQLException e) { 267 // TODO ???????? catch ?? 268 e.printStackTrace(); 269 } 270 return flag; 271 */ 272 273 // ?????л?? 274 /* public List<User> UserList() { 275 DButil DB = new DButil(); 276 String sql = "select * from text "; 277 List<User> list = new ArrayList<>(); 278 Connection conn = null; 279 Statement state = null; 280 ResultSet rs = null; 281 282 try { 283 conn = DB.getConn(); 284 state = conn.createStatement(); 285 rs = state.executeQuery(sql); 286 User user = null; 287 while (rs.next()) { 288 289 int id2 = rs.getInt("id"); 290 String userName2 = rs.getString("username"); 291 // String password2 =rs.getString("password"); 292 String name2 = rs.getString("name"); 293 user = new User(); 294 user.setId(id2); 295 user.setUsername(userName2); 296 list.add(user); 297 } 298 } catch (SQLException e) { 299 e.printStackTrace(); 300 } finally { 301 DB.close(conn); 302 DB.close(state); 303 DB.close(rs); 304 } 305 306 return list; 307 }*/ 308 }
(二).对志愿者操作功能
此部分为对志愿者的增删改操作
数据库:
主界面jsp:
1 <%@ page language="java" contentType="text/html; charset=utf-8" 2 pageEncoding="utf-8"%> 3 <!DOCTYPE html> 4 <html> 5 <head> 6 <script type="text/javascript"> 7 </script> 8 <meta charset="utf-8"> 9 <title>主菜单</title> 10 </head> 11 <body> 12 <a href="log.jsp">返回登录界面</a> 13 14 <div align="center"> 15 <h1>请选择功能</h1> 16 <br> 17 <a href="create.jsp">增加志愿者信息</a> 18 <br> 19 <a href="remove.jsp">删除志愿者信息</a> 20 <br> 21 <a href="update.jsp">修改志愿者信息</a> 22 <br> 23 <a href="mixsarch.jsp">查询志愿者信息</a> 24 <br> 25 <a href="show.jsp">显示志愿者信息</a> 26 27 28 </div> 29 </body> 30 </html>
增加志愿者jsp:
1 <%@ page language="java" contentType="text/html; charset=utf-8" 2 pageEncoding="utf-8"%> 3 <!DOCTYPE html> 4 <html> 5 <head> 6 <meta charset="utf-8"> 7 <script type="text/javascript"> 8 var c=0,limit=4; 9 function doCheck(obj) { 10 obj.checked?c++:c--; 11 if(c>limit){ 12 obj.checked=false; 13 alert("所选个数最大为4个 !!!"); 14 c--; 15 } 16 } 17 </SCRIPT> 18 <title>增加用户</title> 19 </head> 20 <body> 21 <% 22 Object message = request.getAttribute("message"); 23 if (message != null && !"".equals(message)) { 24 %> 25 <script type="text/javascript"> 26 alert("<%=request.getAttribute("message")%>"); 27 </script> 28 <% 29 } 30 %> 31 <a href="main_menu.jsp">返回主界面</a> 32 <form action="Adduser" name="create" method="post"> 33 <div align="center"> 34 <h1>请输入此成员信息</h1> 35 姓名:<input id="name" name="name" type="text" value="" 36 onChange="change1(this.value)"> <br> 性别: <input 37 id="sex" name="sex" type="radio" value="男"> 男 <input 38 id="sex" name="sex" type="radio" value="女"> 女 <br> 民族:<input 39 id="nation" name="nation" type="text" value="" 40 onChange="change3(this.value)"> <br> 注册时间:<input 41 id="time" name="time" type="text" value="" 42 onChange="change4(this.value)"> <br> 年龄:<input id="age" 43 name="age" type="text" value="" onChange="change4(this.value)"> 44 <br> 政治面貌:<select name="outlook"> 45 <option value="群众">群众</option> 46 <option value="共青团员">共青团员</option> 47 <option value="中共党员">中共党员</option> 48 </select> <br> 服务类别(最多选择四项): <br> <input type="checkbox" 49 name="service" value="扶危济困" onClick="doCheck(this)">扶危济困 <input 50 type="checkbox" name="service" value="敬老助残" onClick="doCheck(this)">敬老助残 51 <input type="checkbox" name="service" value="社区服务" 52 onClick="doCheck(this)">社区服务 <input type="checkbox" 53 name="service" value="秩序维护" onClick="doCheck(this)">秩序维护 <input 54 type="checkbox" name="service" value="文体服务" onClick="doCheck(this)">文体服务 55 <br> <input type="checkbox" name="service" value="环境保护" 56 onClick="doCheck(this)">环境保护 <input type="checkbox" 57 name="service" value="治安防范" onClick="doCheck(this)">治安防范 <input 58 type="checkbox" name="service" value="医疗救治" onClick="doCheck(this)">医疗救治 59 <input type="checkbox" name="service" value="法律援助" 60 onClick="doCheck(this)">法律援助 <input type="checkbox" 61 name="service" value="大型活动" onClick="doCheck(this)">大型活动 <br> 62 <input type="checkbox" name="service" value="心理疏导" 63 onClick="doCheck(this)">心理疏导 <input type="checkbox" 64 name="service" value="精神抚慰" onClick="doCheck(this)">精神抚慰 <input 65 type="checkbox" name="service" value="支教支医" onClick="doCheck(this)"> 66 支教支医 <input type="checkbox" name="service" value="科学普及" 67 onClick="doCheck(this)">科学普及 <input type="checkbox" 68 name="service" value="应急救援" onClick="doCheck(this)">应急救援 <br> 69 <input type="checkbox" name="service" value="便民服务" 70 onClick="doCheck(this)">便民服务 <input type="checkbox" 71 name="service" value="民事调解" onClick="doCheck(this)">民事调解 <input 72 type="checkbox" name="service" value="文明引导" onClick="doCheck(this)">文明引导 73 <input type="checkbox" name="service" value="安全生产" 74 onClick="doCheck(this)">安全生产 <input type="checkbox" 75 name="service" value="禁毒宣传" onClick="doCheck(this)">禁毒宣传 <br> 76 <input type="submit" value="确定"> 77 </div> 78 </form> 79 <script type="text/javascript"> 80 /* var reg=/^[a-zA-Z]\w*$/; 81 var flag1=false,flag2=false,flag3=false; 82 function change1(obj) 83 { 84 var pattern = /^\w+$/i; 85 var arr = obj.match(pattern); 86 if(arr) 87 {} 88 else 89 alert("用户名格式不正确,4-10位数字/字母/下划线"); 90 if(obj.length<4||obj.length>10) 91 { 92 alert("用户名格式不正确,4-10位数字/字母/下划线"); } 93 } 94 function change2(obj) 95 { 96 97 var pattern = /^\w+$/i; 98 var arr = obj.match(pattern); 99 if(arr) 100 {} 101 else 102 alert("密码格式不正确,4-10位数字/字母/下划线"); 103 if(obj.length<4||obj.length>10) 104 { 105 alert("密码格式不正确,4-10位数字/字母/下划线"); } 106 107 }*/ 108 </script> 109 </body> 110 </html>
servlet:
1 package servlet; 2 3 import java.io.IOException; 4 import java.io.PrintWriter; 5 6 import javax.servlet.ServletException; 7 import javax.servlet.annotation.WebServlet; 8 import javax.servlet.http.HttpServlet; 9 import javax.servlet.http.HttpServletRequest; 10 import javax.servlet.http.HttpServletResponse; 11 import bean.*; 12 import dao.UserDao; 13 14 /** 15 * Servlet implementation class Adduser 16 */ 17 @WebServlet("/Adduser") 18 public class Adduser extends HttpServlet { 19 private static final long serialVersionUID = 1L; 20 21 /** 22 * @see HttpServlet#HttpServlet() 23 */ 24 public Adduser() { 25 super(); 26 // TODO Auto-generated constructor stub 27 } 28 29 /** 30 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) 31 */ 32 33 34 35 36 37 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 38 39 int s=0; 40 request.setCharacterEncoding("utf-8"); 41 String name=request.getParameter("name"); 42 String sex=request.getParameter("sex"); 43 String nation=request.getParameter("nation"); 44 String time=request.getParameter("time"); 45 String age=request.getParameter("age"); 46 String outlook=request.getParameter("outlook"); 47 String []service = request.getParameterValues("service"); 48 User user=new User(); 49 user.setName(name); 50 user.setSex(sex); 51 user.setNation(nation); 52 user.setTime(time); 53 user.setAge(age); 54 user.setOutlook(outlook); 55 user.setService(service); 56 //UserDao userDao=UserDao.getUserDao(); 57 UserDao userDao=new UserDao(); 58 if(userDao.addUser(user)) 59 { 60 61 //request.setAttribute("message", "添加成功"); 62 response.setContentType("text/html;charset=utf-8"); 63 PrintWriter out = response.getWriter(); 64 //out.print("<script language=‘javascript‘>alert(‘添加成功‘);window.location.href=‘main_menu.jsp‘;</script>"); 65 out.print("<script language=‘javascript‘>alert(‘添加成功‘);window.location.href=‘main_menu.jsp‘;</script>"); 66 } 67 68 else 69 { 70 71 //request.setAttribute("message", "添加成功"); 72 response.setContentType("text/html;charset=utf-8"); 73 PrintWriter out = response.getWriter(); 74 //out.print("<script language=‘javascript‘>alert(‘添加成功‘);window.location.href=‘main_menu.jsp‘;</script>"); 75 out.print("<script language=‘javascript‘>alert(‘添加失败‘);window.location.href=‘main_menu.jsp‘;</script>"); 76 77 78 79 } 80 81 82 83 84 85 } 86 87 /** 88 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) 89 */ 90 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 91 // TODO Auto-generated method stub 92 doGet(request, response); 93 } 94 95 }
删除志愿者jsp(一个确定界面,一个输入界面):
1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2 3 pageEncoding="UTF-8" import="bean.User"%> 4 5 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 6 7 <html> 8 9 <head> 10 <script type="text/javascript"> 11 </script> 12 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 13 14 <title>显示结果</title> 15 16 </head> 17 18 <body> 19 <a href="main_menu.jsp">返回主界面</a> 20 <br> 21 <% 22 User user = (User)request.getAttribute("user"); 23 %> 24 <form action="Deleteuser" name="create" method="post" > 25 <div align="center"> 26 <h1>请确定此成员信息</h1> 27 <input id ="name" name="name" type="hidden" value="<%=user.getName() %>" onChange="change1(this.value)"> 28 <br> 29 性别:<%=user.getSex() %> 30 <br> 31 民族:<%=user.getNation() %> 32 <br> 33 注册时间:<%=user.getTime() %> 34 <br> 35 年龄:<%=user.getAge() %> 36 <br> 37 政治面貌:<%=user.getOutlook() %> 38 <br> 39 服务类别:<%=user.getService() %> 40 <br> 41 <input name="Submit" type="submit" class="inputedit" value="删除" 42 onclick="{if(confirm(‘确定删除吗?‘)){ 43 this.document.formname.submit(); 44 return true;}return false; 45 }"> 46 </div> 47 </form> 48 49 50 51 52 </body> 53 54 </html>
1 <%@ page language="java" contentType="text/html; charset=utf-8" 2 pageEncoding="utf-8"%> 3 <!DOCTYPE html> 4 <html> 5 <head> 6 <meta charset="utf-8"> 7 <title>删除用户</title> 8 </head> 9 <body> 10 <% 11 Object message = request.getAttribute("message"); 12 if(message!=null && !"".equals(message)){ 13 14 %> 15 <script type="text/javascript"> 16 alert("<%=request.getAttribute("message")%>"); 17 </script> 18 <%} %> 19 <a href="main_menu.jsp">返回主界面</a> 20 <form action="deleting" name="remove" method="post" onsubmit="return check()"> 21 <div align="center"> 22 <h1>请输入被删除用户的姓名</h1> 23 姓名:<input id ="name" name="name" type="text" value="" onChange="change1(this.value)"> 24 <br> 25 <input type="submit" value="确定"> 26 27 </div> 28 </form> 29 <script type="text/javascript"> 30 function change1(obj) 31 { 32 if(obj.length==0) 33 { 34 alert("ID不能为空"); } 35 36 37 } 38 39 </script> 40 </body> 41 </html>
servlet:
1 package servlet; 2 3 import java.io.IOException; 4 import java.io.PrintWriter; 5 6 import javax.servlet.ServletException; 7 import javax.servlet.annotation.WebServlet; 8 import javax.servlet.http.HttpServlet; 9 import javax.servlet.http.HttpServletRequest; 10 import javax.servlet.http.HttpServletResponse; 11 12 import bean.Account; 13 import bean.User; 14 import dao.UserDao; 15 16 /** 17 * Servlet implementation class Deleteuser 18 */ 19 @WebServlet("/Deleteuser") 20 public class Deleteuser extends HttpServlet { 21 private static final long serialVersionUID = 1L; 22 23 /** 24 * @see HttpServlet#HttpServlet() 25 */ 26 public Deleteuser() { 27 super(); 28 // TODO Auto-generated constructor stub 29 } 30 31 /** 32 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) 33 */ 34 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 35 int s=0; 36 request.setCharacterEncoding("utf-8"); 37 String name=request.getParameter("name"); 38 User user = new User(); 39 user.setName(name); 40 UserDao userDao=new UserDao(); 41 if(userDao. deleteUser(name)) 42 { 43 44 response.setContentType("text/html;charset=utf-8"); 45 PrintWriter out = response.getWriter(); 46 out.print("<script language=‘javascript‘>alert(‘删除成功‘);window.location.href=‘main_menu.jsp‘;</script>"); 47 } 48 49 else 50 { 51 if(s==1) 52 {request.setAttribute("message", "删除失败"); 53 request.getRequestDispatcher("remove.jsp").forward(request,response); 54 } 55 else if(s==0) 56 {request.getRequestDispatcher("remove.jsp").forward(request,response); 57 s=1;} 58 } 59 } 60 61 /** 62 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) 63 */ 64 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 65 // TODO Auto-generated method stub 66 doGet(request, response); 67 } 68 69 }
1 2 /** 3 * @see HttpServlet#HttpServlet() 4 */ 5 public deleting() { 6 super(); 7 // TODO Auto-generated constructor stub 8 } 9 10 /** 11 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) 12 */ 13 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 14 request.setCharacterEncoding("utf-8"); 15 String name=request.getParameter("name"); 16 DButil DB = new DButil(); 17 Connection conn = DB.getConn(); 18 String sql="select * from middle_text where name= ‘"+name+"‘"; 19 20 try { 21 22 Statement st = conn.createStatement(); 23 24 ResultSet rs = st.executeQuery(sql); 25 26 User user = new User(); 27 28 while(rs.next()) { 29 30 31 user.setName(rs.getString("name")); 32 user.setSex(rs.getString("sex")); 33 user.setNation(rs.getString("nation")); 34 user.setTime(rs.getString("time")); 35 user.setAge(rs.getString("age")); 36 user.setOutlook(rs.getString("outlook")); 37 user.setService(rs.getString("service")); 38 39 40 41 } 42 43 //将数据库中查询到的信息封装在user对象中,use对象保存在request中,之后将通过request对象将数据传递到页面 44 45 //需要的话也可以只传递一个参数request.setAttribute("id", user.getId()); 46 47 request.setAttribute("user", user); 48 49 //这里是转发,从Servlet跳转到showinfo.jsp页面,并且带上request和response对象中原有的参数 50 response.setContentType("text/html;charset=utf-8"); 51 request.getRequestDispatcher("/delete.jsp").forward(request, response); 52 53 54 55 56 } catch (SQLException e) { 57 58 e.printStackTrace(); 59 60 } 61 } 62 63 /** 64 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) 65 */ 66 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 67 // TODO Auto-generated method stub 68 doGet(request, response); 69 } 70 71 }
更改志愿者信息jsp(同删包含两个界面):
1 <%@ page language="java" contentType="text/html; charset=utf-8" 2 pageEncoding="utf-8"%> 3 <!DOCTYPE html> 4 <html> 5 <head> 6 <meta charset="utf-8"> 7 <title>修改用户信息</title> 8 </head> 9 <body> 10 <% 11 Object message = request.getAttribute("message"); 12 if(message!=null && !"".equals(message)){ 13 14 %> 15 <script type="text/javascript"> 16 alert("<%=request.getAttribute("message")%>"); 17 </script> 18 <%} %> 19 <a href="main_menu.jsp">返回主界面</a> 20 <form action="upadting" name="update" method="post" onsubmit="return check()"> 21 <div align="center"> 22 <h1>请输入姓名</h1> 23 姓名:<input id ="name" name="name" type="text" value="" onChange="change1(this.value)"> 24 <br> 25 <input type="submit" value="确定"> 26 </div> 27 </form> 28 <script type="text/javascript"> 29 30 </script> 31 </body> 32 </html>
1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2 3 pageEncoding="UTF-8" import="bean.User"%> 4 5 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 6 7 <html> 8 9 <head> 10 <script type="text/javascript"> 11 12 13 alert("查询成功!") 14 15 </script> 16 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 17 18 <title>显示结果</title> 19 20 </head> 21 22 <body> 23 <a href="main_menu.jsp">返回主界面</a> 24 <br> 25 <% 26 User user = (User)request.getAttribute("user"); 27 %> 28 29 30 <a href="main_menu.jsp">返回主界面</a> 31 <form action="Updateuser" name="create" method="post" > 32 <div align="center"> 33 <h1>请修改此成员信息</h1> 34 <input id ="name" name="name" type="hidden" value="<%=user.getName() %>" onChange="change1(this.value)"> 35 <br> 36 性别:<input id ="sex" name="sex" type="text" value="<%=user.getSex() %>" onChange="change1(this.value)"> 37 <br> 38 民族:<%=user.getNation() %> 39 <br> 40 注册时间:<%=user.getTime() %> 41 <br> 42 年龄:<%=user.getAge() %> 43 <br> 44 政治面貌:<input id ="outlook" name="outlook" type="text" value="<%=user.getOutlook() %>" onChange="change1(this.value)"> 45 <br> 46 服务类别:<input id ="service" name="service" type="text" value="<%=user.getService() %>" onChange="change1(this.value)"> 47 <br> 48 <input type="submit" value="确定"> 49 </div> 50 </form> 51 52 53 54 55 </body> 56 57 </html>
servlet:
1 package servlet; 2 3 import java.io.IOException; 4 import java.sql.Connection; 5 import java.sql.ResultSet; 6 import java.sql.SQLException; 7 import java.sql.Statement; 8 9 import javax.servlet.ServletException; 10 import javax.servlet.annotation.WebServlet; 11 import javax.servlet.http.HttpServlet; 12 import javax.servlet.http.HttpServletRequest; 13 import javax.servlet.http.HttpServletResponse; 14 15 import bean.Account; 16 import bean.User; 17 import util.DButil; 18 19 /** 20 * Servlet implementation class upadting 21 */ 22 @WebServlet("/upadting") 23 public class upadting extends HttpServlet { 24 private static final long serialVersionUID = 1L; 25 26 /** 27 * @see HttpServlet#HttpServlet() 28 */ 29 public upadting() { 30 super(); 31 // TODO Auto-generated constructor stub 32 } 33 34 /** 35 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) 36 */ 37 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 38 request.setCharacterEncoding("utf-8"); 39 String name=request.getParameter("name"); 40 DButil DB = new DButil(); 41 Connection conn = DB.getConn(); 42 String sql="select * from middle_text where name= ‘"+name+"‘"; 43 44 try { 45 46 Statement st = conn.createStatement(); 47 48 ResultSet rs = st.executeQuery(sql); 49 50 User user = new User(); 51 52 while(rs.next()) { 53 54 55 user.setName(rs.getString("name")); 56 user.setSex(rs.getString("sex")); 57 user.setNation(rs.getString("nation")); 58 user.setTime(rs.getString("time")); 59 user.setAge(rs.getString("age")); 60 user.setOutlook(rs.getString("outlook")); 61 user.setService(rs.getString("service")); 62 63 64 65 } 66 67 //将数据库中查询到的信息封装在user对象中,use对象保存在request中,之后将通过request对象将数据传递到页面 68 69 //需要的话也可以只传递一个参数request.setAttribute("id", user.getId()); 70 71 request.setAttribute("user", user); 72 73 //这里是转发,从Servlet跳转到showinfo.jsp页面,并且带上request和response对象中原有的参数 74 response.setContentType("text/html;charset=utf-8"); 75 request.getRequestDispatcher("/updating.jsp").forward(request, response); 76 77 78 79 80 } catch (SQLException e) { 81 82 e.printStackTrace(); 83 84 } 85 } 86 87 /** 88 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) 89 */ 90 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 91 // TODO Auto-generated method stub 92 doGet(request, response); 93 } 94 95 }
1 package servlet; 2 3 import java.io.IOException; 4 import java.io.PrintWriter; 5 6 import javax.servlet.ServletException; 7 import javax.servlet.annotation.WebServlet; 8 import javax.servlet.http.HttpServlet; 9 import javax.servlet.http.HttpServletRequest; 10 import javax.servlet.http.HttpServletResponse; 11 12 import bean.Account; 13 import bean.User; 14 import dao.UserDao; 15 16 /** 17 * Servlet implementation class Updateuser 18 */ 19 @WebServlet("/Updateuser") 20 public class Updateuser extends HttpServlet { 21 private static final long serialVersionUID = 1L; 22 23 /** 24 * @see HttpServlet#HttpServlet() 25 */ 26 public Updateuser() { 27 super(); 28 // TODO Auto-generated constructor stub 29 } 30 31 /** 32 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) 33 */ 34 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 35 int s=0; 36 request.setCharacterEncoding("utf-8"); 37 String name=request.getParameter("name"); 38 String sex=request.getParameter("sex"); 39 String outlook=request.getParameter("outlook"); 40 String service=request.getParameter("service"); 41 User user=new User(); 42 user.setSex(sex); 43 user.setOutlook(outlook); 44 user.setService(service); 45 //UserDao userDao=UserDao.getUserDao(); 46 UserDao userDao=new UserDao(); 47 if(userDao. resetUserPassword(name,sex,outlook,service)) 48 { 49 50 response.setContentType("text/html;charset=utf-8"); 51 PrintWriter out = response.getWriter(); 52 out.print("<script language=‘javascript‘>alert(‘修改成功‘);window.location.href=‘main_menu.jsp‘;</script>"); 53 } 54 55 else 56 { 57 if(s==1) 58 {request.setAttribute("message", "修改失败"); 59 request.getRequestDispatcher("update.jsp").forward(request,response); 60 } 61 else if(s==0) 62 {request.getRequestDispatcher("update.jsp").forward(request,response); 63 s=1;} 64 } 65 66 } 67 68 /** 69 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) 70 */ 71 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 72 // TODO Auto-generated method stub 73 doGet(request, response); 74 } 75 76 }
标签:tag att 医疗 dispatch 不同 check pattern ref public
原文地址:https://www.cnblogs.com/zgk666/p/12012511.html