2012-08-07 21 views
8

I have jsp page -valori passano da jsp per servlet utilizzando <a href>

<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 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 follow -

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 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 .

risposta

6

Se si desidera inviare i parametri al servlet utilizzando un URL, si dovrebbe fare in questo modo

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

E li recuperare i valori che saranno disponibile nella richiesta.

Per quanto riguarda la seconda domanda. Dirò di no. È possibile aggiungere un parametro in ulr, qualcosa come

<a href="goToServlet?method=methodName&param1=value1">Go to servlet</a> 

E utilizzare tali informazioni per chiamare un metodo specifico.

A proposito, se si utilizza un framework come Struts, che sarà più facile dal momento che in Struts, è possibile legato un URL a un metodo di azione specifica (diciamo "servlet")

Redatta:

aver definito il servlet in questo modo:

@WebServlet("/servlet123") 

Tu, tu servlet sarà disponibile on/servlet123. Vedi doc.

Ho testato il codice e si sta lavorando:

@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(); 
    } 
} 

Poi, ho chiamato il servlet in http://localhost:8080/myApp/servlet123 (essendo myApp vostro contesto di applicazione, se si utilizza uno).

+0

grazie, quindi se uso uso URL come "goToServlet? Param1 = value1 & param2 = value2" quale metodo nel servlet verrà richiamato? il doGet? – URL87

+0

Dovresti usare doGet. Dai un'occhiata a questa risposta: http://stackoverflow.com/a/2349741/980472 – jddsantaella

+0

OK. l'ultimo Q, vedi il mio post modificato. – URL87

1

<a href="url">urltitle</a> allows you to define 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&param2=val2 ecc

+0

ringraziamento, si prega di vedere la mia modifica nel post – URL87

+0

Ora come faccio a recuperare i valori di param1 e param2 nel secondo servlet? – Sparker0i

Problemi correlati