URL*_*L87 8 java jsp servlets href
我有jsp页面 -
<html>
<head>
</head>
<body>
<%
String valueToPass = "Hello" ;
%>
<a href="goToServlet...">Go to servlet</a>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
和servlet -
@WebServlet(name="/servlet123",
urlPatterns={"/servlet123"})
public class servlet123 extends HttpServlet {
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
}
public void foo() {
}
}
Run Code Online (Sandbox Code Playgroud)
<a href="goToServlet...">Go to servlet</a>为了传递值(比如valueToPass或者可能将值作为参数添加到),我应该写什么servlet123?
我可以使用jsp中的链接调用servlet123(例如foo())中的特定方法吗?
编辑:
如何在URL中调用servlet?我的页面层次结构如下 -
WebContent
|-- JSPtest
| |-- callServletFromLink.jsp
|-- WEB-INF
: :
Run Code Online (Sandbox Code Playgroud)
我想调用servlet123文件夹src-> control.
我试过<a href="servlet123">Go to servlet</a>但是当我按下链接时它找不到servlet.
第二次编辑:
我试过<a href="http://localhost:8080/MyProjectName/servlet123">Go to servlet</a>,它的工作原理.
如果要使用URL将参数发送到servlet,则应以这种方式执行
<a href="goToServlet?param1=value1¶m2=value2">Go to servlet</a>
Run Code Online (Sandbox Code Playgroud)
并且他们检索请求中可用的值.
关于你的第二个问题.我会说不.你可以在ulr中添加一个参数
<a href="goToServlet?method=methodName¶m1=value1">Go to servlet</a>
Run Code Online (Sandbox Code Playgroud)
并使用该信息来调用特定方法.
顺便说一句,如果你使用像Struts这样的框架,那么在Struts中会更容易,你可以将URL绑定到特定的Action方法(比如说"servlet")
编辑:
您已经以这种方式定义了servlet:
@WebServlet("/servlet123")
Run Code Online (Sandbox Code Playgroud)
您,您的servlet将在/ servlet123上可用.见文档.
我测试了你的代码并且它正在工作:
@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();
}
}
Run Code Online (Sandbox Code Playgroud)
然后,我调用了servlet http://localhost:8080/myApp/servlet123(作为myApp你的应用程序上下文,如果你使用的话).
| 归档时间: |
|
| 查看次数: |
62087 次 |
| 最近记录: |