在jsp页面按钮上调用servlet单击 [英] calling servlet on jsp page button click

查看:66
本文介绍了在jsp页面按钮上调用servlet单击的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在单击JSP按钮时调用一个servlet控制器.我无法使用表单提交按钮,因为它还有其他操作.除了AJAX调用之外,还有其他方法可以调用servlet吗?我想知道,是否有类似以下代码的选项

I want to call a servlet controller on JSP button click.I cannot use form submit button as I have other operations on it. Apart from an AJAX call is there any other way to call a servlet ? I wanted to know, is there any option like the below code

<a href="goToServlet?param1=value1&param2=value2">Go to servlet</a>

推荐答案

是的

Servlet的 doGet方法被映射到一个特定的URL,该URL可以直接访问.使用doPost方法无法实现.

A Servlet's doGet method is mapped onto a specific URL which can be accessed directly with the URL. This is not possible with a doPost method.

例如:

如果我有一个名为 TestServlet 的servlet,它具有url模式 testme

If i have a servlet called TestServlet which has a url-pattern testme,

然后我可以使用achor标签访问此servlet,如下所示:

then i can access this servlet with an achor tag like this:

<a href="/testme?param1=cool&param2=nice">Go to servlet</a>

web.xml

  <servlet>
    <servlet-name>TestServlet</servlet-name>
    <servlet-class>servlets.TestServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>TestServlet</servlet-name>
    <url-pattern>/testme</url-pattern>
  </servlet-mapping>

TestServlet doGet方法

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

          String param1 = request.getParameter("param1");
          String param2 = request.getParameter("param2");



        RequestDispatcher rd=request.getRequestDispatcher("anotherPage.jsp");    

        rd.forward(request,response);  
    }

}

这篇关于在jsp页面按钮上调用servlet单击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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