I have JSP page –
<html>
<head>
</head>
<body>
<%
String valueToPass = "Hello" ;
%>
<a href="goToServlet...">Go to servlet</a>
</body>
</html>
And 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 ?
Can I invoke the specific method in servlet123 (like foo()) using the link in the JSP?
EDIT:
How can I call servlet in URL? 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 .
I tried <a href="servlet123">Go to servlet</a> but it did not find the servlet when I press on the link.
2nd EDIT:
I tried <a href="http://localhost:8080/MyProjectName/servlet123">Go to servlet</a> and it work .
If you want to send parameters to the servlet using an URL, you should do it in this way
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
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”)
Edited:
You have defined your servlet in this way:
You, your servlet will be available on /servlet123. See doc.
I have tested your code and it is working:
Then, I called the servlet in
http://localhost:8080/myApp/servlet123(being myApp your application context if you are using one).