嘉兴高端网站天翼云 安装wordpress
嘉兴高端网站,天翼云 安装wordpress,中国公关公司前十名,广州做网站哪家好【有道云笔记】十七 4.3 转发、重定向、Get、POST、乱码 https://note.youdao.com/s/GD5TRksQ
一、转发 转发#xff1a;一般查询了数据之后#xff0c;转发到一个jsp页面进行展示
req.setAttribute(list, list);
req.getRequestDispatcher(student_lis…【有道云笔记】十七 4.3 转发、重定向、Get、POST、乱码 https://note.youdao.com/s/GD5TRksQ
一、转发 转发一般查询了数据之后转发到一个jsp页面进行展示
req.setAttribute(list, list);
req.getRequestDispatcher(student_list.jsp).forward(req, resp);
二、重定向 重定向一般添加、删除、修改之后重定向到查找所有
resp.sendRedirect(/student);
重定向的状态码是302重定向的地址最终是由浏览器发送这个请求 给超链接添加点击事件并触发
a hrefjavascript:void(0) οnclickmethod()/a a hrefjavascript:; οnclickmethod()/a a hrefjavascript:method();xxx/a
三、Get
采用URL请求路径传输参数参数拼接在URL后面参数传输过程中隐私性较差直接在URL后面路径可以容纳的数据有限只能传递少量参数form表单请求默认就是get
http://localhost:8080/student?methoddeleteByIdid23
http://localhost:8080/student?namezhangsanage12gender男
Get方式传参不是非得在form表单里面可以手动写在超链接的href里面直接在地址后面加?id2
四、POST
采用实体内容传参数参数在传输过程中不可见隐私性好实体内容专门用来传输数据大小没有限制使用在form上加methodpost 不管是Get方式还是POST方式传参数后台代码获取参数的方式都是一样的。
req.getParameter(name);
五、乱码问题总结
1、数据库创建时候选择utf-8编码
连接数据库url:
jdbc:mysql://localhost:3306/java?useUnicodetruecharacterEncodingUTF-8
2、解决post请求乱码问题 methodpost req.setCharacterEncoding(UTF-8);
3、服务器响应浏览器的乱码问题
resp.setContentType(text/html;charsetutf-8);
六、前台往后台发请求方式
form表单超链接删除location.hrefajax
跳转到一个jsp页面的方式
直接访问这个jsp页面 http://localhost:8080/student_update.jsp访问servlet转发到这个页面
七、增删改查代码
//http://localhost:8080/index.jsp //http://localhost:8080/student WebServlet(/student) public class StudentServlet extends HttpServlet { //默认访问service Override protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { //System.out.println(StudentServlet.service); //解决post请求乱码问题 req.setCharacterEncoding(UTF-8); // http://localhost:8080/student?methodselectAll // http://localhost:8080/student?methoddeleteByIdid23 String method req.getParameter(method); if (method null || method.equals()) { method selectAll; } switch (method) { case selectAll: selectAll(req, resp); break; case deleteById: deleteById(req, resp); break; case add: add(req, resp); break; case toUpdate: toUpdate(req, resp); break; case update: update(req, resp); break; } } private void update(HttpServletRequest req, HttpServletResponse resp) throws IOException { System.out.println(StudentServlet.update); String id req.getParameter(id); String name req.getParameter(name); String age req.getParameter(age); String gender req.getParameter(gender); Connection connection null; PreparedStatement preparedStatement null; try { connection JDBCUtil.getConnection(); String sql update student set name?,age?,gender? where id?; preparedStatement connection.prepareStatement(sql); preparedStatement.setString(1, name); preparedStatement.setInt(2, Integer.parseInt(age)); preparedStatement.setString(3, gender); preparedStatement.setInt(4, Integer.parseInt(id)); System.out.println(preparedStatement); int count preparedStatement.executeUpdate(); System.out.println(count: count); } catch (SQLException throwables) { throwables.printStackTrace(); } resp.sendRedirect(/student); } private void toUpdate(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { System.out.println(StudentServlet.toUpdate); String id req.getParameter(id); Connection connection null; PreparedStatement preparedStatement null; ResultSet resultSet null; Student student null; try { connection JDBCUtil.getConnection(); String sql SELECT id,name,age,gender FROM student where id?; preparedStatement connection.prepareStatement(sql); preparedStatement.setInt(1, Integer.parseInt(id)); System.out.println(preparedStatement); resultSet preparedStatement.executeQuery(); while (resultSet.next()) {//判断下一个有没有如果返回true而且指向下一个没有返回false //int id resultSet.getInt(id); String name resultSet.getString(name); int age resultSet.getInt(age); String gender resultSet.getString(gender); student new Student(Integer.parseInt(id), name, age, gender); } } catch (SQLException throwables) { throwables.printStackTrace(); } finally { JDBCUtil.close(connection, preparedStatement, resultSet); } //把list数据放到req里面 req.setAttribute(student, student); //转发到student_list.jsp页面进行展示 req.getRequestDispatcher(student_update.jsp).forward(req, resp); } private void add(HttpServletRequest req, HttpServletResponse resp) throws IOException { System.out.println(StudentServlet.add); String name req.getParameter(name); String age req.getParameter(age); String gender req.getParameter(gender); Connection connection null; PreparedStatement preparedStatement null; try { connection JDBCUtil.getConnection(); String sql insert into student(name,age,gender) values(?,?,?); preparedStatement connection.prepareStatement(sql); preparedStatement.setString(1, name); preparedStatement.setInt(2, Integer.parseInt(age)); preparedStatement.setString(3, gender); System.out.println(preparedStatement); int count preparedStatement.executeUpdate(); System.out.println(count: count); } catch (SQLException throwables) { throwables.printStackTrace(); } finally { JDBCUtil.close(connection, preparedStatement, null); } resp.sendRedirect(/student?methodselectAll); } private void deleteById(HttpServletRequest req, HttpServletResponse resp) throws IOException { String id req.getParameter(id); Connection connection null; PreparedStatement preparedStatement null; try { connection JDBCUtil.getConnection(); String sql delete from student where id?; preparedStatement connection.prepareStatement(sql); preparedStatement.setInt(1, Integer.parseInt(id)); System.out.println(preparedStatement); int count preparedStatement.executeUpdate(); System.out.println(count: count); } catch (SQLException throwables) { throwables.printStackTrace(); } finally { JDBCUtil.close(connection, preparedStatement, null); } // /student 302 // 重定向 resp.sendRedirect(/student?methodselectAll); } private void selectAll(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { Connection connection null; PreparedStatement preparedStatement null; ResultSet resultSet null; ListStudent list new ArrayList(); try { connection JDBCUtil.getConnection(); String sql SELECT id,name,age,gender FROM student; //预编译 preparedStatement connection.prepareStatement(sql); System.out.println(preparedStatement); resultSet preparedStatement.executeQuery(); while (resultSet.next()) {//判断下一个有没有如果返回true而且指向下一个没有返回false int id resultSet.getInt(id); String name resultSet.getString(name); int age resultSet.getInt(age); String gender resultSet.getString(gender); Student student new Student(id, name, age, gender); list.add(student); } for (Student student : list) { System.out.println(student); } } catch (SQLException throwables) { throwables.printStackTrace(); } finally { JDBCUtil.close(connection, preparedStatement, resultSet); } //把list数据放到req里面 req.setAttribute(list, list); //转发到student_list.jsp页面进行展示 req.getRequestDispatcher(student_list.jsp).forward(req, resp); } }
student_list.jsp
% page importcom.situ.web.pojo.Student % % page importjava.util.List % % page contentTypetext/html;charsetUTF-8 languagejava % html head titleTitle/title link relstylesheet hrefstatic/bootstrap-3.4.1-dist/css/bootstrap.css /head body % //JSP页面中可以嵌套Java代码 //JSP脚本在这里可以写任意的Java代码 //request、response:JSP页面的内置对象 ListStudent list (ListStudent) request.getAttribute(list); % a classbtn btn-primary href/student_add.jsp添加/a table classtable table-striped table-bordered table-hover table-condensed tr tdID/td td名字/td td年龄/td td性别/td td编辑/td td删除/td /tr % for (Student student : list) { % tr td%student.getId()%/td td%student.getName()%/td td%student.getAge()%/td td%student.getGender()%/td tda href/student?methodtoUpdateid%student.getId()%编辑/a/td %--/deleteStudent?id12 --% %--tda href/deleteStudent?id%student.getId()%删除/a/td--% %--tda href/student?methoddeleteByIdid%student.getId()%删除/a/td--% tda hrefjavascript:deleteById(%student.getId()%)删除/a/td /tr % } % /table script function deleteById(id) { var isDelete confirm(您确认要删除); if (isDelete) { location.href /student?methoddeleteByIdid id; } } /script /body /html
student_add.jsp
% page contentTypetext/html;charsetUTF-8 languagejava % html head titleTitle/title /head body form action/student?methodadd methodpost 用户名input typetext namename/br/ 年龄input typetext nameage/br/ 性别input typetext namegender/br/ input typesubmit value添加/ /form /body /html
student_update.jsp
% page importcom.situ.web.pojo.Student % % page contentTypetext/html;charsetUTF-8 languagejava % html head titleTitle/title /head body % Student student (Student) request.getAttribute(student); % form action/student?methodupdate methodpost input typehidden nameid value%student.getId()%/ 用户名input typetext namename value%student.getName()%/br/ 年龄input typetext nameage value%student.getAge()%/br/ 性别input typetext namegender value%student.getGender()%/br/ input typesubmit value修改/ /form /body /html
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/diannao/90669.shtml
如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!