大橙子网站建设,新征程启航
为企业提供网站建设、域名注册、服务器等服务
从 servlet转到 jsp不外乎两种方法,重定向 和 url转发
目前创新互联公司已为1000多家的企业提供了网站建设、域名、网站空间、网站托管、企业网站设计、清丰网站维护等服务,公司将坚持客户导向、应用为本的策略,正道将秉承"和谐、参与、激情"的文化,与客户和合作伙伴齐心协力一起成长,共同发展。
1、重定向 ( Redirect):是路径的跳转,内容和 url都改变。不允许带 request参数( session参数可以),即不允许在 servlet里给 request对象使用setAttribute方法传给下一页面。在 servlet里使用 response.sendRedirect(url) 方法。注意这里的 url前不带斜线 /,如 response.sendRedirect(”test.jsp“)
2、url转发 ( Forward):是页面的跳转,页面内容发生改变,url不变。可以带 request和 session参数。在 servlet里使用 getServletConfig().getServletContext().getRequestDispatcher(url).forward(request, response)。而这里的 url前需要带斜线 /,如getServletConfig().getServletContext().getRequestDispatcher(”/test.jsp“).forward(request, response)
servlet使用RequestDispatcher到jsp页面
String url="/EShop.jsp";
ServletContext sc = getServletContext();
RequestDispatcher rd = sc.getRequestDispatcher(url);
rd.forward(req, res);
servlet重定向jsp,并将数据发送到别一个JSP中:
request.setAttubute("messages",aaa);
RequestDispatcher requestDispatcher=request.getRequestDispatcher("/jsp/testbean.jsp");
requestDispatcher.forward(request,response);
3.7、Servlet跳转
从JSP跳转到Servlet可以实现,通过表单提交或超链接可以做到,但是现在如果从一个Servlet跳转到JSP呢?
• HttpServletResponse对象,此对象可以完成跳转: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
以上实现了跳转的功能,但是跳转之后,发现地址栏已经改变了,所以此跳转称为客户端跳转。如果此时假设一个Servlet要向JSP中传递属性,那么应该使用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
此时,发现可以通过session属性范围传递内容,但是如果现在传递的内容很多,而程序中使用session范围的话则肯定会带来额外的性能降低,但是此时,这些值只使用一次显示,那肯定没有必要使用session对象,最好使用request范围。如果request属性范围也有一个限制,必须是服务器端跳转,在JSP中使用jsp:forward语句完成,但是在servlet中呢?就只能使用RequestDsipatcher接口完成,实际上jsp:forward语句也是使用包装了此种应用,既然RequestDispatcher是一个接口,所以必须通过其他方式实例化,通过request对象完成。
JumpServlet.java:
public void doGet(HttpServletRequest req,HttpServletResponse resp)
throws ServletException,java.io.IOException{
req.setAttribute("name","hello") ;
// 准备跳转到此页面
RequestDispatcher rd = req.getRequestDispatcher("jump.jsp") ;
// 把所有的请求向下继续传递
rd.forward(req,resp) ;
}
注意:
当需要传递request属性范围的时候才使用服务器端跳转。如果不需要传递,则可以选择任意的跳转方式。
但是一般情况下都使用RequestDispatcher进行跳转,而且一定要记住的时候,跳转之后页面地址不会有任何的变化。
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("长度:");
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("价格是:%f", price);
}
}
import java.util.Scanner;
interface Traffic{
double calc(double weight,double distance);//计算运费
}
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())?"超重或者距离太远不能接单":"拒载");
}
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("卡车的运费是:"+zuche.useTraffic(new Trunk()));
System.out.println("火车的运费是:"+zuche.useTraffic(new Train()));
System.out.println("飞机的运费是:"+zuche.useTraffic(new Plane()));
}
}