你能用一个链接调用一个servlet吗? [英] Can you call a servlet with a link?

查看:240
本文介绍了你能用一个链接调用一个servlet吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你能用一个链接调用一个servlet吗?例如

Can you call a servlet with a link? For example

<a href="/servletName">link text</a>

并且可能通过将参数添加到查询字符串来将参数传递给请求对象。

And possibly pass parameters to the request object by adding them to the querystring.

如果没有,我见过这样的事情:

If not, I have seen this kind of thing:

RequestDispatcher dispatcher = getServletContext().getRequestDispatcher(/MyServlet); 
dispatcher.include(request,response); 

但我该如何触发呢?例如,如果它是JavaScript代码,我可以把它放在一个jQuery单击函数中,或者如果这是一个servlet,我会把它放到一个方法中。

But how would I trigger this? For example if it was JavaScript code I could put it within a jQuery click function, or if this was a servlet I would put it into a method.

但我怎么做从JSP中调用此代码。据我所知,你不能用JavaScript事件调用Java代码。

But how do I call this code from within a JSP. As far as I know you can't call Java code with JavaScript events.

推荐答案

<a href="servletUrl?param=value">click</a>

完全合法且有效。

这将调用servlet的 doGet(..)方法,并且可以使用 request.getParameter(param)获取参数)

That will make the doGet(..) method of the servlet be called, and you can get the parameter using request.getParameter("param")

这篇关于你能用一个链接调用一个servlet吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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