栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Servlet与Servlet、JSP与Servlet的跳转

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Servlet与Servlet、JSP与Servlet的跳转

目录

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:

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,网页显示为;

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/830776.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号