Servlet与Servlet、JSP与Servlet的跳转

Servlet与Servlet、JSP与Servlet的跳转,第1张

目录

Servlet与Servlet之间的跳转

forward方法实例:

用from表单从JSP页面转到Servlet

实例

Servlet与Servlet之间的跳转

Servlet和Servlet之间的跳转,可以通过请求转发来实现,即RequestDispatcher对象的forward方法;也可以使用重定向方法,即HttpServletResponse对象的sendRedirect方法。

forward方法实例:

调用OperServlet请求,将request请求中的cout属性进行开根号处理,之后将服务转发给另一个Servlet处理,即operTwoServlet

OperServlet:

/**
 * 
 * 
  • 只做一件事情的组件:开根号处理 * */ public class OperServlet extends HttpServlet { public void init(){} public void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException { //获取请求参数 String strcount=request.getParameter("count"); int count = (int)Double.parseDouble(strcount); //int count = (int)Integer.parseInt(strcount); //进行开根号处理 count = (int)Math.sqrt(count); String str=String.valueOf(count); //设置请求上属性的参数 request.setAttribute("count",str); // 转发给另一个Servlet来处理 request.getRequestDispatcher("operTwoServlet").forward(request,response); } }
  • operTwoServlet接收请求,将request请求中的cout属性进行乘以10处理,之后将服务转发给另一个Servlet处理,即showServlet

    operTwoServlet:

    /**
     * 
     * 
  • 只做一件事情的组件:乘10 * */ public class OperTwoServlet extends HttpServlet { public void init(){} public void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException { // 获取请求属性上的参数 String strcount=(String)request.getAttribute("count"); int count=Integer.parseInt(strcount); // 进行乘10处理 count *= 10; // 再次置入请求属性的参数 request.setAttribute("count",String.valueOf(count)); // 转发给另一个Servlet来处理 request.getRequestDispatcher("showServlet").forward(request,response); } }
  • showServlet接收请求,将运算后的结果输出在网页上。

    showServlet:

    /**
     * 
     * 
  • 只做一件事情的处理,显示,即往客户端推流 * */ public class ShowServlet extends HttpServlet { public void init(){} public void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException { response.setContentType("text/html;charset=utf-8"); PrintWriter out=response.getWriter(); //获取请求属性上的参数 String strcount=(String)request.getAttribute("count"); String prefix = "开方十乘处理后的结果是:"; out.println(prefix+strcount); } }
  • 运行: 

     

     

     

    请求转发服务器只会对servlet发出一次请求,剩下的都是在servlet内部进行任务的传输交接,而重定向则是服务器会对servlet进行两次或者多次的请求,如图:

     

    用from表单从JSP页面转到Servlet

    form表单中的action属性的值是表单提交给的url,如果我们没有设置action属性,那么默认的表单还是提交给当前页面

    。。。

     action属性规定当提交表单时,向何处发送表单数据。例如上例,提交表单时,表单数据会提交到名为 “addServlet” 的页面。

    实例

    运行html文件,显示在网页上的界面:

       图1.1

    其中供用户在网页上使用的两个 *** 作的代码,即两个from表单,在html文件中的代码:

    1. 图1.1中 *** 作1
    输入您要购买的产品名称:

    当用户执行完表单中输入文本域("text")与提交按钮("submit")之后,便会转发一个请求到addServlet。AddServlet.java文件源代码:

    /**
     * 
     * 
  • 添加购物车 * */ public class AddServlet extends HttpServlet { public void init(){} public void doGet( HttpServletRequest request, HttpServletResponse response) throws ServletException,IOException { // 配置输出参数 response.setContentType("text/html;charset=utf-8"); // 输出对象,即开流 PrintWriter out=response.getWriter(); // 创建session对象,即创建会话 HttpSession session=request.getSession(); // 获取参数,强转为相应的类型,更方便使用 String product=(String)request.getParameter("product"); String shopping=(String)session.getAttribute("shopping"); // 设置参数的编码,避免中文乱码 String newproduct=new String(product.getBytes(),"utf-8"); if(shopping==null) // 首次,肯定是null状态 { //设置session域的属性参数值 session.setAttribute("shopping",newproduct); } else { String str=shopping+"

    "+newproduct; session.setAttribute("shopping",str); } out.println("产品已添加至购物车中!"); } }

  •  可以看到,运行时将用户的输入(即product对象)通过request请求转化为字符串格式,并将其存储在session会话中的shopping对象中。

    2. 图1.1中 *** 作2

    当用户点击提交按钮("submit")之后,便会运行shoppingServlet文件,可以查看之前用户提交的购物商品。shoppingServlet.java文件源代码: 

    /**
     * 
     * 
  • 购物车信息 * */ public class ShoppingServlet extends HttpServlet { public void init(){} public void doGet( HttpServletRequest request, HttpServletResponse response) throws ServletException,IOException { response.setContentType("text/html;charset=utf-8"); PrintWriter out=response.getWriter(); //获取session HttpSession session=request.getSession(); //获取属性值 String shopping=(String)session.getAttribute("shopping"); if( shopping==null ) { out.println("购物车为空!"); } else { out.println("购物车

    "+shopping); } } }

  • 即将session中shopping对象的值输出到网页上。

    若用户没有提交购物商品,即shopping值为空,网页显示为:

    若用户提交过购物商品,即执行过 *** 作1,网页显示为;

    欢迎分享,转载请注明来源:内存溢出

    原文地址:https://54852.com/langs/723372.html

    (0)
    打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
    上一篇 2022-04-26
    下一篇2022-04-26

    发表评论

    登录后才能评论

    评论列表(0条)

      保存