2012-08-07 20 views
8

I have jsp page -पास मूल्यों <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 .

उत्तर

6

का उपयोग कर सर्वलेट को यदि आप चाहते हैं एक यूआरएल का उपयोग कर सर्वलेट को मापदंडों भेजा है, अगर आप इस तरह

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

में यह करना चाहिए और उनमें मानों हो जाएगा पुनः प्राप्त अनुरोध में उपलब्ध है।

आपके दूसरे प्रश्न के बारे में। मैं नहीं कहूंगा आप ulr में एक param जोड़ सकते हैं,

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

और उस जानकारी का उपयोग किसी विशिष्ट विधि को कॉल करने के लिए करें।

वैसे, अगर आप Struts की तरह एक ढांचे का उपयोग, कि आसान Struts में के बाद से, आप एक विशिष्ट कार्रवाई विधि के लिए एक यूआरएल के लिए बाध्य कर सकते हैं हो सकता है (के कहते हैं कि "सर्वलेट" चलो)

संपादित:

आप इस तरीके से अपने सर्वलेट को परिभाषित किया है:

@WebServlet("/servlet123") 

आप, आप/servlet123 पर उपलब्ध हो जाएगा सर्वलेट। doc देखें।

मैं अपने कोड का परीक्षण किया है और यह काम कर रहा है:

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

फिर, मैं सर्वलेट http://localhost:8080/myApp/servlet123 में (किया जा रहा MyApp आपके आवेदन संदर्भ, यदि आप एक प्रयोग कर रहे हैं) कहा जाता है।

+0

धन्यवाद, इसलिए यदि मैं उपयोग URL का उपयोग करता हूं जैसे "goToServlet? Param1 = value1 और param2 = value2" सर्वलेट में कौन सी विधि लागू की जाएगी? डूगेट? – URL87

+0

आपको डूगेट का उपयोग करना चाहिए। इस उत्तर पर एक नज़र डालें: http://stackoverflow.com/a/2349741/980472 – jddsantaella

+0

ठीक है। अंतिम प्रश्न, मेरी संपादित पोस्ट देखें। – 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 आदि

+0

धन्यवाद, कृपया अब कैसे मैं दूसरे सर्वलेट में param1 और param2 के मूल्यों को पुनः प्राप्त करते पद – URL87

+0

में मेरे संपादन देखते हैं? – Sparker0i

1

मूल्य एन्कोड करने के लिए याद रखें अगर जरूरत

 संबंधित मुद्दे

  • कोई संबंधित समस्या नहीं^_^