【发布时间】:2012-07-25 13:02:27
【问题描述】:
<body>
<form action="testServlet.java">
<TABLE border="0" align="center">
<TR height="40">
<TD width="40"><a href="Hoda/testServlet?direction=b"><img
src=<%=request.getAttribute("imgSrc")%> width="40" height="40" /></a>
</TD>
</form>
</body>
伺服器:
@WebServlet("/testServlet")
public class testServlet extends HttpServlet {
String imgSrc = "red.png";
protected void service(HttpServletRequest reques,HttpServletResponse response) throws ServletException, IOException {
String str = request.getParameter("direction");
if (str.startsWith("b")) {
imgSrc = "black.png";
}
request.setAttribute("imgSrc", imgSrc);
}
}
在我的 JSP 页面中,我创建了一个单元格,我想从 servlet 获取其图像源。我把链接标签向 servlet 询问 imgSrc,但它不起作用。请告诉我如何使用 servlet 更改 JSP 页面中的 imgSrc。我希望 JSP 只显示结果,而不是分派到另一个页面。 这是我的代码:
【问题讨论】:
-
定义“不起作用”。您必须单击链接来请求 servlet;你这样做然后转发到同一个jsp?看起来不像——你确定你知道 servlet 和客户端是如何交互的吗?
-
对我来说看起来不错。除了表单操作负责与 servlet 通信,所以我不希望在您按下表单上的按钮或其他东西之前加载图像,因此可以执行操作
-
@MoatazElmasry 是吗?提交到源文件,以及指向不重定向或转发的 servlet 的链接?
-
我是jsp的新手,你能告诉我如何将req从jsp转发到servlet吗?