[英]pass values from jsp to servlet using <a href>
I have JSP page - 我有JSP页面-
<html>
<head>
</head>
<body>
<%
String valueToPass = "Hello" ;
%>
<a href="goToServlet...">Go to servlet</a>
</body>
</html>
And servlet - 和servlet-
@WebServlet(name="/servlet123",
urlPatterns={"/servlet123"})
public class servlet123 extends HttpServlet {
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
}
public void foo() {
}
}
What should I write in <a href="goToServlet...">Go to servlet</a>
in order to pass values (like valueToPass
or maybe add the value as argument in the ) to the servlet123
? 我应该在<a href="goToServlet...">Go to servlet</a>
中写什么,以便将值(例如valueToPass
或可能将值添加为)添加到servlet123
?
Can I invoke the specific method in servlet123
(like foo()
) using the link in the JSP? 是否可以使用JSP中的链接调用servlet123
的特定方法(如foo()
)?
EDIT: 编辑:
How can I call servlet in URL? 如何在URL中调用servlet? My pages hierarchy is like the following - 我的页面层次结构如下所示-
WebContent
|-- JSPtest
| |-- callServletFromLink.jsp
|-- WEB-INF
: :
And I want to call the servlet123
in the folder src->control . 我想在servlet123
> control文件夹中调用servlet123
。
I tried <a href="servlet123">Go to servlet</a>
but it did not find the servlet when I press on the link. 我尝试了<a href="servlet123">Go to servlet</a>
但是当我按链接时它没有找到servlet。
2nd EDIT: 第二次编辑:
I tried <a href="http://localhost:8080/MyProjectName/servlet123">Go to servlet</a>
and it work . 我尝试<a href="http://localhost:8080/MyProjectName/servlet123">Go to servlet</a>
并成功了。
If you want to send parameters to the servlet using an URL, you should do it in this way 如果要使用URL将参数发送到servlet,则应采用这种方式
<a href="goToServlet?param1=value1¶m2=value2">Go to servlet</a>
And then retrieve the values that will be available in the request. 然后检索将在请求中可用的值。
Regarding your second question. 关于第二个问题。 I will say no. 我会说不。 You can add a param in the URL, something like 您可以在URL中添加参数,例如
<a href="goToServlet?method=methodName¶m1=value1">Go to servlet</a>
And the use of that information to call a specific method. 并使用该信息来调用特定方法。
By the way, if you use a framework like Struts, that will be easier since, in Struts, you can bound an URL to a specific Action method (let's say "servlet") 顺便说一句,如果您使用Struts之类的框架,这将更加容易,因为在Struts中,您可以将URL绑定到特定的Action方法(比如说“ servlet”)
Edited : 编辑 :
You have defined your servlet in this way: 您已通过以下方式定义了servlet:
@WebServlet("/servlet123")
You, your servlet will be available on /servlet123. 您,您的servlet将在/ servlet123上可用。 See doc . 请参阅doc 。
I have tested your code and it is working: 我已经测试过您的代码,并且可以正常工作:
@WebServlet(name = "/servlet123", urlPatterns = { "/servlet123" })
public class Servlet123 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.setContentType("text/html");
PrintWriter out = resp.getWriter();
out.write("<h2>Hello Friends! Welcome to the world of servlet annotation </h2>");
out.write("<br/>");
out.close();
}
}
Then, I called the servlet in http://localhost:8080/myApp/servlet123
(being myApp your application context if you are using one). 然后,我在http://localhost:8080/myApp/servlet123
调用了servlet(如果使用http://localhost:8080/myApp/servlet123
其作为应用程序上下文)。
<a href="url">urltitle</a>
allows you to define a url. <a href="url">urltitle</a>
允许您定义一个URL。 Calling a servlet from here is as good as calling it from a browser, just give the url as you would give it in browser to call the servlet like http://mysite.com?param1=val1¶m2=val2 etc. 从这里调用Servlet与从浏览器调用Servlet一样好,只需提供您在浏览器中提供的url,就像在http://mysite.com?param1=val1¶m2=val2等中调用servlet一样。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.