使用JSP表单向Servlet发送字符串 - 在JSP中使用setAttribute,在Servlet中使用getAttribute

3

我正在尝试使用 JSP 表单发送数据、调用 Servlet,并在 Servlet 中显示该数据。

我想要使用 setAttribute 和 getAttribute 方法。

在这个 JSP 文件中,我正在使用 setAttribute 方法:

<HTML>
<HEAD>
    <TITLE>
        Multi Processor
    </TITLE>
</HEAD>
<BODY>
    <h4>This is a form submitted via POST:</h4>
    <FORM action = "/MyWebArchive/MulitProcessorServlet" method = "POST">
        Enter your name: <INPUT type="TEXT" name="name"/>
        <BR/>
        <INPUT type="submit"/>
    </FORM> 
    <BR/>
    <h4>This is a form submitted via GET:</h4>
    <FORM action = "/Week05WebArchive/MulitProcessorServlet">
        Enter your name: <INPUT type="TEXT" name="name"/>
        <BR/>
        <INPUT type="submit"/>
    </FORM>
</BODY>
<%
String strMasjidLocation = "Selimiyie Masjid Methuen";
session.setAttribute("MasjidLocation", strMasjidLocation);
%>

</HTML>

这是我想要使用getAttribute的servlet,但我不知道如何使用GetAttribute。你能给我展示需要添加到servlet中的附加代码,以便我可以捕获setAttribute中的值吗?

package temp22;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Locale;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class MulitProcessorServlet
 */
public class MulitProcessorServlet extends HttpServlet {

public void doGet(HttpServletRequest req, HttpServletResponse res)
        throws IOException, ServletException {

    doPost(req, res);
}

public void doPost(HttpServletRequest req, HttpServletResponse res)
        throws IOException, ServletException {

    String name = req.getParameter("name");
    StringBuffer page = new StringBuffer();
    String methodWhoMadeTheCall = req.getMethod();
    String localeUsed = req.getLocale().toString();

    String strMasjidLocation = null;
    //strMasjidLocation = this is where I would like to capture the value from the jsp that called this servlet.

    page.append("<HTML><HEAD><TITLE>Multi Form</TITLE></HEAD>");
    page.append("<BODY>");
    page.append("Hello " + name + "!");
    page.append("<BR>");
    page.append("The method who called me is: " + methodWhoMadeTheCall);
    page.append("<BR>");
    page.append("The language used is: " + localeUsed);
    page.append("<BR>");
    page.append("I am at this location: " + strMasjidLocation);
    page.append("</BODY></HTML>");

    res.setContentType("text/html");
    PrintWriter writer = res.getWriter();
    writer.println(page.toString());
    writer.close();
}
}

1
感谢大家的快速回复和帮助。 :-) - Emad-ud-deen
4个回答

4
这应该可以工作: String value = (String) req.getSession(false).getAttribute("MasjidLocation")

3
不要使用脚本片段;那是1999年的风格。学习JSTL并使用它编写您的JSP。
您的servlets绝不能嵌入HTML。只需验证和绑定参数,将其传递给服务进行处理,并将响应对象放置在请求或会话范围内以供JSP显示。

1
感谢您的快速回复。至少现在我想证明我可以使用setAttribute和getAttribute,一旦这个工作正常,我就可以转向您建议的内容,以便我可以看到所有不同的实现方式。 - Emad-ud-deen

2

我同意duffymo的观点,如果可能的话,你应该学习新技术(也许你的客户不能允许这样做...)。无论如何,要获取属性的值,你需要执行以下操作:

strMasjidLocation = (String)req.getSession().getAttribute("MasjidLocation");

此外,我注意到您在HTML的< form>标记中有两个不同的servlet路径:
MyWebArchive/MulitProcessorServlet

并且

Week05WebArchive/MulitProcessorServlet

这是否符合预期?


2
您使用了Session而不是Request。您可能需要从请求中获取Session。
String strMasjidLocation = request.getSession().getAttribute("MasjidLocation");

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接