從 servlet轉(zhuǎn)到 jsp不外乎兩種方法,重定向 和 url轉(zhuǎn)發(fā)
目前創(chuàng)新互聯(lián)公司已為1000多家的企業(yè)提供了網(wǎng)站建設(shè)、域名、網(wǎng)站空間、網(wǎng)站托管、企業(yè)網(wǎng)站設(shè)計(jì)、清豐網(wǎng)站維護(hù)等服務(wù),公司將堅(jiān)持客戶導(dǎo)向、應(yīng)用為本的策略,正道將秉承"和諧、參與、激情"的文化,與客戶和合作伙伴齊心協(xié)力一起成長(zhǎng),共同發(fā)展。
1、重定向 ( Redirect):是路徑的跳轉(zhuǎn),內(nèi)容和 url都改變。不允許帶 request參數(shù)( session參數(shù)可以),即不允許在 servlet里給 request對(duì)象使用setAttribute方法傳給下一頁(yè)面。在 servlet里使用 response.sendRedirect(url) 方法。注意這里的 url前不帶斜線 /,如 response.sendRedirect(”test.jsp“)
2、url轉(zhuǎn)發(fā) ( Forward):是頁(yè)面的跳轉(zhuǎn),頁(yè)面內(nèi)容發(fā)生改變,url不變??梢詭?request和 session參數(shù)。在 servlet里使用 getServletConfig().getServletContext().getRequestDispatcher(url).forward(request, response)。而這里的 url前需要帶斜線 /,如getServletConfig().getServletContext().getRequestDispatcher(”/test.jsp“).forward(request, response)
servlet使用RequestDispatcher到j(luò)sp頁(yè)面
String url="/EShop.jsp";
ServletContext sc = getServletContext();
RequestDispatcher rd = sc.getRequestDispatcher(url);
rd.forward(req, res);
servlet重定向jsp,并將數(shù)據(jù)發(fā)送到別一個(gè)JSP中:
request.setAttubute("messages",aaa);
RequestDispatcher requestDispatcher=request.getRequestDispatcher("/jsp/testbean.jsp");
requestDispatcher.forward(request,response);
3.7、Servlet跳轉(zhuǎn)
從JSP跳轉(zhuǎn)到Servlet可以實(shí)現(xiàn),通過(guò)表單提交或超鏈接可以做到,但是現(xiàn)在如果從一個(gè)Servlet跳轉(zhuǎn)到JSP呢?
? HttpServletResponse對(duì)象,此對(duì)象可以完成跳轉(zhuǎn):public void sendRedirect();
JumpServlet.java:
package org.lxh.demo ;
import javax.servlet.* ;
import javax.servlet.http.* ;
import java.io.* ;
public class JumpServlet extends HttpServlet{
public void init() throws ServletException{
}
public void doGet(HttpServletRequest req,HttpServletResponse resp)
throws ServletException,java.io.IOException{
resp.sendRedirect("jump.jsp") ;
}
public void doPost(HttpServletRequest req,HttpServletResponse resp)
throws ServletException,java.io.IOException{
this.doGet(req,resp) ;
}
public void destroy(){
}
};
web.xml:
servlet
servlet-namejump/servlet-name
servlet-classorg.lxh.demo.JumpServlet/servlet-class
/servlet
servlet-mapping
servlet-namejump/servlet-name
url-pattern/jump/url-pattern
/servlet-mapping
以上實(shí)現(xiàn)了跳轉(zhuǎn)的功能,但是跳轉(zhuǎn)之后,發(fā)現(xiàn)地址欄已經(jīng)改變了,所以此跳轉(zhuǎn)稱為客戶端跳轉(zhuǎn)。如果此時(shí)假設(shè)一個(gè)Servlet要向JSP中傳遞屬性,那么應(yīng)該使用session范圍。
JumpServlet.java:
public void doGet(HttpServletRequest req,HttpServletResponse resp)
throws ServletException,java.io.IOException{
request.getSession().setAttribute("name","hello") ;
resp.sendRedirect("jump.jsp") ;
}
jump.jsp:
h1%=session.getAttribute("name")%/h1
此時(shí),發(fā)現(xiàn)可以通過(guò)session屬性范圍傳遞內(nèi)容,但是如果現(xiàn)在傳遞的內(nèi)容很多,而程序中使用session范圍的話則肯定會(huì)帶來(lái)額外的性能降低,但是此時(shí),這些值只使用一次顯示,那肯定沒(méi)有必要使用session對(duì)象,最好使用request范圍。如果request屬性范圍也有一個(gè)限制,必須是服務(wù)器端跳轉(zhuǎn),在JSP中使用jsp:forward語(yǔ)句完成,但是在servlet中呢?就只能使用RequestDsipatcher接口完成,實(shí)際上jsp:forward語(yǔ)句也是使用包裝了此種應(yīng)用,既然RequestDispatcher是一個(gè)接口,所以必須通過(guò)其他方式實(shí)例化,通過(guò)request對(duì)象完成。
JumpServlet.java:
public void doGet(HttpServletRequest req,HttpServletResponse resp)
throws ServletException,java.io.IOException{
req.setAttribute("name","hello") ;
// 準(zhǔn)備跳轉(zhuǎn)到此頁(yè)面
RequestDispatcher rd = req.getRequestDispatcher("jump.jsp") ;
// 把所有的請(qǐng)求向下繼續(xù)傳遞
rd.forward(req,resp) ;
}
注意:
當(dāng)需要傳遞request屬性范圍的時(shí)候才使用服務(wù)器端跳轉(zhuǎn)。如果不需要傳遞,則可以選擇任意的跳轉(zhuǎn)方式。
但是一般情況下都使用RequestDispatcher進(jìn)行跳轉(zhuǎn),而且一定要記住的時(shí)候,跳轉(zhuǎn)之后頁(yè)面地址不會(huì)有任何的變化。
import?java.util.*;
/**
*?@author?hardneedl
*/
final?class?PriceComputer?{
private?abstract?static?class?Pricer?{
float?getPrice(float?weight,?String?postalCode,?float?length,?float?width,?float?height){
return?(length?*?height?*?width?+?getPriceForWeight(weight))?*?(1?+?attachedPercent(postalCode));
}
abstract?float?getPriceForWeight(float?weight);
abstract?float?attachedPercent(String?postalCode);
}
private?interface?PricerFactory{
Pricer?getPricer();
}
private?static?class?DefaultPriceFactory?implements?PricerFactory{
public?Pricer?getPricer()?{
return?new?Pricer()?{
public?float?getPriceForWeight(float?weight)?{
if?(weight?=?0)?return?0;
if?(weight??0??weight?=?5)?return?12;
if?(weight??5??weight?=?15)?return?14;
if?(weight??15??weight?=?34)?return?17;
if?(weight??34??weight?=?45)?return?21;
if?(weight??45??weight?=?60)?return?33;
return?105;
}
public?float?attachedPercent(String?postalCode)?{
if?(postalCode?==?null?||?postalCode.isEmpty())return?.14f;
if?(postalCode.charAt(0)?==?'4')return?.05f;
if?(postalCode.charAt(0)?==?'6')return?.09f;
return?.14f;
}
};
}
}
public?static?void?main(String...?args)?{
Scanner?scanner?=?new?Scanner(System.in);
System.out.println("長(zhǎng)度:");
float?length?=?scanner.nextFloat();
System.out.println("寬度:");
float?width?=?scanner.nextFloat();
System.out.println("高度:");
float?height?=?scanner.nextFloat();
System.out.println("重量:");
float?weight?=?scanner.nextFloat();
System.out.println("郵政編碼:");
String?postal?=?scanner.next();
PricerFactory?pricerFactory?=?new?DefaultPriceFactory();
Pricer?pricer?=?pricerFactory.getPricer();
float?price?=?pricer.getPrice(weight,?postal,length,width,height);
System.out.printf("價(jià)格是:%f",?price);
}
}
import java.util.Scanner;
interface Traffic{
double calc(double weight,double distance);//計(jì)算運(yùn)費(fèi)
}
class Trunk implements Traffic{
@Override
public double calc(double weight,double distance) {
return (distance=1000weight=60)?(weight*distance*120):(-1);
}
}
class Train implements Traffic{
@Override
public double calc(double weight, double distance) {
return (distance=900)?(weight*distance*250):(distance*weight*300);
}
}
class Plane implements Traffic{
@Override
public double calc(double weight, double distance) {
return (distance500)?(weight*distance*750):(-1);
}
}
class ZuChe{
private double distance;//距離
private double weight;//重量
public String useTraffic(Traffic t){
double result = t.calc(weight, distance);
return (result!=-1)?(""+result):(t.getClass().getName().equals(Trunk.class.getName())?"超重或者距離太遠(yuǎn)不能接單":"拒載");
}
public ZuChe setDistance(double distance) {
this.distance = distance;
return this;
}
public ZuChe setWeight(double weight) {
this.weight = weight;
return this;
}
}
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
double weight = sc.nextDouble();
double distance = sc.nextDouble();
ZuChe zuche = new ZuChe().setDistance(distance).setWeight(weight);
System.out.println("卡車的運(yùn)費(fèi)是:"+zuche.useTraffic(new Trunk()));
System.out.println("火車的運(yùn)費(fèi)是:"+zuche.useTraffic(new Train()));
System.out.println("飛機(jī)的運(yùn)費(fèi)是:"+zuche.useTraffic(new Plane()));
}
}