关于Web中资源跳转的问题:
转发和重定向基本代码:
1 package com.bjpowernode.javaweb; 2 3 import java.io.IOException; 4 5 import javax.servlet.ServletException; 6 import javax.servlet.http.HttpServlet; 7 import javax.servlet.http.HttpServletRequest; 8 import javax.servlet.http.HttpServletResponse; 9 10 public class AServlet extends HttpServlet { 11 12 private static final long serialVersionUID = 1L; 13 14 @Override 15 protected void doGet(HttpServletRequest request, HttpServletResponse response) 16 throws ServletException, IOException { 17 //向request范围中存储数据 18 request.setAttribute("username", "zhangsan"); 19 20 //转发forward:一次请求 21 //request.getRequestDispatcher("/b").forward(request, response); 22 23 //重定向redirect:两次请求 24 //执行到此处之后,将/prj-servlet-16/b路径响应给浏览器,浏览器又向服务器发送了一次全新的请求 25 response.sendRedirect("/prj-servlet-16/b"); 26 //response.sendRedirect(request.getContextPath() + "/b"); 27 } 28 }
1 package com.bjpowernode.javaweb; 2 3 import java.io.IOException; 4 5 import javax.servlet.ServletException; 6 import javax.servlet.http.HttpServlet; 7 import javax.servlet.http.HttpServletRequest; 8 import javax.servlet.http.HttpServletResponse; 9 10 public class BServlet extends HttpServlet { 11 12 private static final long serialVersionUID = 1L; 13 14 @Override 15 protected void doGet(HttpServletRequest request, HttpServletResponse response) 16 throws ServletException, IOException { 17 //向request范围中取数据 18 Object obj = request.getAttribute("username"); 19 System.out.println(obj); 20 } 21 }
使用重定向解决页面刷新问题:
1 <html> 2 <head> 3 <title>save user</title> 4 <meta charset="UTF-8"> 5 </head> 6 <body> 7 <form action="/prj-servlet-17/save" method="post"> 8 usercode<input type="text" name="usercode"> 9 username<input type="text" name="username"> 10 <input type="submit" value="save"> 11 </form> 12 </body> 13 </html>
1 package com.bjpowernode.javaweb.servlet; 2 3 import java.io.IOException; 4 import java.sql.Connection; 5 import java.sql.DriverManager; 6 import java.sql.PreparedStatement; 7 import java.sql.SQLException; 8 9 import javax.servlet.ServletException; 10 import javax.servlet.http.HttpServlet; 11 import javax.servlet.http.HttpServletRequest; 12 import javax.servlet.http.HttpServletResponse; 13 14 public class SaveUserServlet extends HttpServlet { 15 16 private static final long serialVersionUID = 1L; 17 18 @Override 19 protected void doPost(HttpServletRequest request, HttpServletResponse response) 20 throws ServletException, IOException { 21 //解决中文乱码 22 request.setCharacterEncoding("UTF-8"); 23 24 //获取表单提供的数据 25 String usercode = request.getParameter("usercode"); 26 String username = request.getParameter("username"); 27 28 //JDBC 29 Connection conn = null; 30 PreparedStatement ps = null; 31 int count = 0; 32 try{ 33 Class.forName("com.mysql.jdbc.Driver"); 34 conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test1", "root", "248xiaohai"); 35 conn.setAutoCommit(false); 36 String sql = "insert into t_user(usercode,username) values(?,?)"; 37 ps = conn.prepareStatement(sql); 38 ps.setString(1, usercode); 39 ps.setString(2, username); 40 count = ps.executeUpdate(); 41 conn.commit(); 42 } catch (Exception e) { 43 e.printStackTrace(); 44 } finally { 45 if(ps != null){ 46 try { 47 ps.close(); 48 } catch (SQLException e) { 49 e.printStackTrace(); 50 } 51 } 52 if(conn != null){ 53 try { 54 conn.close(); 55 } catch (SQLException e) { 56 e.printStackTrace(); 57 } 58 } 59 60 if(count == 1){ 61 //保存成功,跳转到成功页面 62 //转发,刷新的时候会重复保存数据,因为刷新的是第一个请求/prj-servlet-17/save 63 //request.getRequestDispatcher("/success.html").forward(request, response); 64 //重定向,刷新的时候不会重复保存数据,因为刷新的是后一个请求/prj-servlet-17/success.html 65 response.sendRedirect(request.getContextPath() + "/success.html"); 66 } 67 } 68 } 69 70 71 }