2012-08-07 12 views
8

I have jsp page -giá trị bổng từ jsp để servlet sử dụng <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 .

Trả lời

6

Nếu bạn muốn gửi tham số cho các servlet sử dụng một URL, bạn nên làm điều đó theo cách này

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

Và họ lấy các giá trị đó sẽ là có sẵn trong yêu cầu.

Về câu hỏi thứ hai của bạn. Tôi sẽ nói không. Bạn có thể thêm thông số vào ulr, một cái gì đó như là

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

Và sử dụng thông tin đó để gọi một phương thức cụ thể.

Bằng cách này, nếu bạn sử dụng một khuôn khổ như Struts, mà sẽ được dễ dàng hơn vì trong Struts, bạn có thể ràng buộc một URL đến một phương pháp hành động cụ thể (giả sử "servlet")

Sửa:

bạn đã xác định servlet của bạn theo cách này:

@WebServlet("/servlet123") 

bạn, bạn servlet sẽ có mặt trên/servlet123. Xem doc.

Tôi đã kiểm tra mã của bạn và nó đang làm việc:

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

Sau đó, tôi gọi là servlet trong http://localhost:8080/myApp/servlet123 (là MyApp bối cảnh ứng dụng của bạn, nếu bạn đang sử dụng một).

+0

cảm ơn, vì vậy nếu tôi sử dụng URL như "goToServlet? Param1 = value1 & param2 = value2" thì phương thức nào trong servlet sẽ được gọi? doGet? – URL87

+0

Bạn nên sử dụng doGet. Hãy xem câu trả lời này: http://stackoverflow.com/a/2349741/980472 – jddsantaella

+0

OK. cuối cùng Q, xem bài viết đã chỉnh sửa của tôi. – 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, vv

+0

cảm ơn, vui lòng xem chỉnh sửa của tôi trong bài – URL87

+0

Bây giờ làm thế nào để lấy các giá trị của param1 và param2 trong servlet thứ hai? – Sparker0i