码迷,mamicode.com
首页 > 编程语言 > 详细

javaWeb中request请求转发和response重定向

时间:2017-11-06 21:29:25      阅读:239      评论:0      收藏:0      [点我收藏+]

标签:encoding   perm   tac   refresh   attribute   request请求   解析   很多   writer   

1.访问资源

运用forward方法只能重定向到同一个Web应用程序中的一个资源。
而sendRedirect方法可以让你重定向到任何URL。
 2.request.get
Forward代码中的"/uu"则代表相对与WEB应用的路径。
举例:
  Servlet 和 目标文件的包路径:
 
 技术分享

 

Servlet  代码:中 doPost 和doGet代码:

request 重定向url地址 或者是是相对于 本项目 (WEB13)  http:localhost:8080/WEB13+url     ;

  url 地址为:     /html/Ok.html   或

          html/Ok.html

而 response 请求转发 的url     以   ‘  /   ‘  开头   相当于  本服务器 http:localhost:8080+ url             url   如:  /WEB13/html/No.html  

而开头不以 ‘  /  ’ 开头    (html/No.html)    就 是 地址想对应本项目 (WEB13) http:localhost:8080/WEB13/+url  

 

 1 public void doGet(HttpServletRequest request, HttpServletResponse response)
 2             throws ServletException, IOException {
 3         request.setCharacterEncoding("UTF-8");
 4         //response.setCharacterEncoding("UTF-8");
 5 
 6         
 7         DataSource dataSource=DBCPUtils.getDataSource();
 8         System.out.println(dataSource);
 9         QueryRunner qr=new QueryRunner(dataSource);
10         PrintWriter out=response.getWriter();
11         
12         String userName=request.getParameter("userName");
13         String password=request.getParameter("password");
14         
15         System.out.println(userName);
16         String sql1="select * from user ";
17         String sql="select * from user where userName=? and password=?";
18 //        Object [] params={userName,password};
19         try {
20              Object[] u = qr.query(sql,new ArrayHandler(),userName,password);
21                     
22                  if (u!=null) {
23                      System.out.println("查询结果不为空");
24                          request.getRequestDispatcher("html/Ok.html").forward(request, response);
25 //                         response.sendRedirect("/WEB13/html/Ok.html");
26                     }else {
27                         System.out.println("查询结果为空");
28               response.sendRediect(‘/WEB13/html/NO.html‘);
29                     }
30         } catch (SQLException e) {
31             e.printStackTrace();
32         }
33     }
34 
35     public void doPost(HttpServletRequest request, HttpServletResponse response)
36             throws ServletException, IOException {
37         doGet(request, response);
38     }

 

3.当Servlet 用request 请求转发,表单跳转Servlet 页面 的提交方式 :get ,post:

(运用RequestDispatcher接口的Forward)方法 forward()无法重定向至有frame的jsp文件,可以重定向至有frame的html文件,

同时forward()无法在后面带参数传递, 比如servlet?name=frank,这样不行,可以程序内通过response.setAttribute("name",name)来传至下一个页面.

即:  当 另一个页面(这里可以说是登录页面)跳转到Servlet 用get方式   request.getRequestDispatcher().forword(request.response);  是无法跳转的

 

4.servlet 用request 请求转发  到另一个页面的中文乱码问题

 Servlet  request请求转发跳转到另一个页面时: 会出现中文乱码

OK.html

1 <!DOCTYPE html>
2 <html>
3 <head>
4 <meta charset="UTF-8">
5 <title>登录成功</title>
6 </head>
7 <body>
8     

虽然在第四行  <meta charset="UTF-8">   已经告诉了浏览器显示该Ok页面的编码方式    ,但是没有加   response.setCharacterEncoding("UTF-8");  转发跳转到Ok页面时,显示 的依旧是乱码  

 而response 重定向就不会; 

 

以下是截取 别人博客  地址

只有在客户端没有输出时才可以调用forward方法。如果当前页面的缓冲区(buffer)不是空的,那么你在调用forward方法前必须先清空缓冲区。 "/"代表相对与web应用路径 
RequestDispatcher rd = request.getRequestDispatcher("/ooo"); 
rd.forward(request, response);
提交至http://localhost:8080/Test/ooo 

RequestDispatcher rd = getServletContext().getRequestDispatcher("/ooo"); 
rd.forward(request, response);
提交至http://localhost:8080/Test/ooo 

RequestDispatcher rd =getServletContext().getNamedDispatcher("TestServlet");(TestServlet为一个<servlet-name>) rd.forward(request, response);
提交至名为TestServlet的servlet 

如果在<jsp:forward>之前有很多输出,前面的输出已使缓冲区满,将自动输出到客户端,那么该语句将不起作用,这一点应该特别注意。 另外要注意:它不能改变浏览器地址,刷新的话会导致重复提交 从http://localhost:8080/Test/gw/page.jsp中转发 
<jsp:forward page="OtherPage.jsp"/>在JSP页面被解析后转换成pageContext.forward("OtherPage.jsp"); "/OtherPage.jsp"提交到http://localhost:8080/Test/OtherPage.jsp 
"OtherPage.jsp"提交到http://localhost:8080/Test/gw/OtherPage.jsp 


(运用HttpServletResponse接口的sendRedirect)方法302 
是在用户的浏览器端工作,sendRedirect()可以带参数传递,比如servlet?name=frank传至下个页面, 
同时它可以重定向至不同的主机上,sendRedirect()可以重定向有frame.的jsp文件.
 
假设转发代码包含于注册的servlet-url为/ggg/tt;jsp为/ggg/tt.jsp: 
绝对路径:response.sendRedirect("http://www.brainysoftware.com")发送http://www.brainysoftware.com 
根路径:response.sendRedirect("/ooo")发送至http://localhost:8080/ooo 
相对路径:response.sendRedirect("ooo")发送至http://localhost:8080/Test/ggg/ooo, 

sendRedirect等同于此方式 response.setStatus(HttpServletResponse.SC_MOVED_PERMANENTLY); 
String newLocn = "/newpath/jsa.jsp"; 
response.setHeader("Location",newLocn); 

(Meta Refresh)方法200 
这种方法是由HTML提供的,Meta本身就是HTML标签。
使用方法是:<meta http-equiv="refresh" content="5; url=http://www.dreamdu.com/" /> 
相应的java代码 String content=stayTime+";URL="+URL; 
response.setHeader("REFRESH",content);

 

javaWeb中request请求转发和response重定向

标签:encoding   perm   tac   refresh   attribute   request请求   解析   很多   writer   

原文地址:http://www.cnblogs.com/shaoxiaohuan/p/7794967.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!