我有一个java类,它对文件执行一些操作.由于java代码很大,我不想在jsp中编写这段代码.我想在需要时调用jsp中的方法.
请告诉我保存此文件所需的路径.另外一些示例代码如何使用它将是有帮助的.
che*_*vim 24
在servlet中(在JSP之前运行):
Person p = new Person(); // instantiate business object
p.init(...); // init it or something
request.setAttribute("person", p); // make it available to the template as 'person'
Run Code Online (Sandbox Code Playgroud)
在模板中,您可以使用:
your age is: ${person.age} <%-- calls person.getAge() --%>
Run Code Online (Sandbox Code Playgroud)
Sea*_*wen 10
我认为问题是,如何让Java代码可用于JSP?您可以像任何其他Java代码一样使它可用,这意味着它需要编译成.class文件并放在类路径上.
在Web应用程序中,这意味着类文件必须存在于应用程序的.war文件或目录中的WEB-INF/classes下,与通常的目录结构匹配.因此,编译和部署此代码以及所有其他应用程序Java代码,它应该在正确的位置.
请注意,您需要在JSP中导入您的类,或使用完全限定的类名,否则您可以使用<%%>语法编写您喜欢的任何Java代码.
您还可以使用<%!在其他实用程序JSP中声明一个方法.%>语法(注意!),导入JSP,然后调用在这样的块中声明的方法.这是糟糕的风格.
根据您要调用的操作类型,通常使用taglibs,EL函数或servlet.Java代码确实不属于JSP文件,而是属于Java类.
如果要预处理请求,请使用Servlet doGet()方法.例如
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// Preprocess request here.
doYourThingHere();
// And forward to JSP to display data.
request.getRequestDispatcher("page.jsp").forward(request, response);
}
Run Code Online (Sandbox Code Playgroud)
如果要在某个表单提交后对请求进行后处理,请改用Servlet doPost()方法.
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// Postprocess request here.
doYourThingHere();
// And forward to JSP to display results.
request.getRequestDispatcher("page.jsp").forward(request, response);
}
Run Code Online (Sandbox Code Playgroud)
如果要控制页面流和/或HTML输出,请使用类似JSTL核心标记库的标记库或创建自定义标记.
如果要执行静态/辅助函数,请使用JSTL fn taglib等EL函数或创建自定义函数.
| 归档时间: |
|
| 查看次数: |
96691 次 |
| 最近记录: |