在线一区二区三区高清视频,国产精品妇女一二三区,美女被遭强高潮网站在线播放,实拍各种胸走光见奶头

Servlet跳轉(zhuǎn)到j(luò)sp頁(yè)面的幾種方法

時(shí)間:2020-01-07 17:00:13 類(lèi)型:JAVA
字號(hào):    

  一、Servlet

  當(dāng)然,在servlet中,一般跳轉(zhuǎn)都發(fā)生在doGet, doPost等方法里面。

  1)  redirect 方式

  response.sendRedirect("success.jsp");

  頁(yè)面的路徑是相對(duì)路徑。sendRedirect可以將頁(yè)面跳轉(zhuǎn)到任何頁(yè)面,不一定局限于本web應(yīng)用中,如:

  response.sendRedirect("http://www.ycul.com");

  跳轉(zhuǎn)后瀏覽器地址欄變化。

  這種方式要傳值出去的話(huà),只能在url中帶parameter或者放在session中,無(wú)法使用request.setAttribute來(lái)傳遞。

  2) forward方式

  RequestDispatcher dispatcher = request.getRequestDispatcher("/a.jsp");

  dispatcher .forward(request, response);

  //跳轉(zhuǎn)到出口request.getRequestDispatcher("loginSuccess.jsp").forward(request, response);

  頁(yè)面的路徑是相對(duì)路徑。forward方式只能跳轉(zhuǎn)到本web應(yīng)用中的頁(yè)面上。

  跳轉(zhuǎn)后瀏覽器地址欄不會(huì)變化。

  使用這種方式跳轉(zhuǎn),傳值可以使用三種方法:url中帶parameter,session,request.setAttribute

  request.setAttribute("name",username);            

     (1)  String username=request.getParameter("username"); 

     (2)  String pwd=request.getParameter("pwd");

      System.out.println("用戶(hù)名:"+username+"密碼:"+pwd);

  3)插入,頁(yè)面跳轉(zhuǎn)。

  跳轉(zhuǎn)到Loginservler的doGet

  二、JSP

  1)  response.sendRedirect();和servlet的response.sendRedirect()方式一樣。

  此語(yǔ)句前不允許有out.flush(),如果有,會(huì)有異常:

  java.lang.IllegalStateException: Can't sendRedirect() after data has committed to the client.

  at com.caucho.server.connection.AbstractHttpResponse.sendRedirect(AbstractHttpResponse.java:558)

  ...

  跳轉(zhuǎn)后瀏覽器地址欄變化

  如果要跳到不同主機(jī)下,跳轉(zhuǎn)后,此語(yǔ)句后面的語(yǔ)句會(huì)繼續(xù)執(zhí)行,如同新開(kāi)了線程,但是對(duì)response的操作已經(jīng)無(wú)意義了;

  如果要跳到相同主機(jī)下,此語(yǔ)句后面的語(yǔ)句執(zhí)行完成后才會(huì)跳轉(zhuǎn);

  2)  response.setHeader("Location","");

  此語(yǔ)句前不允許有out.flush(),如果有,頁(yè)面不會(huì)跳轉(zhuǎn)。

  跳轉(zhuǎn)后瀏覽器地址欄變化

  此語(yǔ)句后面的語(yǔ)句執(zhí)行完成后才會(huì)跳轉(zhuǎn)

  3)

  此語(yǔ)句前不允許有out.flush(),如果有,會(huì)有異常:

  java.lang.IllegalStateException: forward() not allowed after buffer has committed.

  at com.caucho.server.webapp.RequestDispatcherImpl.forward(RequestDispatcherImpl.java:134)

  at com.caucho.server.webapp.RequestDispatcherImpl.forward(RequestDispatcherImpl.java:101)

  at com.caucho.jsp.PageContextImpl.forward(PageContextImpl.java:836)

  ...

  跳轉(zhuǎn)后瀏覽器地址欄不變,但是只能跳到當(dāng)前主機(jī)下

  此語(yǔ)句后面的語(yǔ)句執(zhí)行完成后才會(huì)跳轉(zhuǎn)


<