如何通过JSP页面调用servlet [英] How to call servlet through a JSP page

查看:89
本文介绍了如何通过JSP页面调用servlet的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过JSP页面调用Servlet.什么叫方法?

I would like to call a Servlet through a JSP page. What is the method to call?

推荐答案

可以为此使用<jsp:include>.

<jsp:include page="/servletURL" />

然而,通常情况恰恰相反.您调用servlet,该servlet继而转发到JSP以显示结果.创建一个Servlet,其功能类似于doGet()方法中的操作.

It's however usually the other way round. You call the servlet which in turn forwards to the JSP to display the results. Create a Servlet which does something like following in doGet() method.

request.setAttribute("result", "This is the result of the servlet call");
request.getRequestDispatcher("/WEB-INF/result.jsp").forward(request, response);

/WEB-INF/result.jsp

<p>The result is ${result}</p>

现在通过与web.xml中的<url-pattern>相匹配的URL调用Servlet,例如 http://example.com/contextname/servletURL .

Now call the Servlet by the URL which matches its <url-pattern> in web.xml, e.g. http://example.com/contextname/servletURL.

如果您的实际问题是如何向Servlet提交表单?"那么您只需以HTML形式action指定servlet URL.

If your actual question is "How to submit a form to a servlet?" then you just have to specify the servlet URL in the HTML form action.

<form action="servletURL" method="post">

doPost()方法将被调用.

  • Servlets info page - Contains a hello world
  • How to call servlet class from HTML form
  • How do I pass current item to Java method by clicking a hyperlink or button in JSP page?
  • Show JDBC ResultSet in HTML in JSP page using MVC and DAO pattern
  • Design Patterns web based applications

这篇关于如何通过JSP页面调用servlet的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆