Java 如何将jsp的变量传递到标记?(HTML)
我不明白如何从jsp中的表单向标记传递值。。。我用servlet尝试过,并将其保存在会话中,但我认为主要问题在于我的索引,我不知道如何准确地编写代码 这是我的index.jsp:Java 如何将jsp的变量传递到标记?(HTML),java,html,jsp,Java,Html,Jsp,我不明白如何从jsp中的表单向标记传递值。。。我用servlet尝试过,并将其保存在会话中,但我认为主要问题在于我的索引,我不知道如何准确地编写代码 这是我的index.jsp: <form action="WunschServlet" method="post"> <my:geschenk> <input type="text" name="wish"> </my:
<form action="WunschServlet" method="post">
<my:geschenk>
<input type="text" name="wish">
</my:geschenk>
</form>
我已经定义并初始化了标记,所以这不是问题所在。我只是不知道如何将wishinput放到我的标签上我不清楚流程。当您调用index.jsp时,您在yoir浏览器中看到了什么?你能看到名为“wish”的输入框吗?不,我看不到,因为当我将鼠标悬停在index.jsp中的标记上时,它会说标记“(my:geschenk)必须为空”。我知道index.jsp中的编码有问题,但我不知道怎么做。对于“必须为空”这可能是tld中“body content”标记的问题。一般来说,如果我已经理解了您想要什么,请删除标记中的输入元素,并将Wish参数作为属性传递给标记(您必须修改tld)。我不清楚流程。当您调用index.jsp时,您在yoir浏览器中看到了什么?你能看到名为“wish”的输入框吗?不,我看不到,因为当我将鼠标悬停在index.jsp中的标记上时,它会说标记“(my:geschenk)必须为空”。我知道index.jsp中的编码有问题,但我不知道怎么做。对于“必须为空”这可能是tld中“body content”标记的问题。通常,如果我已经理解了您想要的内容,请删除标记中的输入元素,并将Wish参数作为属性传递给标记(您必须修改tld)
public class GeschenkTag extends SimpleTagSupport {
private String wish;
public void doTag() throws JspException, IOException {
JspWriter out = getJspContext().getOut();
PageContext pageContext = (PageContext) getJspContext();
HttpServletRequest request = (HttpServletRequest) pageContext.getRequest();
wish = request.getParameter("wish");
int counter = 0;
for(int i = 1; i <= wish.length(); i++) {
counter++;
}
if(counter > 10) {
out.println("<p>" + "Ich hoffe das Geschenk {Geschenk} kann ich mir merken!" + "</p>");
} else {
out.println("<p>" + "Das kann ich besorgen" + "</p>");
}
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String wish = request.getParameter("wish");
HttpSession session = request.getSession();
session.setAttribute("wish", wish);
request.getRequestDispatcher("index.jsp").forward(request, response);
}