sr.registerServlet( "InternalServlet", InternalServlet.class.getName() );
//创建访问Servlet的客户端
ServletUnitClient sc = sr.newClient();
//发送请求
WebRequest request = new GetMethodWebRequest( " http://localhost/InternalServlet " );
request.setParameter("pwd","pwd");
//获得该请求的上下文环境
InvocationContext ic = sc.newInvocation( request );
//调用Servlet的非服务方法
InternalServlet is = (InternalServlet)ic.getServlet();
is.myMethod();
//直接通过上下文获得request对象
System.out.println("request中获取的内容:"+ic.getRequest().getParameter("pwd"));
//直接通过上下文获得response对象,并且向客户端输出信息
ic.getResponse().getWriter().write("haha");
//直接通过上下文获得session对象,控制session对象
//给session赋值
ic.getRequest().getSession().setAttribute("username","timeson");
//获取session的值
System.out.println("session中的值 :"+ic.getRequest().getSession().getAttribute("username"));
//使用客户端获取返回信息,并且打印出来
文章来源于领测软件测试网 https://www.ltesting.net/