温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

如何测试JSP容器

发布时间:2021-11-22 10:39:21 来源:亿速云 阅读:123 作者:小新 栏目:编程语言

这篇文章给大家分享的是有关如何测试JSP容器的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。

由于上面给出例 复杂 。一般人很难理解。我也是 ^_^但仔细看我还是自己写出一个比较简单的,望大家一起讨论。被测试JSP容器

<%@ taglib prefix="html" uri="/WEB-INF/struts-html.tld" %> <%@ taglib prefix="c" uri="/WEB-INF/c-1_0-rt.tld" %> <html:html> <c:if test="${name != pass}"> ${name}  <br> ${pass} <br> <!-- <html:text property="in" ></html:text> --> </c:if> </html:html>

ant直接把他放在 eclipes 工程的根目录下 build.xml但有有3个参数要设置 tomcat.home Tomcat 的地址webapp.path 工程中的根目录,下面有WEB-INFsrc 原代码 (到时候JSP会翻译成.java到这个目录的 org.apache.JSP.JSP 下)

<project name="Webapp Precompilation" default="all" basedir="."> <!-- tomcat dir --> <property name="tomcat.home" value="D:\Tomcat 5.0"/> <!-- this=..//WEB-INF (in eclipes)  -->   <property name="webapp.path" value=".\WebRoot"/> <!-- src (in eclipes) --> <property name="src" value="./src"/> <target name="jspc"> <taskdef classname="org.apache.jasper.JspC" name="jasper2" > <classpath id="jspc.classpath"> <pathelement location="${java.home}/../lib/tools.jar"/> <fileset dir="${tomcat.home}/bin"> <include name="*.jar"/> </fileset> <fileset dir="${tomcat.home}/server/lib"> <include name="*.jar"/> </fileset> <fileset dir="${tomcat.home}/common/lib"> <include name="*.jar"/> </fileset> </classpath> </taskdef> <jasper2 validateXml="false" uriroot="${webapp.path}" webXmlFragment="${webapp.path}/WEB-INF/generated_web.xml" outputDir="${src}" /> </target> <target name="compile"> <mkdir dir="${webapp.path}/WEB-INF/classes"/> <mkdir dir="${webapp.path}/WEB-INF/lib"/> <javac destdir="${webapp.path}/WEB-INF/classes" optimize="off" debug="on" failonerror="false" srcdir="${src}" excludes="**/*.smap"> <classpath> <pathelement location="${webapp.path}/WEB-INF/classes"/> <fileset dir="${webapp.path}/WEB-INF/lib"> <include name="*.jar"/> </fileset> <pathelement location="${tomcat.home}/common/classes"/> <fileset dir="${tomcat.home}/common/lib"> <include name="*.jar"/> </fileset> <pathelement location="${tomcat.home}/shared/classes"/> <fileset dir="${tomcat.home}/shared/lib"> <include name="*.jar"/> </fileset> <fileset dir="${tomcat.home}/bin"> <include name="*.jar"/> </fileset> </classpath> <include name="**" /> <exclude name="tags/**" /> </javac> </target> <target name="all" depends="jspc,compile"> </target> <target name="cleanup"> <delete> <fileset dir="${webapp.path}/WEB-INF/src"/> <fileset dir="${webapp.path}/WEB-INF/classes/org/apache/jsp"/> </delete> </target> </project>

TEST

package jetty.test.supper;  import javax.servlet.http.HttpServletRequest;  import javax.servlet.http.HttpServletResponse;  import org.apache.jsp.jsp.MyJsp_jsp;  import com.meterware.httpunit.GetMethodWebRequest;  import com.meterware.httpunit.WebRequest;  import com.meterware.httpunit.WebResponse;  import com.meterware.servletunit.InvocationContext;  import com.meterware.servletunit.ServletRunner;  import com.meterware.servletunit.ServletUnitClient;  import junit.framework.TestCase;  public class JSPCTest extends TestCase{  private InvocationContext ic = null ;  protected void setUp() throws Exception {  ServletRunner sr = new ServletRunner();  // 向环境中注册 jsp           sr.registerServlet("HelloWorld", MyJsp_jsp.class.getName());  ServletUnitClient  sc = sr.newClient();  WebRequest request = new GetMethodWebRequest("http://localhost/HelloWorld");  ic = sc.newInvocation(request);  }  public void testJspC() throws Exception{  HttpServletRequest re =  ic .getRequest();  HttpServletResponse rq =  ic.getResponse();  re.setAttribute("name","liukaiyi");  re.setAttribute("pass","123456");  MyJsp_jsp is = (MyJsp_jsp) ic.getServlet();  is._jspService(re,rq);  WebResponse response = ic.getServletResponse();  // 输出          System.out.println( response.getText() );  }  }

结果是

<html> liukaiyi  <br> 123456 <br> </html>

感谢各位的阅读!关于“如何测试JSP容器”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

jsp
AI