java 如何从java调用jsp文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2437768/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 21:08:23  来源:igfitidea点击:

how to call jsp file from java?

javajsp

提问by user246160

I have two jsp file and one java file. My constraints is if jspfile1 call java then java file call the jspfile2. Is it possible? How to achieve this?

我有两个jsp文件和一个java文件。我的限制是如果 jspfile1 调用 java 然后 java 文件调用 jspfile2。是否可以?如何实现这一目标?

回答by Thilo

If by "Java file" you mean a Servlet, you can use the RequestDispatcher:

如果“Java 文件”是指 Servlet,则可以使用 RequestDispatcher:

 request.getRequestDispatcher("/my.jsp").include(request, response);

 request.getRequestDispatcher("/my.jsp").forward(request, response);

回答by BalusC

The normalway is using a Servlet. Just extend HttpServletand map it in web.xmlwith a certain url-pattern. Then just have the HTML links or forms in your JSP to point to an URL which matches the servlet's url-pattern.

正常方式使用Servlet。只需扩展HttpServlet并将其映射web.xml到某个url-pattern. 然后只需在 JSP 中使用 HTML 链接或表单指向与 servlet 的url-pattern.

E.g. page1.jsp:

例如page1.jsp

<form action="servletUrl">
    <input type"submit">
</form>

or

或者

<a href="servletUrl">click here</a>

The <form>without methodattribute (which defaults to method="get") and the <a>links will call servlet's doGet()method.

<form>method属性(默认为method="get")和<a>链接将调用servlet的doGet()方法。

public class MyServlet extends HttpServlet {
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // Do your Java code thing here.
        String message = "hello";
        request.setAttribute("message", message); // Will be available in ${message}.

        // And then forward the request to a JSP file.
        request.getRequestDispatcher("page2.jsp").forward(request, response);
    }
}

If you have a <form method="post">, you'll have to replace doGetby doPostmethod.

如果您有<form method="post">,则必须doGetdoPost方法替换。

Map this servlet in web.xmlas follows:

web.xml如下映射这个 servlet :

<servlet>
    <servlet-name>myServlet</servlet-name>
    <servlet-class>com.example.MyServlet</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>myServlet</servlet-name>
    <url-pattern>/servletUrl</url-pattern>
</servlet-mapping>

so that it's available by http://example.com/contextname/servletUrl. The <form>and <a>URL's have to point either relatively or absolutely to exact that URL to get the servlet invoked.

以便它可以通过http://example.com/contextname/servletUrl. 在<form><a>URL的必须要么相对或绝对的指向精确该URL获得该servlet调用。

Now, this servlet example has set some "result" as request attribute with the name "message" and forwards the request to page2.jsp. To display the result in page2.jspjust do access ${message}:

现在,这个 servlet 示例已经将一些“结果”设置为名称为“消息”的请求属性,并将请求转发到page2.jsp. 要在page2.jspjust do access 中显示结果${message}

<p>Servlet result was: ${message}</p>

回答by fastcodejava

jspfiles get converted to a servlet. You cannot call them directly.

jsp文件被转换为servlet. 你不能直接打电话给他们。

EDIT: typo fixed.

编辑:错别字已修复。

回答by thelost

Do a http web request.

执行 http 网络请求。